Example #1
0
 /**
  * {@inheritdoc}
  */
 public function gc($maxlifetime)
 {
     foreach ($this->files->getFiles($this->location) as $filename) {
         if ($this->files->time($filename) < time() - $maxlifetime) {
             $this->files->delete($filename);
         }
     }
 }
Example #2
0
 /**
  * Index directory files to locate function calls related to Translator or TranslatorTrait
  * methods.
  *
  * @param string $directory
  * @param array  $excludes Filename words to be excluded from analysis.
  * @return self
  * @throws TokenizerException
  * @throws ReflectionException
  * @throws TranslatorException
  */
 public function indexDirectory($directory = null, array $excludes = [])
 {
     foreach ($this->files->getFiles($directory, 'php') as $filename) {
         foreach ($excludes as $exclude) {
             if (strpos($filename, $exclude) !== false) {
                 continue 2;
             }
         }
         $this->indexCalls($this->tokenizer->fileReflection($filename)->getCalls());
     }
     return $this;
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function report()
 {
     $this->debugger->logger()->error($this->getMessage());
     if (!$this->config['reporting']['enabled']) {
         //No need to record anything
         return;
     }
     //Snapshot filename
     $filename = \Spiral\interpolate($this->config['reporting']['filename'], ['date' => date($this->config['reporting']['dateFormat'], time()), 'exception' => $this->getName()]);
     //Writing to hard drive
     $this->files->write($this->config['reporting']['directory'] . '/' . $filename, $this->render(), FilesInterface::RUNTIME, true);
     $snapshots = $this->files->getFiles($this->config['reporting']['directory']);
     if (count($snapshots) > $this->config['reporting']['maxSnapshots']) {
         $oldestSnapshot = '';
         $oldestTimestamp = PHP_INT_MAX;
         foreach ($snapshots as $snapshot) {
             $snapshotTimestamp = $this->files->time($snapshot);
             if ($snapshotTimestamp < $oldestTimestamp) {
                 $oldestTimestamp = $snapshotTimestamp;
                 $oldestSnapshot = $snapshot;
             }
         }
         $this->files->delete($oldestSnapshot);
     }
 }
Example #4
0
 /**
  * Get list of view names associated with their View class.
  *
  * @param string $namespace
  * @return array
  * @throws ViewException
  */
 public function getViews($namespace)
 {
     if (!isset($this->namespaces[$namespace])) {
         throw new ViewException("Invalid view namespace '{$namespace}'.");
     }
     $result = [];
     foreach ($this->namespaces[$namespace] as $location) {
         $location = $this->files->normalizePath($location);
         foreach ($this->files->getFiles($location) as $filename) {
             $foundEngine = false;
             foreach ($this->config['engines'] as $engine => $options) {
                 if (in_array($this->files->extension($filename), $options['extensions'])) {
                     $foundEngine = $engine;
                     break;
                 }
             }
             if (empty($foundEngine)) {
                 //No engines found = not view
                 continue;
             }
             //View filename without extension
             $filename = substr($filename, 0, -1 - strlen($this->files->extension($filename)));
             $name = substr($filename, strlen($location) + strlen(FilesInterface::SEPARATOR));
             $result[$name] = $this->viewClass($foundEngine, $namespace, $name);
         }
     }
     return $result;
 }
Example #5
0
 /**
  * Internal method to fetch all migration filenames.
  */
 private function getFiles()
 {
     foreach ($this->files->getFiles($this->config['directory'], '*.php') as $filename) {
         $reflection = $this->tokenizer->fileReflection($filename);
         $definition = explode('_', basename($filename));
         (yield ['filename' => $filename, 'class' => $reflection->getClasses()[0], 'created' => \DateTime::createFromFormat(self::TIMESTAMP_FORMAT, $definition[0]), 'name' => str_replace('.php', '', join('_', array_slice($definition, 2)))]);
     }
 }
Example #6
0
 /**
  * {@inheritdoc}
  */
 public function getSize()
 {
     $totalSize = 0;
     foreach ($this->files->getFiles($this->getLocation()) as $filename) {
         $totalSize += filesize($filename);
     }
     return $totalSize;
 }
Example #7
0
 /**
  * {@inheritdoc}
  */
 public function flush()
 {
     $files = $this->files->getFiles($this->options['directory'], $this->options['extension']);
     foreach ($files as $filename) {
         $this->files->delete($filename);
     }
     return count($files);
 }
Example #8
0
 /**
  * Save snapshot information on hard-drive.
  */
 protected function saveSnapshot()
 {
     $filename = $this->config->snapshotFilename($this->getException(), time());
     $this->files->write($filename, $this->render(), FilesInterface::RUNTIME, true);
     $snapshots = $this->files->getFiles($this->config->reportingDirectory());
     if (count($snapshots) > $this->config->maxSnapshots()) {
         $this->performRotation($snapshots);
     }
 }
Example #9
0
 /**
  * Internal method to fetch all migration filenames.
  *
  * @return array
  */
 private function getFiles()
 {
     $filenames = [];
     foreach ($this->files->getFiles($this->config['directory'], 'php') as $filename) {
         $reflection = new ReflectionFile($this->tokenizer, $filename);
         $definition = explode('_', basename($filename));
         $filenames[$filename] = ['class' => $reflection->getClasses()[0], 'created' => \DateTime::createFromFormat(self::TIMESTAMP_FORMAT, $definition[0] . '_' . $definition[1]), 'name' => str_replace('.php', '', join('_', array_slice($definition, 3)))];
     }
     return $filenames;
 }
Example #10
0
 /**
  * Load all bundle strings from specified language.
  *
  * @param string $language
  * @param string $prefix Only bundle names started with this prefix will be exported.
  * @return array
  */
 private function loadBundles($language, $prefix = '')
 {
     $bundles = $this->files->getFiles($this->translator->config()['languages'][$language]['directory']);
     $result = [];
     foreach ($bundles as $filename) {
         $bundle = substr(basename($filename), 0, -1 * strlen($this->files->extension($filename)));
         if (!empty($prefix) && stripos($bundle, $prefix) !== 0) {
             continue;
         }
         try {
             $result[$bundle] = (include $filename);
         } catch (\Exception $exception) {
         }
     }
     return $result;
 }
Example #11
0
 /**
  * @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>");
 }
Example #12
0
 /**
  * @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.");
 }
Example #13
0
 /**
  * @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>");
 }
Example #14
0
 /**
  * {@inheritdoc}
  */
 public function flush()
 {
     foreach ($this->files->getFiles($this->directory, $this->extension) as $filename) {
         $this->files->delete($filename);
     }
 }