Completed
Pull Request — master (#29)
by Kévin
02:22
created

AnnotationDirectoryLoader   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 5
c 3
b 0
f 1
lcom 1
cbo 2
dl 0
loc 38
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A supports() 0 18 4
1
<?php
2
3
/*
4
 * (c) Kévin Dunglas <[email protected]>
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
namespace Dunglas\ActionBundle\Routing;
11
12
use Symfony\Component\Config\FileLocatorInterface;
13
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Dunglas\ActionBundle\Routing\AnnotationClassLoader.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader as BaseAnnotationDirectoryLoader;
15
16
/**
17
 * Overrides the supported type.
18
 *
19
 * @author Kévin Dunglas <[email protected]>
20
 */
21
class AnnotationDirectoryLoader extends BaseAnnotationDirectoryLoader
22
{
23
    private $scannedDirectories;
24
25
    /**
26
     * @param FileLocatorInterface  $locator
27
     * @param AnnotationClassLoader $loader
28
     * @param array                 $scannedDirectories
29
     */
30
    public function __construct(FileLocatorInterface $locator, AnnotationClassLoader $loader, $scannedDirectories)
31
    {
32
        parent::__construct($locator, $loader);
33
34
        $this->scannedDirectories = $scannedDirectories;
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function supports($resource, $type = null)
41
    {
42
        if (!is_string($resource)) {
43
            return false;
44
        }
45
46
        try {
47
            $path = $this->locator->locate($resource);
48
        } catch (\Exception $e) {
49
            return false;
50
        }
51
52
        if (!isset($this->scannedDirectories[rtrim($path, '/')])) {
53
            return false;
54
        }
55
56
        return parent::supports($resource, $type);
57
    }
58
}
59