Example #1
0
 /**
  * {@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);
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function export($filename)
 {
     if (empty($this->language)) {
         throw new ExporterException("No language specified to be exported.");
     }
     $this->files->write($filename, $this->compile());
 }
Example #3
0
 /**
  * {@inheritdoc}
  */
 public function saveData($section, $data, $location = null)
 {
     $filename = $this->memoryFilename($section, $location);
     //We are packing data into plain php
     $data = '<?php return ' . var_export($data, true) . ';';
     //We need help to write file with directory creation
     $this->files->write($filename, $data, FilesInterface::RUNTIME, true);
 }
Example #4
0
 /**
  * Export UML classes diagram to specified file, all found Documents with their fields, methods
  * and compositions will be used to generate such UML.
  *
  * @param string $filename
  * @return bool
  */
 public function export($filename)
 {
     $this->line('@startuml');
     foreach ($this->builder->getDocuments() as $document) {
         $this->renderDocument($document);
     }
     $this->line('@enduml');
     return $this->files->write($filename, join("\n", $this->lines));
 }
Example #5
0
 /**
  * Validate and save all altered configuration files.
  */
 public function save()
 {
     foreach ($this->injectors as $config => $injector) {
         if (!$injector->checkSyntax()) {
             throw new RegistratorException("Config syntax of '{$config}' does not valid after registrations.");
         } else {
             $this->logger()->debug("Syntax of config '{$config}' has been checked.");
         }
         //Saving to file
         $this->files->write($this->configFilename($config), $injector->render());
         $this->logger()->info("Config '{$config}' were updated with new content.");
     }
 }
Example #6
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 #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);
 }
Example #8
0
 /**
  * @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>");
 }
Example #9
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 #10
0
 /**
  * {@inheritdoc}
  */
 public function compile()
 {
     $source = $this->getSource();
     foreach ($this->getProcessors() as $processor) {
         $reflection = new \ReflectionClass($processor);
         $context = $this->namespace . ViewProviderInterface::NS_SEPARATOR . $this->view;
         $benchmark = $this->benchmark($reflection->getShortName(), $context);
         try {
             $source = $processor->process($source);
         } finally {
             $this->benchmark($benchmark);
         }
     }
     $this->files->write($this->compiledFilename(), $source, FilesInterface::RUNTIME, true);
 }
Example #11
0
 /**
  * {@inheritdoc}
  */
 public function registerMigration($name, $class)
 {
     if (!class_exists($class)) {
         throw new MigratorException("Unable to register migration, representing class does not exists.");
     }
     foreach ($this->getMigrations() as $migration) {
         if (get_class($migration) == $class) {
             //Already presented
             return false;
         }
     }
     //Copying
     $this->files->write($filename = $this->createFilename($name), $this->files->read((new \ReflectionClass($class))->getFileName()), FilesInterface::READONLY, true);
     return basename($filename);
 }
Example #12
0
 /**
  * {@inheritdoc}
  */
 public function registerMigration($name, $class, $body = null)
 {
     if (empty($body) && !class_exists($class)) {
         throw new RepositoryException("Unable to register migration '{$class}', representing class does not exists");
     }
     foreach ($this->getMigrations() as $migration) {
         if (get_class($migration) == $class) {
             throw new RepositoryException("Unable to register migration '{$class}', migration already exists");
         }
         if ($migration->getMeta()->getName() == $name) {
             throw new RepositoryException("Unable to register migration '{$name}', migration under same name already exists");
         }
     }
     if (empty($body)) {
         //Let's read body from a given class filename
         $body = $this->files->read((new \ReflectionClass($class))->getFileName());
     }
     //Copying
     $this->files->write($filename = $this->createFilename($name), $body, FilesInterface::READONLY);
     return basename($filename);
 }
Example #13
0
 /**
  * @param FilesInterface $files
  * @param ODM            $odm
  */
 public function perform(FilesInterface $files, ODM $odm)
 {
     $umlExporter = new UmlExporter($odm->schemaBuilder());
     $files->write($this->argument('filename'), $umlExporter->generate());
     $this->writeln("<info>UML schema has been  successfully exported:</info> {$this->argument('filename')}");
 }
Example #14
0
 /**
  * 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);
 }
Example #15
0
 /**
  * {@inheritdoc}
  */
 public function write($key, $content)
 {
     $this->files->write($key, $content, FilesInterface::RUNTIME, true);
 }
Example #16
0
 /**
  * Render file declaration into file.
  *
  * @param string          $filename
  * @param int             $mode
  * @param bool            $ensureLocation
  * @param ArraySerializer $exporter Class used to render array values for default properties and etc.
  * @return bool
  */
 public function renderTo($filename, $mode = FilesInterface::RUNTIME, $ensureLocation = false, ArraySerializer $exporter = null)
 {
     return $this->files->write($filename, $this->render(0, $exporter), $mode, $ensureLocation);
 }
Example #17
0
 /**
  * {@inheritdoc}
  */
 public function forever($name, $data)
 {
     return $this->files->write($this->makeFilename($name), serialize([0, $data]));
 }
Example #18
0
 /**
  * {@inheritdoc}
  */
 public function write($session_id, $session_data)
 {
     return $this->files->write($this->getFilename($session_id), $session_data, FilesInterface::RUNTIME, true);
 }