Example #1
0
 /**
  * Compile one source file using compilers
  *
  * @param $source        Reflection_Source
  * @param $compilers     ICompiler[]
  * @param $cache_dir     string
  * @param $first_group   boolean
  * @param $sources_count integer
  * @return mixed
  */
 private function compileSource(Reflection_Source $source, $compilers, $cache_dir, $first_group, $sources_count)
 {
     foreach ($compilers as $compiler) {
         $compiler->compile($source, $this);
     }
     $file_name = substr($source->file_name, 0, strlen($cache_dir)) === $cache_dir ? $source->file_name : $this->getCacheDir() . SL . str_replace(SL, '-', substr($source->file_name, 0, -4));
     if ($source->hasChanged()) {
         /** @var Reflection_Source $source inspector bug */
         /** @noinspection PhpParamsInspection inspector bug (a Dependency is an object) */
         (new Set())->replace($source->getDependencies(true), Dependency::class, ['file_name' => $source->file_name]);
         script_put_contents($file_name, $source->getSource());
     } elseif (file_exists($file_name) && $first_group) {
         unlink($file_name);
     }
     if ($sources_count > self::MAX_OPENED_SOURCES) {
         $source->free(self::SOURCES_FREE);
     }
 }
Example #2
0
 /**
  * Scans all PHP files into the project (excluding vendor) and store their paths to the cache
  */
 public function update($last_time = 0)
 {
     $files = Application::current()->include_path->getSourceFiles();
     $this->full_class_names = [];
     $this->paths = [];
     foreach ($files as $file_path) {
         if (substr($file_path, -4) == '.php') {
             $buffer = file_get_contents($file_path);
             $short_class = trim(mParse($buffer, LF . 'class' . SP, LF)) ?: trim(mParse($buffer, LF . 'final class' . SP, LF)) ?: trim(mParse($buffer, LF . 'abstract class' . SP, LF)) ?: trim(mParse($buffer, LF . 'final abstract class' . SP, LF));
             if ($short_class) {
                 $type = 'class';
             } else {
                 $short_class = trim(mParse($buffer, LF . 'interface' . SP, LF));
                 if ($short_class) {
                     $type = 'interface';
                 } else {
                     $short_class = trim(mParse($buffer, LF . 'trait' . SP, LF));
                     if ($short_class) {
                         $type = 'trait';
                     }
                 }
             }
             if ($short_class && isset($type)) {
                 if ($i = strpos($short_class, SP)) {
                     $short_class = substr($short_class, 0, $i);
                 }
                 $namespace = trim(mParse($buffer, LF . 'namespace' . SP, LF));
                 if (substr($namespace, -1) == ';') {
                     $namespace = trim(substr($namespace, 0, -1));
                 }
                 $full_class = $namespace . BS . $short_class;
                 if ($type == 'class' && !isset($this->full_class_names[$short_class])) {
                     $this->full_class_names[$short_class] = $full_class;
                 }
                 if (!isset($this->paths[$full_class])) {
                     $this->paths[$full_class] = $file_path;
                 }
             }
         }
     }
     if (!is_dir($this->cache_path)) {
         mkdir($this->cache_path);
     }
     script_put_contents($this->cache_path . SL . 'autoload.php', '<?php' . LF . LF . '$this->full_class_names = ' . var_export($this->full_class_names, true) . ';' . LF . LF . '$this->paths = ' . var_export($this->paths, true) . ';' . LF);
 }