1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Skalpa\Silex\Symfony\Routing\Loader; |
4
|
|
|
|
5
|
|
|
use Pimple\Container; |
6
|
|
|
use Symfony\Component\Config\Loader\LoaderInterface; |
7
|
|
|
use Symfony\Component\Config\Loader\LoaderResolverInterface; |
8
|
|
|
use Symfony\Component\Routing\RouteCollection; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Lazy routes loader. |
12
|
|
|
* |
13
|
|
|
* Delegates loading to a Pimple service instantiated lazily. This loader |
14
|
|
|
* can also merge routes loaded from multiple resources if an array is |
15
|
|
|
* passed to load(). |
16
|
|
|
* |
17
|
|
|
* Arrays of resources can be: |
18
|
|
|
* - Associative arrays with resource path as key and resource type as value |
19
|
|
|
* - Numerical arrays with resource path as value |
20
|
|
|
*/ |
21
|
|
|
class LazyLoader implements LoaderInterface |
22
|
|
|
{ |
23
|
|
|
private $container; |
24
|
|
|
private $serviceName; |
25
|
|
|
/** |
26
|
|
|
* @var LoaderInterface |
27
|
|
|
*/ |
28
|
|
|
private $loader; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* ArrayLoader constructor. |
32
|
|
|
* |
33
|
|
|
* @param Container $container |
34
|
|
|
* @param string $serviceName |
35
|
|
|
*/ |
36
|
|
|
public function __construct(Container $container, $serviceName) |
37
|
|
|
{ |
38
|
|
|
$this->container = $container; |
39
|
|
|
$this->serviceName = $serviceName; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
|
View Code Duplication |
public function load($resource, $type = null) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if (!is_array($resource)) { |
48
|
|
|
return $this->getLoader()->load($resource, $type); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
$loader = $this->getLoader(); |
52
|
|
|
$collection = new RouteCollection(); |
53
|
|
|
|
54
|
|
|
foreach ($resource as $key => $value) { |
55
|
|
|
$collection->addCollection(is_int($key) ? $loader->load($value) : $loader->load($key, $value)); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $collection; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
View Code Duplication |
public function supports($resource, $type = null) |
|
|
|
|
65
|
|
|
{ |
66
|
|
|
if (!is_array($resource)) { |
67
|
|
|
return $this->getLoader()->supports($resource, $type); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$loader = $this->getLoader(); |
71
|
|
|
$supported = true; |
72
|
|
|
|
73
|
|
|
foreach ($resource as $key => $value) { |
74
|
|
|
$supported = $supported && (is_int($key) ? $loader->supports($value) : $loader->supports($key, $value)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
return $supported; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* {@inheritdoc} |
82
|
|
|
*/ |
83
|
|
|
public function getResolver() |
84
|
|
|
{ |
85
|
|
|
return $this->getLoader()->getResolver(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* {@inheritdoc} |
90
|
|
|
*/ |
91
|
|
|
public function setResolver(LoaderResolverInterface $resolver) |
92
|
|
|
{ |
93
|
|
|
return $this->getLoader()->setResolver($resolver); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
protected function getLoader() |
97
|
|
|
{ |
98
|
|
|
if (null === $this->loader) { |
99
|
|
|
$this->loader = $this->container[$this->serviceName]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return $this->loader; |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.