/** * Метод получает экземпляр класса и, если нужно, кеширует его. */ public static function getClassInstance($__DIR__, $subDir, $className, $parent, $caching = true) { if (!is_valid_file_name($className)) { return null; //--- } $className = get_file_name($className); if ($className == $parent) { //Абстрактный класс/интерфейс лежит рядом с классами реализации - пропустим его return null; } //Абсолютный путь к классу $classPath = file_path(array($__DIR__, $subDir), $className, PsConst::EXT_PHP); //Ключ кеширования $cacheKey = md5($classPath); $CACHE = $caching ? SimpleDataCache::inst(__CLASS__, __FUNCTION__) : null; if ($CACHE && $CACHE->has($cacheKey)) { return $CACHE->get($cacheKey); } $INST = null; if (is_file($classPath)) { //Подключим данный класс require_once $classPath; //Проверим, существует ли класс $rc = PsUtil::newReflectionClass($className, false); $INST = $rc && $rc->isSubclassOf($parent) ? $rc->newInstance() : null; } if ($CACHE && $INST) { $CACHE->set($cacheKey, $INST); } return $INST; }
/** * Метод рекурсивно собирает все классы в директории. * * @param string $dirAbsPath - путь к директории * @param array $classes - карта [PsUtil] => [C:/www/postupayu.ru/www/kitcore/utils/PsUtil.php] * @param bool $skipDirClasses - пропускать ли классы в корневой директории. * Флаг позволит не подключать классы, лежащие в корне kitcore, * так как их мы подключим сами (Globals, Defines, PsCoreIncluder) */ public static function loadClassPath($dirAbsPath, array &$classes, $skipDirClasses) { if (!is_dir($dirAbsPath)) { return; //--- } $dir = openDir($dirAbsPath); while ($file = readdir($dir)) { if (!is_valid_file_name($file)) { continue; } $isphp = ends_with($file, '.php'); if ($isphp && $skipDirClasses) { continue; } $path = next_level_dir($dirAbsPath, $file); if ($isphp) { $classes[cut_string_end($file, '.php')] = $path; } else { self::loadClassPath($path, $classes, false); } } closedir($dir); }
/** * Получает названия папок, вложенных в переданную директорию * * @param array $allowed - список допустимых названий подпапок */ public final function getSubDirNames($dirs = null, $allowed = null, $denied = null) { $result = array(); $allowed = $allowed === null ? null : to_array($allowed); if (is_array($allowed) && empty($allowed)) { return $result; } $denied = $denied === null ? null : to_array($denied); $absDirPath = $this->absDirPath($dirs); if (is_dir($absDirPath)) { $dir = openDir($absDirPath); if ($dir) { $absDirPath = ensure_dir_endswith_dir_separator($absDirPath); while ($file = readdir($dir)) { if (!is_valid_file_name($file)) { continue; } if (is_array($allowed) && !in_array($file, $allowed)) { continue; } if (is_array($denied) && in_array($file, $denied)) { continue; } if (is_dir($absDirPath . $file)) { $result[] = $file; } } closedir($dir); } } return $result; }