/**
  * {@inheritdoc}
  *
  * @param Isolator $isolator Custom PHP isolator instance.
  */
 public function process($source, Isolator $isolator = null)
 {
     $isolator = !empty($isolator) ? $isolator : new Isolator();
     //Real php source code isolation
     $source = $isolator->isolatePHP($source);
     //Restoring only evaluator blocks
     $phpBlocks = $evaluateBlocks = [];
     foreach ($isolator->getBlocks() as $id => $phpBlock) {
         foreach ($this->options['flags'] as $flag) {
             if (strpos($phpBlock, $flag) !== false) {
                 $evaluateBlocks[$id] = $phpBlock;
                 continue 2;
             }
         }
         $phpBlocks[$id] = $phpBlock;
     }
     $source = $isolator->setBlocks($evaluateBlocks)->repairPHP($source);
     $isolator->setBlocks($phpBlocks);
     //Required to prevent collisions
     $filename = $this->views->config()['cache']['directory'] . "/{$this->uniqueID()}.php";
     try {
         $this->files->write($filename, $source, FilesInterface::RUNTIME, true);
         ob_start();
         include_once $filename;
         $source = ob_get_clean();
         $this->files->delete($filename);
     } catch (\ErrorException $exception) {
         throw $exception;
     }
     return $isolator->repairPHP($source);
 }
Exemple #2
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);
         }
     }
 }
Exemple #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);
     }
 }
Exemple #4
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);
 }
Exemple #5
0
 /**
  * Clean old snapshots.
  *
  * @todo Possibly need better implementation.
  * @param array $snapshots
  */
 protected function performRotation(array $snapshots)
 {
     $oldest = '';
     $oldestTimestamp = PHP_INT_MAX;
     foreach ($snapshots as $snapshot) {
         $snapshotTimestamp = $this->files->time($snapshot);
         if ($snapshotTimestamp < $oldestTimestamp) {
             $oldestTimestamp = $snapshotTimestamp;
             $oldest = $snapshot;
         }
     }
     $this->files->delete($oldest);
 }
Exemple #6
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>");
 }
Exemple #7
0
 /**
  * {@inheritdoc}
  *
  * @param Isolator|null $isolator
  */
 public function process($source, $namespace, $view, $cachedFilename = null, Isolator $isolator = null)
 {
     $isolator = !empty($isolator) ? $isolator : new Isolator();
     //Let's hide original php blocks
     $source = $isolator->isolatePHP($source);
     //Now we have to detect blocks which has to be compiled
     //Restoring only evaluator blocks
     $phpBlocks = $evaluateBlocks = [];
     foreach ($isolator->getBlocks() as $id => $phpBlock) {
         if ($this->evaluatable($phpBlock)) {
             $evaluateBlocks[$id] = $phpBlock;
             continue;
         }
         $phpBlocks[$id] = $phpBlock;
     }
     //Let's only mount blocks to be evaluated
     $source = $isolator->setBlocks($evaluateBlocks)->repairPHP($source);
     $isolator->setBlocks($phpBlocks);
     //Let's create temporary filename
     $filename = $this->evalFilename($cachedFilename);
     //I must validate file syntax in a future
     try {
         $this->files->write($filename, $source, FilesInterface::RUNTIME, true);
         ob_start();
         $__outputLevel__ = ob_get_level();
         //Can be accessed in evaluated php
         $this->namespace = $namespace;
         $this->view = $view;
         try {
             include_once $this->files->localUri($filename);
         } finally {
             while (ob_get_level() > $__outputLevel__) {
                 ob_end_clean();
             }
         }
         $this->namespace = '';
         $this->view = '';
         $source = ob_get_clean();
         //Dropping temporary filename
         $this->files->delete($filename);
     } catch (\ErrorException $exception) {
         throw $exception;
     }
     //Restoring original php blocks
     return $isolator->repairPHP($source);
 }
Exemple #8
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>");
 }
 /**
  * {@inheritdoc}
  */
 public function flush()
 {
     foreach ($this->files->getFiles($this->directory, $this->extension) as $filename) {
         $this->files->delete($filename);
     }
 }