1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Symfony\Component\Routing\Loader;
13:
14: use Symfony\Component\Routing\RouteCollection;
15: use Symfony\Component\Config\Resource\DirectoryResource;
16:
17: 18: 19: 20: 21: 22:
23: class AnnotationDirectoryLoader extends AnnotationFileLoader
24: {
25: 26: 27: 28: 29: 30: 31: 32: 33: 34:
35: public function load($path, $type = null)
36: {
37: $dir = $this->locator->locate($path);
38:
39: $collection = new RouteCollection();
40: $collection->addResource(new DirectoryResource($dir, '/\.php$/'));
41: $files = iterator_to_array(new \RecursiveIteratorIterator(
42: new RecursiveCallbackFilterIterator(
43: new \RecursiveDirectoryIterator($dir),
44: function (\SplFileInfo $current) {
45: return '.' !== substr($current->getBasename(), 0, 1);
46: }
47: ),
48: \RecursiveIteratorIterator::LEAVES_ONLY
49: ));
50: usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
51: return (string) $a > (string) $b ? 1 : -1;
52: });
53:
54: foreach ($files as $file) {
55: if (!$file->isFile() || '.php' !== substr($file->getFilename(), -4)) {
56: continue;
57: }
58:
59: if ($class = $this->findClass($file)) {
60: $refl = new \ReflectionClass($class);
61: if ($refl->isAbstract()) {
62: continue;
63: }
64:
65: $collection->addCollection($this->loader->load($class, $type));
66: }
67: }
68:
69: return $collection;
70: }
71:
72: 73: 74:
75: public function supports($resource, $type = null)
76: {
77: if (!is_string($resource)) {
78: return false;
79: }
80:
81: try {
82: $path = $this->locator->locate($resource);
83: } catch (\Exception $e) {
84: return false;
85: }
86:
87: return is_dir($path) && (!$type || 'annotation' === $type);
88: }
89: }
90:
91: 92: 93:
94: class RecursiveCallbackFilterIterator extends \FilterIterator implements \RecursiveIterator
95: {
96: private $iterator;
97: private $callback;
98:
99: public function __construct(\RecursiveIterator $iterator, $callback)
100: {
101: $this->iterator = $iterator;
102: $this->callback = $callback;
103: parent::__construct($iterator);
104: }
105:
106: public function accept()
107: {
108: return call_user_func($this->callback, $this->current(), $this->key(), $this->iterator);
109: }
110:
111: public function hasChildren()
112: {
113: return $this->iterator->hasChildren();
114: }
115:
116: public function getChildren()
117: {
118: return new static($this->iterator->getChildren(), $this->callback);
119: }
120: }
121: