fileManager = $fileManager; $this->config = $config; $this->metadata = $metadata; } protected function getFileManager() { return $this->fileManager; } protected function getConfig() { return $this->config; } protected function getMetadata() { return $this->metadata; } /** * Return paths to class files. * * @param string | array $paths in format [ * 'corePath' => '', * 'modulePath' => '', * 'customPath' => '', * ] * @param $cacheFile Full path for a cache file, ex. data/cache/application/entryPoints.php. * @param $allowedMethods If specified, classes w/o specified method will be ignored. */ public function getData($paths, ?string $cacheFile = null, ?array $allowedMethods = null, bool $subDirs = false) : array { $data = null; if (is_string($paths)) { $paths = [ 'corePath' => $paths, ]; } if ($cacheFile && file_exists($cacheFile) && $this->getConfig()->get('useCache')) { $data = $this->getFileManager()->getPhpContents($cacheFile); if (!is_array($data)) { $GLOBALS['log']->error("ClassParser: Non-array value stored in {$cacheFile}."); } } if (!is_array($data)) { $data = $this->getClassNameHash($paths['corePath'], $allowedMethods, $subDirs); if (isset($paths['modulePath'])) { foreach ($this->getMetadata()->getModuleList() as $moduleName) { $path = str_replace('{*}', $moduleName, $paths['modulePath']); $data = array_merge($data, $this->getClassNameHash($path, $allowedMethods, $subDirs)); } } if (isset($paths['customPath'])) { $data = array_merge($data, $this->getClassNameHash($paths['customPath'], $allowedMethods, $subDirs)); } if ($cacheFile && $this->getConfig()->get('useCache')) { $result = $this->getFileManager()->putPhpContents($cacheFile, $data); if ($result == false) { throw new Error("ClassParser: Could not save file {$cacheFile}."); } } } return $data; } protected function getClassNameHash($dirs, ?array $allowedMethods = [], bool $subDirs = false) { if (is_string($dirs)) { $dirs = (array) $dirs; } $data = []; foreach ($dirs as $dir) { if (file_exists($dir)) { $fileList = $this->getFileManager()->getFileList($dir, $subDirs, '\.php$', true); $this->fillHashFromFileList($fileList, $dir, $allowedMethods, $data); } } return $data; } protected function fillHashFromFileList( array $fileList, string $dir, ?array $allowedMethods, array &$data, string $category = '' ) { foreach ($fileList as $key => $file) { if (is_string($key)) { if (is_array($file)) { $this->fillHashFromFileList($file, $dir . '/'. $key, $allowedMethods, $data, $category . $key . '\\'); } continue; } $filePath = Util::concatPath($dir, $file); $className = Util::getClassName($filePath); $fileName = $this->getFileManager()->getFileName($filePath); $name = Util::normilizeScopeName(ucfirst($fileName)); $name = $category . $name; if (empty($allowedMethods)) { $data[$name] = $className; continue; } foreach ($allowedMethods as $methodName) { if (method_exists($className, $methodName)) { $data[$name] = $className; } } } } }