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 loader that can load multiple resources. |
12
|
|
|
* |
13
|
|
|
* Array of resources can be: |
14
|
|
|
* - Associative arrays with resource path as key and resource type as value |
15
|
|
|
* - Numerical arrays with resource path as value |
16
|
|
|
*/ |
17
|
|
|
class ArrayLoader implements LoaderInterface |
18
|
|
|
{ |
19
|
|
|
private $container; |
20
|
|
|
private $serviceName; |
21
|
|
|
/** |
22
|
|
|
* @var LoaderInterface |
23
|
|
|
*/ |
24
|
|
|
private $loader; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* ArrayLoader constructor. |
28
|
|
|
* |
29
|
|
|
* @param Container $container |
30
|
|
|
* @param string $serviceName |
31
|
|
|
*/ |
32
|
17 |
|
public function __construct(Container $container, $serviceName) |
33
|
|
|
{ |
34
|
17 |
|
$this->container = $container; |
35
|
17 |
|
$this->serviceName = $serviceName; |
36
|
17 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
10 |
View Code Duplication |
public function load($resource, $type = null) |
|
|
|
|
42
|
|
|
{ |
43
|
10 |
|
if (!is_array($resource)) { |
44
|
9 |
|
return $this->getLoader()->load($resource, $type); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
$loader = $this->getLoader(); |
48
|
1 |
|
$collection = new RouteCollection(); |
49
|
|
|
|
50
|
1 |
|
foreach ($resource as $key => $value) { |
51
|
1 |
|
$collection->addCollection(is_int($key) ? $loader->load($value) : $loader->load($key, $value)); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
1 |
|
return $collection; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
3 |
View Code Duplication |
public function supports($resource, $type = null) |
|
|
|
|
61
|
|
|
{ |
62
|
3 |
|
if (!is_array($resource)) { |
63
|
1 |
|
return $this->getLoader()->supports($resource, $type); |
64
|
|
|
} |
65
|
|
|
|
66
|
2 |
|
$loader = $this->getLoader(); |
67
|
2 |
|
$supported = true; |
68
|
|
|
|
69
|
2 |
|
foreach ($resource as $key => $value) { |
70
|
3 |
|
$supported = $supported && (is_int($key) ? $loader->supports($value) : $loader->supports($key, $value)); |
71
|
2 |
|
} |
72
|
|
|
|
73
|
2 |
|
return $supported; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* {@inheritdoc} |
78
|
|
|
*/ |
79
|
1 |
|
public function getResolver() |
80
|
|
|
{ |
81
|
1 |
|
return $this->getLoader()->getResolver(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* {@inheritdoc} |
86
|
|
|
*/ |
87
|
1 |
|
public function setResolver(LoaderResolverInterface $resolver) |
88
|
|
|
{ |
89
|
1 |
|
return $this->getLoader()->setResolver($resolver); |
90
|
|
|
} |
91
|
|
|
|
92
|
15 |
|
protected function getLoader() |
93
|
|
|
{ |
94
|
15 |
|
if (null === $this->loader) { |
95
|
15 |
|
$this->loader = $this->container[$this->serviceName]; |
96
|
15 |
|
} |
97
|
|
|
|
98
|
15 |
|
return $this->loader; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
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.