1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
namespace TYPO3\PharStreamWrapper\Resolver; |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the TYPO3 project. |
7
|
|
|
* |
8
|
|
|
* It is free software; you can redistribute it and/or modify it under the terms |
9
|
|
|
* of the MIT License (MIT). For the full copyright and license information, |
10
|
|
|
* please read the LICENSE file that was distributed with this source code. |
11
|
|
|
* |
12
|
|
|
* The TYPO3 project - inspiring people to share! |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
class PharInvocationStack |
16
|
|
|
{ |
17
|
|
|
const UNIQUE_INVOCATION = 1; |
18
|
|
|
const UNIQUE_BASE_NAME = 2; |
19
|
|
|
const DUPLICATE_ALIAS_WARNING = 32; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var PharInvocation[] |
23
|
|
|
*/ |
24
|
|
|
private $invocations = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param PharInvocation $invocation |
28
|
|
|
* @param null|int $flags |
29
|
|
|
* @return bool |
30
|
|
|
*/ |
31
|
|
|
public function learn(PharInvocation $invocation, int $flags = null): bool |
32
|
|
|
{ |
33
|
|
|
if ($flags === null) { |
34
|
|
|
$flags = static::UNIQUE_INVOCATION | static::DUPLICATE_ALIAS_WARNING; |
35
|
|
|
} |
36
|
|
|
if ($invocation->getAlias() === '' |
37
|
|
|
|| !$this->assertUniqueBaseName($invocation, $flags) |
38
|
|
|
|| !$this->assertUniqueInvocation($invocation, $flags) |
39
|
|
|
) { |
40
|
|
|
return false; |
41
|
|
|
} |
42
|
|
|
if ($flags & static::DUPLICATE_ALIAS_WARNING) { |
43
|
|
|
$this->triggerDuplicateAliasWarning($invocation); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$this->invocations[] = $invocation; |
47
|
|
|
return true; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $baseName |
52
|
|
|
* @param string $alias |
53
|
|
|
* @param bool $reverse |
54
|
|
|
* @return null|PharInvocation |
55
|
|
|
*/ |
56
|
|
|
public function findByBaseNameAndAlias(string $baseName, string $alias, bool $reverse = false) |
57
|
|
|
{ |
58
|
|
|
if ($baseName === '' || $alias === '') { |
59
|
|
|
return null; |
60
|
|
|
} |
61
|
|
|
foreach ($this->getInvocations($reverse) as $invocation) { |
62
|
|
|
if ($invocation->getBaseName() === $baseName && $invocation->getAlias() === $alias) { |
63
|
|
|
return $invocation; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
return null; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $baseName |
71
|
|
|
* @param bool $reverse |
72
|
|
|
* @return null|PharInvocation |
73
|
|
|
*/ |
74
|
|
|
public function findByBaseName(string $baseName, bool $reverse = false) |
75
|
|
|
{ |
76
|
|
|
if ($baseName === '') { |
77
|
|
|
return null; |
78
|
|
|
} |
79
|
|
|
foreach ($this->getInvocations($reverse) as $invocation) { |
80
|
|
|
if ($invocation->getBaseName() === $baseName) { |
81
|
|
|
return $invocation; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return null; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $alias |
89
|
|
|
* @param bool $reverse |
90
|
|
|
* @return null|PharInvocation |
91
|
|
|
*/ |
92
|
|
|
public function findByAlias(string $alias, bool $reverse = false) |
93
|
|
|
{ |
94
|
|
|
if ($alias === '') { |
95
|
|
|
return null; |
96
|
|
|
} |
97
|
|
|
foreach ($this->getInvocations($reverse) as $invocation) { |
98
|
|
|
if ($invocation->getAlias() === $alias) { |
99
|
|
|
return $invocation; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
return null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Asserts that base-name is unique. This disallows having multiple invocations for |
107
|
|
|
* same base-name but having different alias names. |
108
|
|
|
* |
109
|
|
|
* @param PharInvocation $invocation |
110
|
|
|
* @param int $flags |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
private function assertUniqueBaseName(PharInvocation $invocation, int $flags): bool |
114
|
|
|
{ |
115
|
|
|
if ($flags ^ static::UNIQUE_BASE_NAME) { |
116
|
|
|
return true; |
117
|
|
|
} |
118
|
|
|
return $this->findByBaseName($invocation->getBaseName()) !== null; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Asserts that combination of base-name and alias is unique. This allows having multiple |
123
|
|
|
* invocations for same base-name but having different alias names (for whatever reason). |
124
|
|
|
* |
125
|
|
|
* @param PharInvocation $invocation |
126
|
|
|
* @param int $flags |
127
|
|
|
* @return bool |
128
|
|
|
*/ |
129
|
|
|
private function assertUniqueInvocation(PharInvocation $invocation, int $flags): bool |
130
|
|
|
{ |
131
|
|
|
if ($flags ^ static::UNIQUE_INVOCATION) { |
132
|
|
|
return true; |
133
|
|
|
} |
134
|
|
|
return $this->findByBaseNameAndAlias($invocation->getBaseName(), $invocation->getAlias()) !== null; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param PharInvocation $invocation |
139
|
|
|
*/ |
140
|
|
|
private function triggerDuplicateAliasWarning(PharInvocation $invocation) |
141
|
|
|
{ |
142
|
|
|
$sameAliasInvocation = $this->findByAlias($invocation->getAlias(), true); |
143
|
|
|
if ($sameAliasInvocation !== null) { |
144
|
|
|
trigger_error( |
145
|
|
|
sprintf( |
146
|
|
|
'Alias %s cannot be used by %s, used already by %s', |
147
|
|
|
$invocation->getAlias(), |
148
|
|
|
$invocation->getBaseName(), |
149
|
|
|
$sameAliasInvocation->getBaseName() |
150
|
|
|
), |
151
|
|
|
E_USER_WARNING |
152
|
|
|
); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param bool $reverse |
158
|
|
|
* @return PharInvocation[] |
159
|
|
|
*/ |
160
|
|
|
private function getInvocations(bool $reverse = false): array |
161
|
|
|
{ |
162
|
|
|
if ($reverse) { |
163
|
|
|
return array_reverse($this->invocations); |
164
|
|
|
} |
165
|
|
|
return $this->invocations; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|