forked from ezsystems/ezpublish-kernel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLWildcardService.php
323 lines (293 loc) · 9.88 KB
/
URLWildcardService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
<?php
/**
* File containing the eZ\Publish\Core\Repository\URLWildcardService class.
*
* @copyright Copyright (C) 1999-2013 eZ Systems AS. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
* @version //autogentag//
* @package eZ\Publish\Core\Repository
*/
namespace eZ\Publish\Core\Repository;
use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
use eZ\Publish\API\Repository\Repository as RepositoryInterface;
use eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler;
use eZ\Publish\API\Repository\Values\Content\URLWildcard;
use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
use eZ\Publish\SPI\Persistence\Content\UrlWildcard as SPIUrlWildcard;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
use eZ\Publish\Core\Base\Exceptions\ContentValidationException;
use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
use Exception;
/**
* URLAlias service
*
* @example Examples/urlalias.php
*
* @package eZ\Publish\Core\Repository
*/
class URLWildcardService implements URLWildcardServiceInterface
{
/**
* @var \eZ\Publish\API\Repository\Repository
*/
protected $repository;
/**
* @var \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler
*/
protected $urlWildcardHandler;
/**
* @var array
*/
protected $settings;
/**
* Setups service with reference to repository object that created it & corresponding handler
*
* @param \eZ\Publish\API\Repository\Repository $repository
* @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler $urlWildcardHandler
* @param array $settings
*/
public function __construct( RepositoryInterface $repository, Handler $urlWildcardHandler, array $settings = array() )
{
$this->repository = $repository;
$this->urlWildcardHandler = $urlWildcardHandler;
// Union makes sure default settings are ignored if provided in argument
$this->settings = $settings + array(
//'defaultSetting' => array(),
);
}
/**
* Creates a new url wildcard
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException if the $sourceUrl pattern already exists
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to create url wildcards
* @throws \eZ\Publish\API\Repository\Exceptions\ContentValidationException if the number of "*" patterns in $sourceUrl and
* the numbers in {\d} placeholders in $destinationUrl does not match.
*
* @param string $sourceUrl
* @param string $destinationUrl
* @param boolean $forward
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function create( $sourceUrl, $destinationUrl, $forward = false )
{
if ( $this->repository->hasAccess( 'content', 'urltranslator' ) !== true )
{
throw new UnauthorizedException( 'content', 'urltranslator' );
}
$sourceUrl = $this->cleanUrl( $sourceUrl );
$destinationUrl = $this->cleanUrl( $destinationUrl );
$spiUrlWildcards = $this->urlWildcardHandler->loadAll();
foreach ( $spiUrlWildcards as $wildcard )
{
if ( $wildcard->sourceUrl === $sourceUrl )
{
throw new InvalidArgumentException(
"\$sourceUrl",
"Pattern already exists"
);
}
}
preg_match_all( '(\\*)', $sourceUrl, $patterns );
preg_match_all( '(\{(\d+)\})', $destinationUrl, $placeholders );
$patterns = array_map( 'intval', $patterns[0] );
$placeholders = array_map( 'intval', $placeholders[1] );
if ( !empty( $placeholders ) && max( $placeholders ) > count( $patterns ) )
{
throw new ContentValidationException( "Placeholders are not matching with wildcards." );
}
$this->repository->beginTransaction();
try
{
$spiUrlWildcard = $this->urlWildcardHandler->create(
$sourceUrl,
$destinationUrl,
$forward
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
return $this->buildUrlWildcardDomainObject( $spiUrlWildcard );
}
/**
* Removes leading and trailing slashes and spaces.
*
* @param string $url
*
* @return string
*/
protected function cleanUrl( $url )
{
return "/" . trim( $url, "/ " );
}
/**
* removes an url wildcard
*
* @throws \eZ\Publish\API\Repository\Exceptions\UnauthorizedException if the user is not allowed to remove url wildcards
*
* @param \eZ\Publish\API\Repository\Values\Content\UrlWildcard $urlWildcard the url wildcard to remove
*/
public function remove( URLWildcard $urlWildcard )
{
if ( $this->repository->hasAccess( 'content', 'urltranslator' ) !== true )
{
throw new UnauthorizedException( 'content', 'urltranslator' );
}
$this->repository->beginTransaction();
try
{
$this->urlWildcardHandler->remove(
$urlWildcard->id
);
$this->repository->commit();
}
catch ( Exception $e )
{
$this->repository->rollback();
throw $e;
}
}
/**
* Loads a url wild card
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url wild card was not found
*
* @param mixed $id
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard
*/
public function load( $id )
{
return $this->buildUrlWildcardDomainObject(
$this->urlWildcardHandler->load( $id )
);
}
/**
* Loads all url wild card (paged)
*
* @param int $offset
* @param int $limit
*
* @return \eZ\Publish\API\Repository\Values\Content\UrlWildcard[]
*/
public function loadAll( $offset = 0, $limit = -1 )
{
$spiUrlWildcards = $this->urlWildcardHandler->loadAll(
$offset,
$limit
);
$urlWildcards = array();
foreach ( $spiUrlWildcards as $spiUrlWildcard )
{
$urlWildcards[] = $this->buildUrlWildcardDomainObject( $spiUrlWildcard );
}
return $urlWildcards;
}
/**
* Translates an url to an existing uri resource based on the
* source/destination patterns of the url wildcard.
*
* @throws \eZ\Publish\API\Repository\Exceptions\NotFoundException if the url could not be translated
*
* @param mixed $url
*
* @return \eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult
*/
public function translate( $url )
{
$spiUrlWildcards = $this->urlWildcardHandler->loadAll();
// sorts wildcards by length of source URL string
// @todo sort by specificity of the pattern?
uasort(
$spiUrlWildcards,
function ( SPIUrlWildcard $w1, SPIUrlWildcard $w2 )
{
return strlen( $w2->sourceUrl ) - strlen( $w1->sourceUrl );
}
);
foreach ( $spiUrlWildcards as $wildcard )
{
if ( $uri = $this->match( $url, $wildcard ) )
{
return new URLWildcardTranslationResult(
array(
'uri' => $uri,
'forward' => $wildcard->forward
)
);
}
}
throw new NotFoundException( "URLWildcard", $url );
}
/**
* Tests if the given url matches against the given url wildcard.
*
* if the wildcard matches on the given url this method will return a ready
* to use destination url, otherwise this method will return <b>NULL</b>.
*
* @param string $url
* @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard $wildcard
*
* @return null|string
*/
private function match( $url, SPIUrlWildcard $wildcard )
{
if ( preg_match( $this->compile( $wildcard->sourceUrl ), $url, $match ) )
{
return $this->substitute( $wildcard->destinationUrl, $match );
}
return null;
}
/**
* Compiles the given url pattern into a regular expression.
*
* @param string $sourceUrl
*
* @return string
*/
private function compile( $sourceUrl )
{
return '(^' . str_replace( '\\*', '(.*)', preg_quote( $sourceUrl ) ) . '$)U';
}
/**
* Substitutes all placeholders ({\d}) in the given <b>$destinationUrl</b> with
* the values from the given <b>$values</b> array.
*
* @param string $destinationUrl
* @param array $values
*
* @return string
*/
private function substitute( $destinationUrl, array $values )
{
preg_match_all( '(\{(\d+)\})', $destinationUrl, $matches );
foreach ( $matches[1] as $match )
{
$destinationUrl = str_replace( "{{$match}}", $values[$match], $destinationUrl );
}
return $destinationUrl;
}
/**
* Builds API UrlWildcard object from given SPI UrlWildcard object
*
* @param \eZ\Publish\SPI\Persistence\Content\UrlWildcard $wildcard
*
* @return \eZ\Publish\API\Repository\Values\Content\URLWildcard
*/
private function buildUrlWildcardDomainObject( SPIUrlWildcard $wildcard )
{
return new URLWildcard(
array(
"id" => $wildcard->id,
"destinationUrl" => $wildcard->destinationUrl,
"sourceUrl" => $wildcard->sourceUrl,
"forward" => $wildcard->forward
)
);
}
}