예제 #1
0
 /**
  * @param $last_time integer compile only files modified since this time
  */
 public function compile($last_time = 0)
 {
     set_time_limit(900);
     clearstatcache();
     $cache_dir = $this->getCacheDir();
     // create data set for dependencies, check for dependencies for deleted files
     Dao::createStorage(Dependency::class);
     Dao::begin();
     if (isset($_GET['Z'])) {
         $link = Dao::current();
         if ($link instanceof Link) {
             $link->query('TRUNCATE TABLE `dependencies`');
         }
     }
     foreach (Dao::select(Dependency::class, ['file_name' => Func::distinct()]) as $file_dependency) {
         /** @var $file_dependency List_Row */
         $file_name = $file_dependency->getValue('file_name');
         if (!file_exists($file_name)) {
             foreach (Dao::search(['file_name' => $file_name], Dependency::class) as $dependency) {
                 /** @var $dependency Dependency */
                 Dao::delete($dependency);
                 foreach (Dao::search(['dependency_name' => $dependency->class_name], Dependency::class) as $sub_dependency) {
                     /** @var $sub_dependency Dependency */
                     Dao::delete($sub_dependency);
                 }
             }
         }
     }
     Dao::commit();
     $this->sources = array_merge($this->more_sources, $this->getFilesToCompile($last_time));
     $first_group = true;
     foreach ($this->compilers as $compilers) {
         /** @var $compilers ICompiler[] */
         $saved_sources = $this->sources;
         while ($this->sources) {
             // get source and update dependencies
             foreach ($this->sources as $source) {
                 /** @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]);
             }
             do {
                 $added = [];
                 // ask each compiler for adding of compiled files, until they have nothing to add
                 foreach ($compilers as $compiler) {
                     if ($compiler instanceof Needs_Main) {
                         $compiler->setMainController($this->main_controller);
                     }
                     $added = array_merge($added, $compiler->moreSourcesToCompile($this->sources));
                 }
                 foreach ($added as $source) {
                     /** @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]);
                     $this->sources[$source->file_name] = $source;
                 }
                 if (count($compilers) == 1) {
                     $added = [];
                 }
             } while ($added);
             $saved_sources = array_merge($saved_sources, $this->sources);
             // fill in sources cache
             $sources_count = count($this->sources);
             foreach ($this->sources as $source) {
                 foreach (array_keys($source->getClasses()) as $class_name) {
                     $this->sources_cache[$class_name] = $source;
                 }
                 if ($sources_count > self::MAX_OPENED_SOURCES) {
                     $source->free(self::SOURCES_FREE);
                 }
             }
             // compile sources
             foreach ($this->sources as $source) {
                 $this->compileSource($source, $compilers, $cache_dir, $first_group, $sources_count);
             }
             $this->sources = $this->more_sources;
             $this->more_sources = [];
             foreach ($this->sources as $source) {
                 if (!isset($saved_sources[$source->file_name])) {
                     $saved_sources[$source->file_name] = $source;
                 }
             }
         }
         $this->sources = $saved_sources;
         $first_group = false;
     }
     $this->sources = null;
 }