/** * _get_scopes * @return array */ private function _get_scopes() { static $scopes = array(); $scopes_to_register = apply_filters('FHEE__EE_Promotions_Config___get_scopes__scopes_to_register', glob(EE_PROMOTIONS_PATH . 'lib/scopes/*.lib.php')); foreach ($scopes_to_register as $scope) { $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($scope); // if parent let's skip - it's already been required. if ($class_name == 'EE_Promotion_Scope') { continue; } $loaded = (require_once $scope); // avoid instantiating classes twice by checking whether file has already been loaded // ( first load returns (int)1, subsequent loads return (bool)true ) if ($loaded === 1) { if (class_exists($class_name)) { $reflector = new ReflectionClass($class_name); $sp = $reflector->newInstance(); $scopes[$sp->slug] = $sp; } } } return $scopes; }
/** * cycles through all of the models/*.model.php files, and assembles an array of model names * * @return void */ private function _parse_model_names() { //get all the files in the EE_MODELS folder that end in .model.php $models = glob(EE_MODELS . '*.model.php'); $model_names = array(); $non_abstract_db_models = array(); foreach ($models as $model) { // get model classname $classname = EEH_File::get_classname_from_filepath_with_standard_filename($model); $short_name = str_replace('EEM_', '', $classname); $reflectionClass = new ReflectionClass($classname); if ($reflectionClass->isSubclassOf('EEM_Base') && !$reflectionClass->isAbstract()) { $non_abstract_db_models[$short_name] = $classname; } $model_names[$short_name] = $classname; } $this->registry->models = apply_filters('FHEE__EE_System__parse_model_names', $model_names); $this->registry->non_abstract_db_models = apply_filters('FHEE__EE_System__parse_implemented_model_names', $non_abstract_db_models); }
public function test_get_classname_from_filepath_with_standard_filename() { $file_path = '/we/fds/vdw/ew/EE_Thingy.junk.php'; $this->assertEquals('EE_Thingy', EEH_File::get_classname_from_filepath_with_standard_filename($file_path)); }
/** * Assumes all the files in this folder have the normal naming scheme (namely that their classname * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly. * Yes this has to scan the directory for files, but it only does it once -- not on EACH * time the autoloader is used * * @param string $folder name, with or without trailing /, doesn't matter * @param bool $recursive * @param bool $debug - set to true to display autoloader class => path mappings * @return void * @throws \EE_Error */ public static function register_autoloaders_for_each_file_in_folder($folder, $recursive = false, $debug = false) { // make sure last char is a / $folder .= $folder[strlen($folder) - 1] != DS ? DS : ''; $class_to_filepath_map = array(); $exclude = array('index'); //get all the files in that folder that end in php $filepaths = glob($folder . '*'); if (empty($filepaths)) { return; } foreach ($filepaths as $filepath) { if (substr($filepath, -4, 4) == '.php') { $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath); if (!in_array($class_name, $exclude)) { $class_to_filepath_map[$class_name] = $filepath; } } else { if ($recursive) { EEH_Autoloader::register_autoloaders_for_each_file_in_folder($filepath, $recursive); } } } // we remove the necessity to do a is_readable() check via the $read_check flag because glob by nature will not return non_readable files/directories. self::register_autoloader($class_to_filepath_map, false, $debug); }
/** * Assumes all the files in this folder have the normal naming scheme (namely that their classname * is the file's name, plus ".whatever.php".) and adds each of them to the autoloader list. * If that's not the case, you'll need to improve this function or just use EEH_File::get_classname_from_filepath_with_standard_filename() directly. * Yes this has to scan the directory for files, but it only does it once -- not on EACH * time the autoloader is used * @param string $folder name, with or without trailing /, doesn't matter * @return void */ public static function register_autoloaders_for_each_file_in_folder($folder) { // make sure last char is a / $folder .= $folder[strlen($folder) - 1] != DS ? DS : ''; $class_to_filepath_map = array(); $exclude = array('index'); //get all the files in that folder that end in php $filepaths = glob($folder . '*.php'); foreach ($filepaths as $filepath) { $class_name = EEH_File::get_classname_from_filepath_with_standard_filename($filepath); if (!in_array($class_name, $exclude)) { $class_to_filepath_map[$class_name] = str_replace(array('\\/', '/'), DS, $filepath); } } self::register_autoloader($class_to_filepath_map); }