Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/Repository/URLWildcardService.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\Repository;
8
9 use eZ\Publish\API\Repository\PermissionResolver;
10 use eZ\Publish\API\Repository\URLWildcardService as URLWildcardServiceInterface;
11 use eZ\Publish\API\Repository\Repository as RepositoryInterface;
12 use eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler;
13 use eZ\Publish\API\Repository\Values\Content\URLWildcard;
14 use eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult;
15 use eZ\Publish\SPI\Persistence\Content\UrlWildcard as SPIUrlWildcard;
16 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentException;
17 use eZ\Publish\Core\Base\Exceptions\ContentValidationException;
18 use eZ\Publish\Core\Base\Exceptions\UnauthorizedException;
19 use Exception;
20
26 class URLWildcardService implements URLWildcardServiceInterface
27 {
29 protected $repository;
30
32 protected $urlWildcardHandler;
33
35 private $permissionResolver;
36
38 protected $settings;
39
48 public function __construct(
49 RepositoryInterface $repository,
50 Handler $urlWildcardHandler,
51 PermissionResolver $permissionResolver,
52 array $settings = []
53 ) {
54 $this->repository = $repository;
55 $this->urlWildcardHandler = $urlWildcardHandler;
56 $this->permissionResolver = $permissionResolver;
57 $this->settings = $settings;
58 }
59
74 public function create($sourceUrl, $destinationUrl, $forward = false): URLWildcard
75 {
76 if ($this->permissionResolver->hasAccess('content', 'urltranslator') === false) {
77 throw new UnauthorizedException('content', 'urltranslator');
78 }
79
80 $sourceUrl = $this->cleanUrl($sourceUrl);
81 $destinationUrl = $this->cleanUrl($destinationUrl);
82
83 if ($this->urlWildcardHandler->exactSourceUrlExists($this->cleanPath($sourceUrl))) {
84 throw new InvalidArgumentException(
85 '$sourceUrl',
86 'Pattern already exists'
87 );
88 }
89
90 preg_match_all('(\\*)', $sourceUrl, $patterns);
91 preg_match_all('(\{(\d+)\})', $destinationUrl, $placeholders);
92
93 $patterns = array_map('intval', $patterns[0]);
94 $placeholders = array_map('intval', $placeholders[1]);
95
96 if (!empty($placeholders) && max($placeholders) > count($patterns)) {
97 throw new ContentValidationException('Placeholders are not matching with wildcards.');
98 }
99
100 $this->repository->beginTransaction();
101 try {
102 $spiUrlWildcard = $this->urlWildcardHandler->create(
103 $sourceUrl,
104 $destinationUrl,
105 $forward
106 );
107 $this->repository->commit();
108 } catch (Exception $e) {
109 $this->repository->rollback();
110 throw $e;
111 }
112
113 return $this->buildUrlWildcardDomainObject($spiUrlWildcard);
114 }
115
123 public function remove(URLWildcard $urlWildcard): void
124 {
125 if (!$this->permissionResolver->canUser('content', 'urltranslator', $urlWildcard)) {
126 throw new UnauthorizedException('content', 'urltranslator');
127 }
128
129 $this->repository->beginTransaction();
130 try {
131 $this->urlWildcardHandler->remove(
132 $urlWildcard->id
133 );
134 $this->repository->commit();
135 } catch (Exception $e) {
136 $this->repository->rollback();
137 throw $e;
138 }
139 }
140
150 public function load($id): URLWildcard
151 {
152 return $this->buildUrlWildcardDomainObject(
153 $this->urlWildcardHandler->load($id)
154 );
155 }
156
165 public function loadAll($offset = 0, $limit = -1): array
166 {
167 $spiUrlWildcards = $this->urlWildcardHandler->loadAll(
168 $offset,
169 $limit
170 );
171
172 $urlWildcards = [];
173 foreach ($spiUrlWildcards as $spiUrlWildcard) {
174 $urlWildcards[] = $this->buildUrlWildcardDomainObject($spiUrlWildcard);
175 }
176
177 return $urlWildcards;
178 }
179
190 public function translate($url): URLWildcardTranslationResult
191 {
192 $spiWildcard = $this->urlWildcardHandler->translate($this->cleanPath($url));
193
194 return new URLWildcardTranslationResult(
195 [
196 'uri' => $spiWildcard->destinationUrl,
197 'forward' => $spiWildcard->forward,
198 ]
199 );
200 }
201
209 private function buildUrlWildcardDomainObject(SPIUrlWildcard $wildcard): URLWildcard
210 {
211 return new URLWildcard(
212 [
213 'id' => $wildcard->id,
214 'destinationUrl' => $wildcard->destinationUrl,
215 'sourceUrl' => $wildcard->sourceUrl,
216 'forward' => $wildcard->forward,
217 ]
218 );
219 }
220
228 private function cleanUrl(string $url): string
229 {
230 return '/' . trim($url, '/ ');
231 }
232
240 private function cleanPath(string $path): string
241 {
242 return trim($path, '/ ');
243 }
244 }
eZ\Publish\API\Repository\Values\Content\URLWildcardTranslationResult
Definition: URLWildcardTranslationResult.php:18
eZ\Publish\API\Repository\Values\Content\URLWildcard
Definition: API/Repository/Values/Content/URLWildcard.php:20
eZ\Publish\Core\Base\Exceptions\ContentValidationException
This Exception is thrown on create or update content one or more given fields are not valid.
Definition: Core/Base/Exceptions/ContentValidationException.php:17
eZ\Publish\Core\Base\Exceptions\InvalidArgumentException
Invalid Argument Type Exception implementation.
Definition: Core/Base/Exceptions/InvalidArgumentException.php:20
eZ\Publish\Core\Base\Exceptions\UnauthorizedException
UnauthorizedException Exception implementation.
Definition: Core/Base/Exceptions/UnauthorizedException.php:21
eZ\Publish\Core\Repository\URLWildcardService
Definition: Core/Repository/URLWildcardService.php:27
eZ\Publish\Core\Repository\URLWildcardService\load
load($id)
Loads a url wild card.
Definition: Core/Repository/URLWildcardService.php:150
eZ\Publish\Core\Repository\URLWildcardService\$permissionResolver
$permissionResolver
Definition: Core/Repository/URLWildcardService.php:35
eZ\Publish\Core\Repository\URLWildcardService\$settings
$settings
Definition: Core/Repository/URLWildcardService.php:38
eZ\Publish\Core\Repository\URLWildcardService\cleanUrl
cleanUrl(string $url)
Removes leading and trailing slashes and spaces.
Definition: Core/Repository/URLWildcardService.php:228
eZ\Publish\Core\Repository\URLWildcardService\__construct
__construct(RepositoryInterface $repository, Handler $urlWildcardHandler, PermissionResolver $permissionResolver, array $settings=[])
Setups service with reference to repository object that created it & corresponding handler.
Definition: Core/Repository/URLWildcardService.php:48
eZ\Publish\Core\Repository\URLWildcardService\cleanPath
cleanPath(string $path)
Removes leading slash from given path.
Definition: Core/Repository/URLWildcardService.php:240
eZ\Publish\Core\Repository\URLWildcardService\create
create($sourceUrl, $destinationUrl, $forward=false)
Creates a new url wildcard.
Definition: Core/Repository/URLWildcardService.php:74
eZ\Publish\Core\Repository\URLWildcardService\$repository
$repository
Definition: Core/Repository/URLWildcardService.php:29
eZ\Publish\Core\Repository\URLWildcardService\translate
translate($url)
Translates an url to an existing uri resource based on the source/destination patterns of the url wil...
Definition: Core/Repository/URLWildcardService.php:190
eZ\Publish\Core\Repository\URLWildcardService\$urlWildcardHandler
$urlWildcardHandler
Definition: Core/Repository/URLWildcardService.php:32
eZ\Publish\Core\Repository\URLWildcardService\buildUrlWildcardDomainObject
buildUrlWildcardDomainObject(SPIUrlWildcard $wildcard)
Builds API UrlWildcard object from given SPI UrlWildcard object.
Definition: Core/Repository/URLWildcardService.php:209
eZ\Publish\Core\Repository\URLWildcardService\loadAll
loadAll($offset=0, $limit=-1)
Loads all url wild card (paged).
Definition: Core/Repository/URLWildcardService.php:165
eZ\Publish\API\Repository\PermissionResolver
This service provides methods for resolving permissions.
Definition: API/Repository/PermissionResolver.php:17
eZ\Publish\API\Repository\Repository
Repository interface.
Definition: API/Repository/Repository.php:16
eZ\Publish\API\Repository\URLWildcardService
Definition: API/Repository/URLWildcardService.php:17
eZ\Publish\SPI\Persistence\Content\Language\Handler
Language Handler interface.
Definition: SPI/Persistence/Content/Language/Handler.php:15
eZ\Publish\SPI\Persistence\Content\UrlWildcard\Handler
The UrlWildcard Handler interface provides nice urls with wildcards management.
Definition: SPI/Persistence/Content/UrlWildcard/Handler.php:18
eZ\Publish\Core\Repository
Definition: Core/Repository/BookmarkService.php:9
eZ\Publish\SPI\Persistence\Content\UrlWildcard
Definition: SPI/Persistence/Content/UrlWildcard/Handler.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.