Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d749406

Browse files
committed
[Routing] Ignore hidden directories when loading routes from annotations
1 parent a786b5a commit d749406

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/Symfony/Component/Routing/Loader/AnnotationDirectoryLoader.php

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,15 @@ public function load($path, $type = null)
3838

3939
$collection = new RouteCollection();
4040
$collection->addResource(new DirectoryResource($dir, '/\.php$/'));
41-
$files = iterator_to_array(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir), \RecursiveIteratorIterator::LEAVES_ONLY));
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+
));
4250
usort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
4351
return (string) $a > (string) $b ? 1 : -1;
4452
});
@@ -79,3 +87,34 @@ public function supports($resource, $type = null)
7987
return is_dir($path) && (!$type || 'annotation' === $type);
8088
}
8189
}
90+
91+
/**
92+
* @internal To be removed in 3.1 as RecursiveCallbackFilterIterator is available since PHP 5.4
93+
*/
94+
class RecursiveCallbackFilterIterator extends \FilterIterator implements \RecursiveIterator
95+
{
96+
private $iterator;
97+
private $callback;
98+
99+
public function __construct(\Iterator $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+
}

src/Symfony/Component/Routing/Tests/Loader/AnnotationDirectoryLoaderTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,22 @@ public function testLoad()
4040
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
4141
}
4242

43+
public function testLoadIgnoresHiddenDirectories()
44+
{
45+
$this->expectAnnotationsToBeReadFrom(array(
46+
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\BarClass',
47+
'Symfony\Component\Routing\Tests\Fixtures\AnnotatedClasses\FooClass',
48+
));
49+
50+
$this->reader
51+
->expects($this->any())
52+
->method('getMethodAnnotations')
53+
->will($this->returnValue(array()))
54+
;
55+
56+
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses');
57+
}
58+
4359
public function testSupports()
4460
{
4561
$fixturesDir = __DIR__.'/../Fixtures';
@@ -50,4 +66,13 @@ public function testSupports()
5066
$this->assertTrue($this->loader->supports($fixturesDir, 'annotation'), '->supports() checks the resource type if specified');
5167
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
5268
}
69+
70+
private function expectAnnotationsToBeReadFrom(array $classes)
71+
{
72+
$this->reader->expects($this->exactly(count($classes)))
73+
->method('getClassAnnotation')
74+
->with($this->callback(function (\ReflectionClass $class) use ($classes) {
75+
return in_array($class->getName(), $classes);
76+
}));
77+
}
5378
}

0 commit comments

Comments
 (0)