Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Routing\Loader; Chris@0: Chris@17: use Symfony\Component\Config\Resource\DirectoryResource; Chris@0: use Symfony\Component\Routing\RouteCollection; Chris@0: Chris@0: /** Chris@0: * AnnotationDirectoryLoader loads routing information from annotations set Chris@0: * on PHP classes and methods. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class AnnotationDirectoryLoader extends AnnotationFileLoader Chris@0: { Chris@0: /** Chris@0: * Loads from annotations from a directory. Chris@0: * Chris@0: * @param string $path A directory path Chris@0: * @param string|null $type The resource type Chris@0: * Chris@0: * @return RouteCollection A RouteCollection instance Chris@0: * Chris@0: * @throws \InvalidArgumentException When the directory does not exist or its routes cannot be parsed Chris@0: */ Chris@0: public function load($path, $type = null) Chris@0: { Chris@14: if (!is_dir($dir = $this->locator->locate($path))) { Chris@14: return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection(); Chris@14: } Chris@0: Chris@0: $collection = new RouteCollection(); Chris@0: $collection->addResource(new DirectoryResource($dir, '/\.php$/')); Chris@0: $files = iterator_to_array(new \RecursiveIteratorIterator( Chris@0: new \RecursiveCallbackFilterIterator( Chris@14: new \RecursiveDirectoryIterator($dir, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS), Chris@0: function (\SplFileInfo $current) { Chris@0: return '.' !== substr($current->getBasename(), 0, 1); Chris@0: } Chris@0: ), Chris@0: \RecursiveIteratorIterator::LEAVES_ONLY Chris@0: )); Chris@0: usort($files, function (\SplFileInfo $a, \SplFileInfo $b) { Chris@0: return (string) $a > (string) $b ? 1 : -1; Chris@0: }); Chris@0: Chris@0: foreach ($files as $file) { Chris@0: if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: if ($class = $this->findClass($file)) { Chris@0: $refl = new \ReflectionClass($class); Chris@0: if ($refl->isAbstract()) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: $collection->addCollection($this->loader->load($class, $type)); Chris@0: } Chris@0: } Chris@0: Chris@0: return $collection; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function supports($resource, $type = null) Chris@0: { Chris@14: if ('annotation' === $type) { Chris@14: return true; Chris@14: } Chris@14: Chris@17: if ($type || !\is_string($resource)) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: try { Chris@14: return is_dir($this->locator->locate($resource)); Chris@0: } catch (\Exception $e) { Chris@0: return false; Chris@0: } Chris@0: } Chris@0: }