Completed
Push — master ( c7deb4...314746 )
by Beñat
09:20 queued 06:49
created

RoutesLoader::load()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 17
rs 9.2
cc 4
eloc 10
nc 4
nop 2
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle\Routing;
14
15
use Symfony\Component\Config\Loader\LoaderInterface;
16
use Symfony\Component\Config\Loader\LoaderResolverInterface;
17
use Symfony\Component\Routing\RouteCollection;
18
19
/**
20
 * Routes loader base class.
21
 *
22
 * Service that loads dynamically routes via PHP.
23
 *
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
abstract class RoutesLoader implements LoaderInterface
27
{
28
    /**
29
     * Array which contains the
30
     * routes configuration.
31
     *
32
     * @var array
33
     */
34
    protected $config;
35
36
    /**
37
     * Boolean that checks if the
38
     * routes are already loaded or not.
39
     *
40
     * @var bool
41
     */
42
    protected $loaded;
43
44
    /**
45
     * Collection of routes.
46
     *
47
     * @var RouteCollection
48
     */
49
    protected $routes;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param array $config Array which contains the routes configuration
55
     */
56
    public function __construct(array $config = [])
57
    {
58
        $this->config = $config;
59
        $this->loaded = false;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function load($resource, $type = null)
66
    {
67
        if (true === $this->loaded) {
68
            throw new \RuntimeException('Do not add this loader twice');
69
        }
70
        if (empty($this->config)) {
71
            return;
72
        }
73
74
        $this->routes = new RouteCollection();
75
        foreach ($this->config as $file => $config) {
76
            $this->register($file, $config);
77
        }
78
        $this->loaded = true;
79
80
        return $this->routes;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function getResolver()
87
    {
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function setResolver(LoaderResolverInterface $resolver)
94
    {
95
    }
96
97
    /**
98
     * Registers a new route inside route
99
     * collection with the given params.
100
     *
101
     * @param string $file   The file name
102
     * @param array  $config The user configuration array
103
     */
104
    abstract protected function register($file, array $config);
105
}
106