/** * {@inheritdoc} */ public function publish($filename, $destination, $merge = self::FOLLOW, $mode = FilesInterface::READONLY) { if (!$this->files->isFile($filename)) { throw new PublishException("Given '{$filename}' is not valid file."); } //wtf? always empty if (empty($relativeFilename)) { $relativeFilename = $this->files->normalizePath($this->files->relativePath($filename, $this->directories->directory('root'))); } //wtf? if (empty($relativeDestination)) { $relativeDestination = $this->files->relativePath($destination, $this->directories->directory('root')); } if ($this->files->exists($destination)) { if ($this->files->md5($destination) == $this->files->md5($filename)) { $this->logger()->debug("File '{relativeFilename}' already published and latest version.", compact('relativeFilename', 'destination')); //Nothing to do return; } if ($merge == self::FOLLOW) { //We are not allowed to replace file $this->logger()->warning("File '{relativeFilename}' already published and can not be replaced.", compact('relativeFilename', 'destination')); return; } } $this->logger()->info("Publish file '{relativeFilename}' to '{relativeDestination}'.", compact('relativeFilename', 'relativeDestination')); $this->ensureDirectory(dirname($destination), $mode); $this->files->copy($filename, $destination); $this->files->setPermissions($destination, $mode); }
/** * {@inheritdoc} */ public function getTimestamp($key) { if ($this->files->exists($key)) { return $this->files->time($key); } return 0; }
/** * {@inheritdoc} */ public function open($filename) { if (!$this->files->exists($filename)) { throw new ImporterException("Unable import translator bundles from '{$filename}', file does not exists."); } $this->parseStrings($this->files->read($filename)); return $this; }
/** * Last update time. * * @param string $key * @return int */ public function getTimestamp($key) { if (!$this->environment->cachable()) { //Always expired return 0; } if ($this->files->exists($key)) { return $this->files->time($key); } return 0; }
/** * {@inheritdoc} * * @param int $expiration Current expiration time value in seconds (reference). */ public function get($name, &$expiration = null) { if (!$this->files->exists($filename = $this->makeFilename($name))) { return null; } $cacheData = unserialize($this->files->read($filename)); if (!empty($cacheData[0]) && $cacheData[0] < time()) { $this->delete($name); return null; } return $cacheData[1]; }
/** * @param string $config * @return string * @throws RegistratorException */ protected function configFilename($config) { $filename = $this->directories->directory('config') . $config . Configurator::EXTENSION; if (!$this->files->exists($filename)) { throw new RegistratorException("Unable to find filename for config '{$config}'."); } return $filename; }
/** * Load environment data. * * @return $this */ public function load() { if (!$this->files->exists($this->filename)) { throw new EnvironmentException("Unable to load environment, file is missing"); } //Unique env file hash $this->id = $this->files->md5($this->filename); if (!empty($values = $this->memory->loadData($this->id, static::MEMORY_SECTION))) { //Restore from cache $this->initEnvironment($values); return $this; } //Load env values using DotEnv extension $values = $this->initEnvironment($this->parseValues($this->filename)); $this->memory->saveData($this->id, $values, static::MEMORY_SECTION); return $this; }
/** * Cast local filename to be used in file based methods and etc. * * @param string|StreamInterface $source * @return string */ protected function castFilename($source) { if (empty($source) || is_string($source)) { if (!$this->files->exists($source)) { //This code is going to use additional abstraction layer to connect storage and guzzle return StreamWrapper::getUri(\GuzzleHttp\Psr7\stream_for('')); } return $source; } if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) { $source = $source->getStream(); } if ($source instanceof StreamInterface) { return StreamWrapper::getUri($source); } throw new ServerException("Unable to get filename for non Stream instance."); }
/** * @param DirectoriesInterface $directories * @param FilesInterface $files * @param EncrypterConfig $config */ public function perform(DirectoriesInterface $directories, FilesInterface $files, EncrypterConfig $config) { $envFilename = $directories->directory('root') . '.env'; if (!$files->exists($envFilename)) { $this->writeln("<fg=red>'env.' file does not exists, unable to sek encryption key.</fg=red>"); } $files->write($envFilename, str_replace($config->getKey(), Strings::random(32), $files->read($envFilename))); $this->writeln("<info>Encryption key has been successfully updated.</info>"); }
/** * {@inheritdoc} */ public function isCompiled() { if (!$this->config['cache']['enabled']) { return false; } if (!$this->files->exists($viewFilename = $this->compiledFilename())) { return false; } return $this->files->time($viewFilename) >= $this->files->time($this->filename); }
/** * Internal method to fetch filename using multiple input formats. * * @param mixed|UploadedFileInterface $filename * @param bool $onlyUploaded Check if file uploaded. * @return string|bool */ protected function filename($filename, $onlyUploaded = true) { if (empty($filename) || $onlyUploaded && !$this->isUploaded($filename)) { return false; } if ($filename instanceof UploadedFileInterface || $filename instanceof StreamableInterface) { return StreamWrapper::getUri($filename->getStream()); } if (is_array($filename)) { $filename = $filename['tmp_name']; } return $this->files->exists($filename) ? $filename : false; }
/** * @param DirectoriesInterface $directories * @param FilesInterface $files */ protected function ensurePermissions(DirectoriesInterface $directories, FilesInterface $files) { $this->writeln("<info>Verifying runtime directory existence and file permissions...</info>"); $runtime = $directories->directory('runtime'); if (!$files->exists(directory('runtime'))) { $files->ensureDirectory(directory('runtime')); $this->writeln("Runtime data directory was created."); return; } foreach ($files->getFiles(directory('runtime')) as $filename) { //Both file and it's directory must be writable $files->setPermissions($filename, FilesInterface::RUNTIME); $files->setPermissions(dirname($filename), FilesInterface::RUNTIME); } $this->writeln("Runtime directory permissions were updated."); }
/** * @param FilesInterface $files * @param DirectoriesInterface $directories */ public function perform(FilesInterface $files, DirectoriesInterface $directories) { $cacheDirectory = $directories->directory('cache'); if (!$files->exists($cacheDirectory)) { $this->writeln("Cache directory is missing, no cache to be cleaned."); return; } $this->isVerbosity() && $this->writeln("<info>Cleaning application runtime cache:</info>"); foreach ($files->getFiles($cacheDirectory) as $filename) { $files->delete($filename); if ($this->isVerbosity()) { $this->writeln($files->relativePath($filename, $cacheDirectory)); } } $this->writeln("<info>Runtime cache has been cleared.</info>"); }
/** * Cast stream associated with origin data. * * @param string|StreamInterface $source * @return StreamInterface */ protected function castStream($source) { if ($source instanceof UploadedFileInterface || $source instanceof StreamableInterface) { $source = $source->getStream(); } if ($source instanceof StreamInterface) { return $source; } if (empty($source)) { //This code is going to use additional abstraction layer to connect storage and guzzle return \GuzzleHttp\Psr7\stream_for(''); } if (is_string($source) && $this->files->exists($source)) { $source = fopen($source, 'rb'); } return \GuzzleHttp\Psr7\stream_for($source); }
/** * Find view file specified by namespace and view name and associated engine id. * * @param string $namespace * @param string $view * @param string $engine Found engine id, reference. * @return string * @throws ViewException */ public function getFilename($namespace, $view, &$engine = null) { if (!isset($this->namespaces[$namespace])) { throw new ViewException("Undefined view namespace '{$namespace}'."); } //This part better be cached one dat foreach ($this->namespaces[$namespace] as $directory) { foreach ($this->config['engines'] as $engine => $options) { foreach ($options['extensions'] as $extension) { $candidate = $directory . FilesInterface::SEPARATOR . $view . '.' . $extension; if ($this->files->exists($candidate)) { return $this->files->normalizePath($candidate); } } } } throw new ViewException("Unable to find view '{$view}' in namespace '{$namespace}'."); }
/** * Locate view filename based on current loader settings. * * @param string $path * @return string * @throws LoaderException */ protected function findView($path) { if (isset($this->cache[$path])) { return $this->cache[$path][0]; } $this->validateName($path); list($namespace, $filename) = $this->parsePath($path); if (!isset($this->namespaces[$namespace])) { throw new LoaderException("Undefined view namespace '{$namespace}'."); } foreach ($this->namespaces[$namespace] as $directory) { if ($this->files->exists($directory . $filename)) { $this->cache[$path] = [$directory . $filename, $namespace, $this->resolveName($filename)]; return $this->cache[$path][0]; } } throw new LoaderException("Unable to locate view '{$filename}' in namespace '{$namespace}'."); }
/** * @param ViewsConfig $config * @param FilesInterface $files */ public function perform(ViewsConfig $config, FilesInterface $files) { if (!$files->exists($config->cacheDirectory())) { $this->writeln("Cache directory is missing, no cache to be cleaned."); return; } $this->isVerbosity() && $this->writeln("<info>Clearing view cache:</info>"); $cachedViews = $files->getFiles($config->cacheDirectory()); foreach ($cachedViews as $filename) { $files->delete($filename); if ($this->isVerbosity()) { $this->writeln($files->relativePath($filename, $config->cacheDirectory())); } } if (empty($filename) && $this->isVerbosity()) { $this->writeln("No cached views were found."); } $this->writeln("<info>View cache has been cleared.</info>"); }
/** * Get all memory sections belongs to given memory location (default location to be used if * none specified). * * @param string $location * @return array */ public function getSections($location = null) { if (!empty($location)) { $location = $this->directory . $location . '/'; } else { $location = $this->directory; } if (!$this->files->exists($location)) { return []; } $finder = new Finder(); $finder->in($location); /** * @var SplFileInfo $file */ $sections = []; foreach ($finder->name("*" . static::EXTENSION) as $file) { $sections[] = substr($file->getRelativePathname(), 0, -1 * strlen(static::EXTENSION)); } return $sections; }
/** * Check if resource is local. * * @param string $uri * @return bool */ protected function isLocal($uri) { return $this->files->exists($this->directories->directory('public') . $uri); }
/** * {@inheritdoc} */ public function hasLocale($locale) { $locale = preg_replace("/[^a-zA-Z_]/", '', mb_strtolower($locale)); return $this->files->exists($this->config->localeDirectory($locale)); }
/** * {@inheritdoc} */ public function read($session_id) { return $this->files->exists($this->getFilename($session_id)) ? $this->files->read($this->getFilename($session_id)) : false; }
/** * Export configuration data to final directory (application configs directory by default). If * configuration file already exists it's content will be merged with new configuration data * using merge method. * * @param string $directory Destination config directory, application directory by default. * @param int $mode File mode, use FilesInterface::RUNTIME for publicly accessible * files. * @return bool */ public function writeConfig($directory = null, $mode = FilesInterface::READONLY) { $directory = !empty($directory) ? $directory : $this->core->directory('config'); //Target configuration file $filename = $this->configFilename($directory, $this->name); $original = []; if ($this->files->exists($filename)) { $original = (require $filename); //We are going to use original config header $this->readHeader($filename); } return $this->files->write($filename, $this->renderConfig($original), $mode, true); }