Skip to content

Commit db979e8

Browse files
bug #25113 [Routing] Fix "config-file-relative" annotation loader resources (nicolas-grekas, sroze)
This PR was merged into the 3.3 branch. Discussion ---------- [Routing] Fix "config-file-relative" annotation loader resources | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | slight behavior change | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - By using the locator in its `supports()` method, the `AnnotationDirectoryLoader` breaks config-relative annotation resources. The workaround is to fallback on `kernel.root_dir`-relative paths, as done in https://github.com/symfony/recipes/blob/master/doctrine/annotations/1.0/config/routes/annotations.yaml But as you can see, this is rather WTF: extra knowledge is required to know what to type there. All the other loader look relatively to the config file first. This is a bug, but since this is a slight behavior change, I think it's best to merge it on 3.4. Commits ------- f4999d8 Add tests proving it can load annotated files 5998e9d [Routing] Fix "config-file-relative" annotation loader resources
2 parents d141541 + f4999d8 commit db979e8

File tree

2 files changed

+27
-5
lines changed

2 files changed

+27
-5
lines changed

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ class AnnotationDirectoryLoader extends AnnotationFileLoader
3434
*/
3535
public function load($path, $type = null)
3636
{
37-
$dir = $this->locator->locate($path);
37+
if (!is_dir($dir = $this->locator->locate($path))) {
38+
return parent::supports($path, $type) ? parent::load($path, $type) : new RouteCollection();
39+
}
3840

3941
$collection = new RouteCollection();
4042
$collection->addResource(new DirectoryResource($dir, '/\.php$/'));
@@ -74,16 +76,18 @@ function (\SplFileInfo $current) {
7476
*/
7577
public function supports($resource, $type = null)
7678
{
77-
if (!is_string($resource)) {
79+
if ('annotation' === $type) {
80+
return true;
81+
}
82+
83+
if ($type || !is_string($resource)) {
7884
return false;
7985
}
8086

8187
try {
82-
$path = $this->locator->locate($resource);
88+
return is_dir($this->locator->locate($resource));
8389
} catch (\Exception $e) {
8490
return false;
8591
}
86-
87-
return is_dir($path) && (!$type || 'annotation' === $type);
8892
}
8993
}

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,24 @@ public function testSupports()
6969
$this->assertFalse($this->loader->supports($fixturesDir, 'foo'), '->supports() checks the resource type if specified');
7070
}
7171

72+
public function testItSupportsAnyAnnotation()
73+
{
74+
$this->assertTrue($this->loader->supports(__DIR__.'/../Fixtures/even-with-not-existing-folder', 'annotation'));
75+
}
76+
77+
public function testLoadFileIfLocatedResourceIsFile()
78+
{
79+
$this->reader->expects($this->exactly(1))->method('getClassAnnotation');
80+
81+
$this->reader
82+
->expects($this->any())
83+
->method('getMethodAnnotations')
84+
->will($this->returnValue(array()))
85+
;
86+
87+
$this->loader->load(__DIR__.'/../Fixtures/AnnotatedClasses/FooClass.php');
88+
}
89+
7290
private function expectAnnotationsToBeReadFrom(array $classes)
7391
{
7492
$this->reader->expects($this->exactly(count($classes)))

0 commit comments

Comments
 (0)