/** * Load classmap at construct * * @param ClassIterator $classIterator * @param boolean $register True if loader should be register at creation */ public function __construct(ClassIterator $classIterator, $register = true) { $this->classMap = $classIterator->getClassMap(); if ($register) { $this->register(); } }
/** * Access the class map * * `getClassMap()` returns a map of class names to * [SplFileInfo](http://api.symfony.com/2.5/Symfony/Component/Finder/SplFileInfo.html) * objects. * * @expectOutputRegex /php$/ */ public function exampleGetClassMap() { $finder = new Finder(); $iter = new ClassIterator($finder->in('src')); // Print the file names of classes, interfaces and traits in 'src' foreach ($iter->getClassMap() as $classname => $splFileInfo) { echo $classname . ': ' . $splFileInfo->getRealPath(); } }
/** * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function getExistingClasses(array $paths) : array { $namespaceList = []; $classList = []; $list = []; foreach ($paths as $path) { if (false === $path->isSourceCode()) { continue; } $iter = new ClassIterator($this->getPhpFiles($path->getPathBase())); foreach (array_keys($iter->getClassMap()) as $classname) { if (in_array(substr($classname, -4), ['Test', 'Spec'])) { continue; } $reflection = new \ReflectionClass($classname); if ($reflection->isAbstract() || $reflection->isTrait() || $reflection->isInterface()) { continue; } if ($reflection->inNamespace()) { $namespaces = explode('\\', $reflection->getNamespaceName()); $namespace = ''; foreach ($namespaces as $key => $namespacePart) { if ($namespace !== '') { $namespace .= '/'; } $namespace .= $namespacePart; if (!array_key_exists($key, $namespaceList)) { $namespaceList[$key] = []; } $namespaceList[$key][$namespace] = $namespace; } } $escapedName = str_replace('\\', '/', $classname); $classList[$escapedName] = $escapedName; } } foreach ($namespaceList as $namespaceDepthList) { $list = array_merge($list, $namespaceDepthList); } $list = array_merge($list, $classList); return $list; }
/** * Simple enumerated output * * @param OutputInterface $output * @param ClassIterator $classIterator * @return null */ public function output(OutputInterface $output, ClassIterator $classIterator) { foreach ($classIterator->getClassMap() as $className => $file) { $output->writeln("{$file} => {$className}"); } }