1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Zicht\Bundle\UrlBundle\Aliasing; |
8
|
|
|
|
9
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken; |
10
|
|
|
use Symfony\Component\Security\Core\Authorization\AccessDecisionManagerInterface; |
11
|
|
|
use Zicht\Bundle\UrlBundle\Entity\UrlAlias; |
12
|
|
|
use Zicht\Bundle\UrlBundle\Url\Provider; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Creates aliases |
16
|
|
|
*/ |
17
|
|
|
class Aliaser |
18
|
|
|
{ |
19
|
|
|
/** @var Aliasing */ |
20
|
|
|
protected $aliasing; |
21
|
|
|
/** @var Provider */ |
22
|
|
|
protected $provider; |
23
|
|
|
/** @var AliasingStrategy|DefaultAliasingStrategy */ |
24
|
|
|
protected $aliasingStrategy; |
25
|
|
|
/** @var string */ |
26
|
|
|
protected $conflictingPublicUrlStrategy = Aliasing::STRATEGY_SUFFIX; |
27
|
|
|
/** @var string */ |
28
|
|
|
protected $conflictingInternalUrlStrategy = Aliasing::STRATEGY_IGNORE; |
29
|
|
|
/** @var array */ |
30
|
|
|
protected $recursionProtection = []; |
31
|
|
|
/** @var AccessDecisionManagerInterface */ |
32
|
|
|
protected $decisionManager; |
33
|
|
|
/** @var array */ |
34
|
|
|
protected $scheduledRemoveAlias; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Constructor |
38
|
|
|
* |
39
|
|
|
* @param Aliasing $aliasing |
40
|
|
|
* @param Provider $provider |
41
|
|
|
* @param AliasingStrategy $naming |
42
|
|
|
* @param AccessDecisionManagerInterface $decisionManager |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Aliasing $aliasing, Provider $provider, AliasingStrategy $naming = null, AccessDecisionManagerInterface $decisionManager = null) |
45
|
|
|
{ |
46
|
|
|
$this->aliasing = $aliasing; |
47
|
|
|
$this->provider = $provider; |
48
|
|
|
if (null === $naming) { |
49
|
|
|
// TODO: remove this. It should break when not given any. This obstructs tests |
50
|
|
|
$naming = new DefaultAliasingStrategy(); |
51
|
|
|
} |
52
|
|
|
$this->aliasingStrategy = $naming; |
53
|
|
|
$this->decisionManager = $decisionManager; |
54
|
|
|
$this->scheduledRemoveAlias = []; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getConflictingInternalUrlStrategy() |
61
|
|
|
{ |
62
|
|
|
return $this->conflictingInternalUrlStrategy; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $conflictingInternalUrlStrategy |
67
|
|
|
*/ |
68
|
|
|
public function setConflictingInternalUrlStrategy($conflictingInternalUrlStrategy) |
69
|
|
|
{ |
70
|
|
|
Aliasing::validateInternalConflictingStrategy($conflictingInternalUrlStrategy); |
71
|
|
|
|
72
|
|
|
$this->conflictingInternalUrlStrategy = $conflictingInternalUrlStrategy; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
|
|
public function getConflictingPublicUrlStrategy() |
79
|
|
|
{ |
80
|
|
|
return $this->conflictingPublicUrlStrategy; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $conflictingPublicUrlStrategy |
85
|
|
|
*/ |
86
|
|
|
public function setConflictingPublicUrlStrategy($conflictingPublicUrlStrategy) |
87
|
|
|
{ |
88
|
|
|
Aliasing::validatePublicConflictingStrategy($conflictingPublicUrlStrategy); |
89
|
|
|
|
90
|
|
|
$this->conflictingPublicUrlStrategy = $conflictingPublicUrlStrategy; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Create an alias for the provided object. |
95
|
|
|
* |
96
|
|
|
* @param mixed $record |
97
|
|
|
* @param array $action |
98
|
|
|
* @return bool Whether or not an alias was created. |
99
|
|
|
*/ |
100
|
|
|
public function createAlias($record, $action = ['VIEW']) |
101
|
|
|
{ |
102
|
|
|
$internalUrl = $this->provider->url($record); |
103
|
|
|
|
104
|
|
|
if (in_array($internalUrl, $this->recursionProtection)) { |
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->recursionProtection[] = $internalUrl; |
109
|
|
|
|
110
|
|
|
if ($this->shouldGenerateAlias($record, $action)) { |
111
|
|
|
// Don't save an alias if the generated public alias is null |
112
|
|
|
if (null !== ($generatedAlias = $this->aliasingStrategy->generatePublicAlias($record))) { |
113
|
|
|
return $this->aliasing->addAlias( |
114
|
|
|
$generatedAlias, |
115
|
|
|
$internalUrl, |
116
|
|
|
UrlAlias::REWRITE, |
117
|
|
|
$this->conflictingPublicUrlStrategy, |
118
|
|
|
$this->conflictingInternalUrlStrategy |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return false; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Determines whether an alias should be generated for the given record. |
128
|
|
|
* |
129
|
|
|
* @param mixed $record |
130
|
|
|
* @param array $action |
131
|
|
|
* |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
|
|
public function shouldGenerateAlias($record, array $action = ['VIEW']) |
135
|
|
|
{ |
136
|
|
|
// without security, everything is considered public |
137
|
|
|
if (null === $this->decisionManager) { |
138
|
|
|
return true; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
return $this->decisionManager->decide(new AnonymousToken('main', 'anonymous'), $action, $record); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Removes an alias |
146
|
|
|
* |
147
|
|
|
* When $SCHEDULE is true the alias removal is delayed until removeScheduledAliases is called. |
148
|
|
|
* |
149
|
|
|
* @param mixed $record |
150
|
|
|
* @param boolean $schedule |
151
|
|
|
* @return void |
152
|
|
|
*/ |
153
|
|
|
public function removeAlias($record, $schedule = false) |
154
|
|
|
{ |
155
|
|
|
if ($schedule) { |
156
|
|
|
// delay removal until flushed |
157
|
|
|
$this->scheduledRemoveAlias [] = $this->provider->url($record); |
158
|
|
|
} else { |
159
|
|
|
$this->aliasing->removeAlias($this->provider->url($record)); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Remove scheduled aliases |
165
|
|
|
* |
166
|
|
|
* Example: |
167
|
|
|
* $aliaser->removeAlias($page, true); |
168
|
|
|
* # alias for $page is scheduled for removal, i.e. not yet actually removed |
169
|
|
|
* $aliaser->removeScheduledAliases() |
170
|
|
|
* # now the alias for $page is removed |
171
|
|
|
* |
172
|
|
|
* @return void |
173
|
|
|
*/ |
174
|
|
|
public function removeScheduledAliases() |
175
|
|
|
{ |
176
|
|
|
foreach ($this->scheduledRemoveAlias as $alias) { |
177
|
|
|
$this->aliasing->removeAlias($alias); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
$this->scheduledRemoveAlias = []; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Set batch processing on the aliasing service. |
185
|
|
|
* |
186
|
|
|
* @param bool $batch |
187
|
|
|
* @return callable |
188
|
|
|
*/ |
189
|
|
|
public function setIsBatch($batch) |
190
|
|
|
{ |
191
|
|
|
return $this->aliasing->setIsBatch($batch); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|