Completed
Push — master ( f9a35e...e44c95 )
by Guilh
08:02 queued 04:27
created

DirectoryRouteLoader::load()   B

Complexity

Conditions 6
Paths 8

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 6.0106

Importance

Changes 2
Bugs 1 Features 0
Metric Value
dl 0
loc 23
ccs 14
cts 15
cp 0.9333
rs 8.5906
c 2
b 1
f 0
cc 6
eloc 12
nc 8
nop 2
crap 6.0106
1
<?php
2
3
/*
4
 * This file is part of the FOSRestBundle package.
5
 *
6
 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\RestBundle\Routing\Loader;
13
14
use Symfony\Component\Config\FileLocatorInterface;
15
use Symfony\Component\Config\Loader\Loader;
16
use Symfony\Component\Finder\Finder;
17
use Symfony\Component\Routing\RouteCollection;
18
19
/**
20
 * Parse annotated controller classes from all files of a directory.
21
 *
22
 * @author Christian Flothmann <[email protected]>
23
 */
24
class DirectoryRouteLoader extends Loader
25
{
26
    private $fileLocator;
27
    private $processor;
28
29 17
    public function __construct(FileLocatorInterface $fileLocator, RestRouteProcessor $processor)
30
    {
31 17
        $this->fileLocator = $fileLocator;
32 17
        $this->processor = $processor;
33 17
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 2
    public function load($resource, $type = null)
39
    {
40 2
        if (isset($resource[0]) && '@' === $resource[0]) {
41 1
            $resource = $this->fileLocator->locate($resource);
42 1
        }
43
44 2
        if (!is_dir($resource)) {
45
            throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource));
46
        }
47
48 2
        $collection = new RouteCollection();
49
50 2
        $finder = new Finder();
51
52 2
        foreach ($finder->in($resource)->name('*.php')->files() as $file) {
53 2
            if($class = ClassUtils::findClassInFile($file)) {
54 2
                $imported = $this->processor->importResource($this, $class, array(), null, null, 'rest');
55 2
                $collection->addCollection($imported);
56 2
            }
57 2
        }
58
59 2
        return $collection;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 16
    public function supports($resource, $type = null)
66
    {
67 16
        if ('rest' !== $type || !is_string($resource)) {
68 1
            return false;
69
        }
70
71 15
        if (isset($resource[0]) && '@' === $resource[0]) {
72 1
            $resource = $this->fileLocator->locate($resource);
73 1
        }
74
75 15
        return is_dir($resource);
76
    }
77
}
78