|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @copyright Copyright (C) eZ Systems AS. All rights reserved. |
|
5
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
6
|
|
|
*/ |
|
7
|
|
|
declare(strict_types=1); |
|
8
|
|
|
|
|
9
|
|
|
namespace eZ\Publish\Core\IO\UrlDecorator; |
|
10
|
|
|
|
|
11
|
|
|
use eZ\Publish\Core\IO\Exception\InvalidBinaryPrefixException; |
|
12
|
|
|
use eZ\Publish\Core\IO\IOConfigProvider; |
|
13
|
|
|
use eZ\Publish\Core\IO\UrlDecorator; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Prefixes the URI with a string. Ensures an initial / in the parameter. |
|
17
|
|
|
*/ |
|
18
|
|
|
class Prefix implements UrlDecorator |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var \eZ\Publish\Core\IO\IOConfigProvider */ |
|
21
|
|
|
protected $ioConfigResolver; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct(IOConfigProvider $IOConfigResolver) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->ioConfigResolver = $IOConfigResolver; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
public function getPrefix(): string |
|
29
|
|
|
{ |
|
30
|
|
|
$prefix = $this->ioConfigResolver->getLegacyUrlPrefix(); |
|
31
|
|
|
|
|
32
|
|
|
return trim($prefix, '/') . '/'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function decorate($id) |
|
36
|
|
|
{ |
|
37
|
|
|
$prefix = $this->getPrefix(); |
|
38
|
|
|
if (empty($prefix)) { |
|
39
|
|
|
return $id; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
return $prefix . trim($id, '/'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException |
|
47
|
|
|
*/ |
|
48
|
|
|
public function undecorate($url) |
|
49
|
|
|
{ |
|
50
|
|
|
$prefix = $this->getPrefix(); |
|
51
|
|
|
if (empty($prefix)) { |
|
52
|
|
|
return $url; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
if (strpos($url, $prefix) !== 0) { |
|
56
|
|
|
throw new InvalidBinaryPrefixException($url, $prefix); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return trim(substr($url, strlen($prefix)), '/'); |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|