1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Url; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface; |
10
|
|
|
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException; |
11
|
|
|
use function Zicht\Itertools\iterable; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* A provider that delegates to a number of registered providers, ordered by priority. |
15
|
|
|
*/ |
16
|
|
|
class DelegatingProvider implements Provider, SuggestableProvider, ListableProvider |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var Provider[] |
20
|
|
|
*/ |
21
|
|
|
protected $providers; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Initialize the provider |
25
|
|
|
*/ |
26
|
4 |
|
public function __construct() |
27
|
|
|
{ |
28
|
4 |
|
$this->providers = []; |
29
|
4 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Add a provider with the specified priority. Higher priority means exactly that ;) |
33
|
|
|
* |
34
|
|
|
* @param Provider $provider |
35
|
|
|
* @param int $priority |
36
|
|
|
* @return void |
37
|
|
|
*/ |
38
|
3 |
|
public function addProvider(Provider $provider, $priority = 0) |
39
|
|
|
{ |
40
|
3 |
|
$this->providers[] = [ |
41
|
3 |
|
'priority' => $priority, |
42
|
3 |
|
'provider' => $provider |
43
|
|
|
]; |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
1 |
|
public function supports($object) |
50
|
|
|
{ |
51
|
1 |
|
foreach ($this->getProviders() as $provider) { |
52
|
1 |
|
if ($provider->supports($object)) { |
53
|
1 |
|
return true; |
54
|
|
|
} |
55
|
|
|
} |
56
|
1 |
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
3 |
|
public function url($object, array $options = array()) |
63
|
|
|
{ |
64
|
3 |
|
foreach ($this->getProviders() as $provider) { |
65
|
2 |
|
if ($provider->supports($object)) { |
66
|
2 |
|
return $provider->url($object, $options); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
1 |
|
$objectType = is_object($object) |
72
|
|
|
? get_class($object) |
73
|
1 |
|
: (gettype($object) . ' (' . var_export($object, true) . ')'); |
74
|
|
|
|
75
|
1 |
|
throw new UnsupportedException("Can not render url for {$objectType}"); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritDoc} |
80
|
|
|
*/ |
81
|
1 |
|
public function suggest($pattern) |
82
|
|
|
{ |
83
|
1 |
|
$ret = array(); |
84
|
1 |
|
foreach ($this->getProviders() as $provider) { |
85
|
1 |
|
if ($provider instanceof SuggestableProvider) { |
86
|
1 |
|
$ret = array_merge($ret, $provider->suggest($pattern)); |
87
|
|
|
} |
88
|
|
|
} |
89
|
1 |
|
return $ret; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritDoc} |
94
|
|
|
*/ |
95
|
|
|
public function all(AuthorizationCheckerInterface $securityContext) |
96
|
|
|
{ |
97
|
|
|
$ret = array(); |
98
|
|
|
foreach ($this->getProviders() as $provider) { |
99
|
|
|
if ($provider instanceof ListableProvider) { |
100
|
|
|
$ret = array_merge($ret, $provider->all($securityContext)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
return $ret; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @return Provider[]|array |
108
|
|
|
*/ |
109
|
4 |
|
private function getProviders() |
110
|
|
|
{ |
111
|
4 |
|
return iterable($this->providers)->sorted('priority')->map('provider')->values(); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|