Completed
Push — master ( 98adac...89edb3 )
by Christian
05:26
created

DirectoryRouteLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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\Loader\Loader;
15
use Symfony\Component\Routing\RouteCollection;
16
17
/**
18
 * Parse annotated controller classes from all files of a directory.
19
 *
20
 * @author Christian Flothmann <[email protected]>
21
 */
22
class DirectoryRouteLoader extends Loader
23
{
24
    private $processor;
25
26 12
    public function __construct(RestRouteProcessor $processor)
27
    {
28 12
        $this->processor = $processor;
29 12
    }
30
31
    /**
32
     * {@inheritdoc}
33
     */
34 1
    public function load($resource, $type = null)
35
    {
36 1
        if (!is_dir($resource)) {
37
            throw new \InvalidArgumentException(sprintf('Given resource of type "%s" is no directory.', $resource));
38
        }
39
40 1
        $collection = new RouteCollection();
41
42 1
        $directoryIterator = new \DirectoryIterator($resource);
43
44 1
        foreach ($directoryIterator as $file) {
45 1
            if ($file->getFilename() === '.' || $file->getFilename() === '..') {
46 1
                continue;
47
            }
48
49 1
            $imported = $this->processor->importResource($this, ClassUtils::findClassInFile($file->getPathname()), array(), null, null, 'rest');
50 1
            $collection->addCollection($imported);
51 1
        }
52
53 1
        return $collection;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 5
    public function supports($resource, $type = null)
60
    {
61 5
        return 'rest' === $type && is_string($resource) && is_dir($resource);
62
    }
63
}
64