|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Groundskeeper\Tokens; |
|
4
|
|
|
|
|
5
|
|
|
use Groundskeeper\Configuration; |
|
6
|
|
|
use Groundskeeper\Exceptions\ValidationException; |
|
7
|
|
|
use Groundskeeper\Tokens\Elements\Element; |
|
|
|
|
|
|
8
|
|
|
use Psr\Log\LoggerInterface; |
|
9
|
|
|
|
|
10
|
|
|
final class TokenContainer implements Cleanable, ContainsChildren, Removable |
|
11
|
|
|
{ |
|
12
|
|
|
/** @var array[Token] */ |
|
13
|
|
|
private $children; |
|
14
|
|
|
|
|
15
|
|
|
/** @var Configuration */ |
|
16
|
|
|
private $configuration; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Constructor |
|
20
|
|
|
*/ |
|
21
|
51 |
|
public function __construct(Configuration $configuration) |
|
22
|
|
|
{ |
|
23
|
51 |
|
$this->children = array(); |
|
24
|
51 |
|
$this->configuration = $configuration; |
|
25
|
51 |
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Required by ContainsChildren interface. |
|
29
|
|
|
*/ |
|
30
|
51 |
|
public function getChildren() |
|
31
|
|
|
{ |
|
32
|
51 |
|
return $this->children; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Required by ContainsChildren interface. |
|
37
|
|
|
*/ |
|
38
|
1 |
|
public function hasChild(Token $token) |
|
39
|
|
|
{ |
|
40
|
1 |
|
return array_search($token, $this->children, true) !== false; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Required by ContainsChildren interface. |
|
45
|
|
|
*/ |
|
46
|
50 |
|
public function appendChild(Token $token) |
|
47
|
|
|
{ |
|
48
|
50 |
|
$this->children[] = $token; |
|
49
|
|
|
|
|
50
|
50 |
|
return $this; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Required by ContainsChildren interface. |
|
55
|
|
|
*/ |
|
56
|
1 |
|
public function prependChild(Token $token) |
|
57
|
|
|
{ |
|
58
|
1 |
|
array_unshift($this->children, $token); |
|
59
|
|
|
|
|
60
|
1 |
|
return $this; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Required by ContainsChildren interface. |
|
65
|
|
|
*/ |
|
66
|
5 |
View Code Duplication |
public function removeChild(Token $token) |
|
|
|
|
|
|
67
|
|
|
{ |
|
68
|
5 |
|
$key = array_search($token, $this->children, true); |
|
69
|
5 |
|
if ($key !== false) { |
|
70
|
5 |
|
unset($this->children[$key]); |
|
71
|
|
|
|
|
72
|
5 |
|
return true; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
return false; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Required by Cleanable interface. |
|
80
|
|
|
*/ |
|
81
|
43 |
|
public function clean(LoggerInterface $logger) |
|
82
|
|
|
{ |
|
83
|
43 |
|
return AbstractToken::cleanChildTokens( |
|
84
|
43 |
|
$this->configuration, |
|
85
|
43 |
|
$this->children, |
|
86
|
|
|
$logger |
|
87
|
43 |
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Required by the Removable interface. |
|
92
|
|
|
*/ |
|
93
|
42 |
View Code Duplication |
public function remove(LoggerInterface $logger) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
42 |
|
$hasRemovableElements = $this->configuration->get('element-blacklist') != ''; |
|
96
|
42 |
|
$hasRemovableTypes = $this->configuration->get('type-blacklist') != ''; |
|
97
|
42 |
|
foreach ($this->children as $child) { |
|
98
|
|
|
// Check types. |
|
99
|
42 |
|
if ($hasRemovableTypes && |
|
100
|
42 |
|
!$this->configuration->isAllowedType($child->getType())) { |
|
101
|
2 |
|
$logger->debug('Removing ' . $child); |
|
102
|
2 |
|
$this->removeChild($child); |
|
103
|
2 |
|
|
|
104
|
|
|
continue; |
|
105
|
2 |
|
} |
|
106
|
2 |
|
|
|
107
|
|
|
// Check elements. |
|
108
|
|
|
if ($hasRemovableElements && |
|
109
|
|
|
$child->getType() == Token::ELEMENT && |
|
110
|
42 |
|
!$this->configuration->isAllowedElement($child->getName())) { |
|
111
|
42 |
|
$logger->debug('Removing ' . $child); |
|
112
|
42 |
|
$this->removeChild($child); |
|
113
|
2 |
|
|
|
114
|
2 |
|
continue; |
|
115
|
2 |
|
} |
|
116
|
|
|
|
|
117
|
2 |
|
// Check children. |
|
118
|
2 |
|
if ($child instanceof Removable) { |
|
119
|
|
|
$child->remove($logger); |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
40 |
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: