10000 bug #21832 [Routing] Ignore hidden directories when loading routes fr… · symfony/symfony@c579b96 · GitHub
[go: up one dir, main page]

Skip to content

Commit c579b96

Browse files
committed
bug #21832 [Routing] Ignore hidden directories when loading routes from annotations (jakzal)
This PR was merged into the 2.7 branch. Discussion ---------- [Routing] Ignore hidden directories when loading routes from annotations | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #21497 | License | MIT | Doc PR | - The problem surfaced after implementing #18869. Therefore it doesn't exist on 2.7, but I'd still merge it there to avoid conflicts when merging between branches. Without this fix, the oldest branch the added test will fail is 3.2. Commits ------- ce9df02 [Routing] Ignore hidden directories when loading routes from annotations
2 parents 0581218 + ce9df02 commit c579b96

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 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(\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+
}

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)
0