コード例 #1
0
ファイル: wplib.php プロジェクト: r-a-stone/wplib
 /**
  * Scan registered autoload files, by priority
  *
  * This will get called 4 times.
  *
  *      1 & 2: Find all autoloading files from components that have been loaded by (1) plugins or (2) the theme.
  *      3 & 4: Find all autoloading files defined by modules specified by (1) plugins or (2) the theme.
  */
 private static function _find_autoload_files()
 {
     static $class_count = 0;
     $classes = static::component_classes();
     $latest_classes = array_slice($classes, $class_count);
     if (count($latest_classes)) {
         $class_count += count($latest_classes);
         $class_key = implode('|', $latest_classes);
         $class_key = WPLib::is_production() ? md5($class_key) : $class_key;
         if (!(self::$_new_files = static::cache_get($cache_key = "autoload_files[{$class_key}]"))) {
             $autoload_files = array();
             /**
              * These were the files that were manually added
              */
             $added_files = array_values(self::$_autoload_files);
             /**
              * For each Site/App/Module/Lib/Theme class
              */
             foreach ($latest_classes as $class_name) {
                 /**
                  * Scan the includes directory for all files.
                  */
                 $found_files = glob($autoload_dir = static::get_root_dir('includes', $class_name) . '/*.php');
                 if (0 == count($found_files)) {
                     continue;
                 }
                 /**
                  * Find out what classes are currently defined.
                  */
                 $declared_classes = get_declared_classes();
                 /**
                  * Diff the manually added files with the new ones scanned to get new files.
                  */
                 self::$_new_files = array_diff($found_files, $added_files);
                 /**
                  * Load all the scanned files from the /include/ directory
                  */
                 do {
                     self::$_file_loading = array_shift(self::$_new_files);
                     require self::$_file_loading;
                     self::$_file_loading = false;
                 } while (count(self::$_new_files));
                 /**
                  * Find the newly declared classes by comparing what was declared before with what is declared now.
                  */
                 $loaded_classes = array_diff(get_declared_classes(), $declared_classes);
                 if (count($loaded_classes) > count($found_files)) {
                     $message = __('More than one class defined in \'/includes/\' directory of %s.', 'wplib');
                     static::trigger_error(sprintf($message, $class_name));
                 } else {
                     if (count($loaded_classes) < count($found_files)) {
                         $message = __('Files with no classes defined in \'/includes/\' directory of %s.', 'wplib');
                         static::trigger_error(sprintf($message, $class_name));
                     } else {
                         /**
                          * Add them in for autoloading.
                          * $loaded_classes should be in the same order as $found_files.
                          */
                         $autoload_files += array_combine($loaded_classes, $found_files);
                     }
                 }
             }
             /**
              * Now stuff into cache
              */
             static::cache_set($cache_key, $autoload_files);
         }
         if (isset($autoload_files)) {
             /**
              * Set the mustload classes based on on_load() ordered by parent/child classes.
              */
             self::_set_mustload_classes($autoload_files);
             /**
              * Add these new files to the list of files to autoload at the default priority.
              */
             self::$_autoload_files = array_merge(self::$_autoload_files, $autoload_files);
         }
     }
 }