/** * Discoverer constructor. * @param Config $config */ public function __construct(Config $config) { $paths = $config->getDiscovererPaths(); if (count($paths) == 0) { throw new \DomainException('The Config object has no discoverable paths'); } //@TODO check mandatory properties of API declaration (url, type) foreach ($paths as $path) { if (!is_dir($path)) { throw new \InvalidArgumentException(sprintf('%s is not a directory', $path)); } if (!is_readable($path)) { throw new \DomainException(sprintf('%s is not a readable directory', $path)); } } $this->config = $config; }
/** * Scan discoverable paths and get actions * * @return array */ public function mapClasses() { $paths = $this->config->getDiscovererPaths(); $files = $classMap = []; foreach ($paths as $path) { $files = array_merge($files, $this->loadDir($path)); } foreach ($files as $file) { $fileContent = file_get_contents($file); $classes = array_keys(AnnotationsParser::parsePhp($fileContent)); Config::includeFile($file); foreach ($classes as $className) { $class = new \ReflectionClass($className); if (!$class->isInstantiable()) { continue; } $classAnnotations = AnnotationsParser::getAll($class); if (!isset($classAnnotations['ExtDirect'])) { continue; } $methods = $this->getMethods($class); $classAlias = null; if (isset($classAnnotations['ExtDirect\\Alias'])) { if (is_array($classAnnotations['ExtDirect\\Alias']) && is_string($classAnnotations['ExtDirect\\Alias'][0])) { $classAlias = $classAnnotations['ExtDirect\\Alias'][0]; } } $actionName = $classAlias ?: $className; $classMap[$actionName]['action'] = $actionName; $classMap[$actionName]['class'] = $className; $classMap[$actionName]['file'] = $file; $classMap[$actionName]['methods'] = $methods; } } return $classMap; }