1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Sulu CMS. |
5
|
|
|
* |
6
|
|
|
* (c) MASSIVE ART WebServices GmbH |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Sulu\Component\DocumentManager; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* The path builder provides a way to create paths from templates. |
16
|
|
|
*/ |
17
|
|
|
class PathBuilder |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var PathSegmentRegistry |
21
|
|
|
*/ |
22
|
|
|
private $registry; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param PathSegmentRegistry $registry |
26
|
|
|
*/ |
27
|
|
|
public function __construct(PathSegmentRegistry $registry) |
28
|
|
|
{ |
29
|
|
|
$this->registry = $registry; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Build a path from an array of path segments. |
34
|
|
|
* |
35
|
|
|
* Segments demarcated by "%" characters will be interpreted as path |
36
|
|
|
* segment *names* and their value will be resolved from the PathSegmentRegistry. |
37
|
|
|
* |
38
|
|
|
* Other segments will be interpreted literally. |
39
|
|
|
* |
40
|
|
|
* The following: |
41
|
|
|
* |
42
|
|
|
* ```` |
43
|
|
|
* $path = $pathBuilder->build(array('%base%', 'hello', '%articles%')); |
44
|
|
|
* ```` |
45
|
|
|
* |
46
|
|
|
* Will yield `/cms/hello/articleDirectory` where `%base%` is "cms" and |
47
|
|
|
* `%articles` is "articleDirectory" |
48
|
|
|
* |
49
|
|
|
* @see Sulu\Component\DocumentManager\PathSegmentRegistry |
50
|
|
|
* |
51
|
|
|
* @param array $segments |
52
|
|
|
* |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function build(array $segments) |
56
|
|
|
{ |
57
|
|
|
$results = []; |
58
|
|
|
foreach ($segments as $segment) { |
59
|
|
|
$result = $this->buildSegment($segment); |
60
|
|
|
|
61
|
|
|
if (null === $result) { |
62
|
|
|
continue; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$results[] = $result; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
return '/' . implode('/', $results); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $segment |
73
|
|
|
*/ |
74
|
|
|
private function buildSegment($segment) |
75
|
|
|
{ |
76
|
|
|
if (empty($segment) || $segment == '/') { |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
if (substr($segment, 0, 1) == '%') { |
81
|
|
|
if (substr($segment, -1) == '%') { |
82
|
|
|
return $this->registry->getPathSegment(substr($segment, 1, -1)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $segment; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|