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