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