|
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
|
|
|
|