/** * {@inheritdoc} */ public function write(Config $config, $target) { $file = new File($target); if ($file->exists()) { throw new \LogicException(sprintf("Config target file already exists '%s'", $target)); } $data = sprintf("<?php\n\n// \nreturn %s;\n", var_export($config->getData(), true)); $file->write($data); return $this; }
/** * @param boolean $process * * @return File */ public function getFile($process = true) { $config = $this->resource->getManager()->getConfig($this->resource->getName(), $this->name); $path = $this->compilePath($config['path']); $file = new File($path); $file->guess(); if ($process && $this->isProcessable()) { $this->processor->run($this); } return $file; }
/** * Creator * * @param mixed $data Source (automatic determining) or reader config * @param array $args Constructor arguments * * @throws \InvalidArgumentException * @return $this */ public static function objectify($data, $args = array()) { if (is_string($data)) { $source = new File($data); $extension = $source->getExtension(); switch ($extension) { case 'php': case 'php5': return new Php(); break; } } return Objector::objectify($data, $args, get_called_class()); }
/** * @param string $source * * @throws \LogicException * @throws \ErrorException * * @return Config */ public function read($source) { $file = new File($source); if (!$file->exists()) { throw new \LogicException(sprintf("Config source file '%s' does not exist.", $source)); } // Possible syntax errors, do not handle them / expensive! $path = $file->getPath(); $data = (include $path); if (!is_array($data)) { throw new \ErrorException("Config source file should return array: '%s'", $source); } $config = new Config(); $config->setData($data); return $config; }
/** * Prepare before using filters * * @return $this */ protected function prepare() { if (empty($this->filters)) { $this->filters[] = $this->manager->getFilter($this->source->getExtension()); } foreach ($this->filters as $filter) { $filter->prepare($this); } return $this; }
/** * Write log message * * @param string $message Message * @param string $level Level * * @return $this * @throws \InvalidArgumentException */ public function write($message, $level) { if (empty($message)) { throw new \InvalidArgumentException(sprintf("Logger write error. Message cannot be empty.")); } if (!in_array($level, self::$levels)) { throw new \InvalidArgumentException(sprintf("Logger write error. Invalid level '%s'", $level)); } $date = new \DateTime('now'); $this->template->set('level', mb_strtoupper($level))->set('message', trim($message))->set('date', $date->format('Y-m-d H:i:s.u')); if ($this->callback !== null && is_callable($this->callback)) { call_user_func($this->callback, $this->template); } $text = $this->template->render(); $this->file->append($text); return $this; }
/** * @param null $process * * @return File */ public function getFile($process = null) { $config = $this->manager->getConfig($this->name); $path = $this->compilePath($config['path']); $file = new File($path); $file->guess(); return $file; }
/** * Upload resource * * @param array $data File data * @param array $params Path parameters * @param bool $useTemp Use temporary state * * @throws \InvalidArgumentException * @throws \ErrorException * @return Resource|null */ public function upload($data, $params = array(), $useTemp = true) { if (empty($data)) { throw new \InvalidArgumentException("File data to be uploaded is empty."); } $resource = null; switch ($data['error']) { case UPLOAD_ERR_OK: $source = new File($data['name']); $temp = new File($data['tmp_name']); $params = array_merge(array('filename' => $source->getFileName(), 'extension' => $source->getExtension()), $params); $resource = $this->map(static::UPLOAD, $params); $target = $resource->getFile(); $temp->move($target); break; case UPLOAD_ERR_NO_FILE: if ($useTemp) { $resource = $this->map(static::UPLOAD, $params); } else { throw new \ErrorException("File has not been uploaded."); } break; case UPLOAD_ERR_INI_SIZE: throw new \ErrorException(sprintf("Uploaded file size exceeds server limit: %d MB", Php::get('upload_max_filesize'))); break; case UPLOAD_ERR_FORM_SIZE: throw new \ErrorException("Uploaded file size exceeds form limit."); break; case UPLOAD_ERR_PARTIAL: throw new \ErrorException("Uploaded file is only partially completed."); break; case UPLOAD_ERR_NO_TMP_DIR: throw new \ErrorException("Missing temporary directory for uploaded file."); break; case UPLOAD_ERR_CANT_WRITE: throw new \ErrorException("Failed to write uploaded file to disk."); break; case UPLOAD_ERR_EXTENSION: default: throw new \ErrorException("Unknown upload error."); break; } return $resource; }
/** * Generate hashed filename with same extension * * @param string $source Path * @param Encrypter $encrypter Encrypter * * @return File */ public function hashFile($source, Encrypter $encrypter = null) { if (!$source instanceof File) { $source = new File($source); } if ($encrypter === null) { $encrypter = new Encrypter(); } $hash = $encrypter->crypt($source->getAbsolutePath()); $path = $this->getPath(); $ext = $source->getExtension(); $target = new File($path . '/' . $hash . '.' . $ext); return $target; }