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
|
|
|
use TYPO3\PharStreamWrapper\Collectable; |
16
|
|
|
|
17
|
|
|
class PharInvocationCollection implements Collectable |
18
|
|
|
{ |
19
|
|
|
const UNIQUE_INVOCATION = 1; |
20
|
|
|
const UNIQUE_BASE_NAME = 2; |
21
|
|
|
const DUPLICATE_ALIAS_WARNING = 32; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var PharInvocation[] |
25
|
|
|
*/ |
26
|
|
|
private $invocations = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param PharInvocation $invocation |
30
|
|
|
* @return bool |
31
|
|
|
*/ |
32
|
|
|
public function has(PharInvocation $invocation): bool |
33
|
|
|
{ |
34
|
|
|
return in_array($invocation, $this->invocations, true); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param PharInvocation $invocation |
39
|
|
|
* @param null|int $flags |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function collect(PharInvocation $invocation, int $flags = null): bool |
43
|
|
|
{ |
44
|
|
|
if ($flags === null) { |
45
|
|
|
$flags = static::UNIQUE_INVOCATION | static::DUPLICATE_ALIAS_WARNING; |
46
|
|
|
} |
47
|
|
|
if ($invocation->getBaseName() === '' |
48
|
|
|
|| $invocation->getAlias() === '' |
49
|
|
|
|| !$this->assertUniqueBaseName($invocation, $flags) |
50
|
|
|
|| !$this->assertUniqueInvocation($invocation, $flags) |
51
|
|
|
) { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
if ($flags & static::DUPLICATE_ALIAS_WARNING) { |
55
|
|
|
$this->triggerDuplicateAliasWarning($invocation); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$this->invocations[] = $invocation; |
59
|
|
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param callable $callback |
64
|
|
|
* @param bool $reverse |
65
|
|
|
* @return null|PharInvocation |
66
|
|
|
*/ |
67
|
|
|
public function findByCallback(callable $callback, $reverse = false) |
68
|
|
|
{ |
69
|
|
|
foreach ($this->getInvocations($reverse) as $invocation) { |
70
|
|
|
if (call_user_func($callback, $invocation) === true) { |
71
|
|
|
return $invocation; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Asserts that base-name is unique. This disallows having multiple invocations for |
79
|
|
|
* same base-name but having different alias names. |
80
|
|
|
* |
81
|
|
|
* @param PharInvocation $invocation |
82
|
|
|
* @param int $flags |
83
|
|
|
* @return bool |
84
|
|
|
*/ |
85
|
|
|
private function assertUniqueBaseName(PharInvocation $invocation, int $flags): bool |
86
|
|
|
{ |
87
|
|
|
if (!($flags & static::UNIQUE_BASE_NAME)) { |
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
return $this->findByCallback( |
91
|
|
|
function (PharInvocation $candidate) use ($invocation) { |
92
|
|
|
return $candidate->getBaseName() === $invocation->getBaseName(); |
93
|
|
|
} |
94
|
|
|
) === null; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Asserts that combination of base-name and alias is unique. This allows having multiple |
99
|
|
|
* invocations for same base-name but having different alias names (for whatever reason). |
100
|
|
|
* |
101
|
|
|
* @param PharInvocation $invocation |
102
|
|
|
* @param int $flags |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
|
|
private function assertUniqueInvocation(PharInvocation $invocation, int $flags): bool |
106
|
|
|
{ |
107
|
|
|
if (!($flags & static::UNIQUE_INVOCATION)) { |
108
|
|
|
return true; |
109
|
|
|
} |
110
|
|
|
return $this->findByCallback( |
111
|
|
|
function (PharInvocation $candidate) use ($invocation) { |
112
|
|
|
return $candidate->equals($invocation); |
113
|
|
|
} |
114
|
|
|
) === null; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Triggers warning for invocations with same alias and same confirmation state. |
119
|
|
|
* |
120
|
|
|
* @param PharInvocation $invocation |
121
|
|
|
* @see \TYPO3\PharStreamWrapper\PharStreamWrapper::collectInvocation() |
122
|
|
|
*/ |
123
|
|
|
private function triggerDuplicateAliasWarning(PharInvocation $invocation) |
124
|
|
|
{ |
125
|
|
|
$sameAliasInvocation = $this->findByCallback( |
126
|
|
|
function (PharInvocation $candidate) use ($invocation) { |
127
|
|
|
return $candidate->isConfirmed() === $invocation->isConfirmed() |
128
|
|
|
&& $candidate->getAlias() === $invocation->getAlias(); |
129
|
|
|
}, |
130
|
|
|
true |
131
|
|
|
); |
132
|
|
|
if ($sameAliasInvocation === null) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
trigger_error( |
136
|
|
|
sprintf( |
137
|
|
|
'Alias %s cannot be used by %s, already used by %s', |
138
|
|
|
$invocation->getAlias(), |
139
|
|
|
$invocation->getBaseName(), |
140
|
|
|
$sameAliasInvocation->getBaseName() |
141
|
|
|
), |
142
|
|
|
E_USER_WARNING |
143
|
|
|
); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @param bool $reverse |
148
|
|
|
* @return PharInvocation[] |
149
|
|
|
*/ |
150
|
|
|
private function getInvocations(bool $reverse = false): array |
151
|
|
|
{ |
152
|
|
|
if ($reverse) { |
153
|
|
|
return array_reverse($this->invocations); |
154
|
|
|
} |
155
|
|
|
return $this->invocations; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|