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
|
|
|
/** |
18
|
|
|
* @var PharInvocation[] |
19
|
|
|
*/ |
20
|
|
|
private $invocations = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param PharInvocation $invocation |
24
|
|
|
* @return bool |
25
|
|
|
*/ |
26
|
|
|
public function learn(PharInvocation $invocation): bool |
27
|
|
|
{ |
28
|
|
|
if ($this->findFirstByBaseName($invocation->getBaseName()) !== null) { |
29
|
|
|
return false; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
$sameAliasInvocation = $this->findLastByAlias($invocation->getAlias()); |
33
|
|
|
if ($sameAliasInvocation !== null) { |
34
|
|
|
trigger_error( |
35
|
|
|
sprintf( |
36
|
|
|
'Alias %s cannot be used by %s, used already by %s', |
37
|
|
|
$invocation->getAlias(), |
38
|
|
|
$invocation->getBaseName(), |
39
|
|
|
$sameAliasInvocation->getBaseName() |
40
|
|
|
), |
41
|
|
|
E_USER_WARNING |
42
|
|
|
); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$this->invocations[] = $invocation; |
46
|
|
|
return true; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $baseName |
51
|
|
|
* @return null|PharInvocation |
52
|
|
|
*/ |
53
|
|
|
public function findFirstByBaseName(string $baseName) |
54
|
|
|
{ |
55
|
|
|
if ($baseName === '') { |
56
|
|
|
return null; |
57
|
|
|
} |
58
|
|
|
foreach ($this->invocations as $reference) { |
59
|
|
|
if ($reference->getBaseName() === $baseName) { |
60
|
|
|
return $reference; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param string $alias |
68
|
|
|
* @return null|PharInvocation |
69
|
|
|
*/ |
70
|
|
|
public function findLastByAlias(string $alias) |
71
|
|
|
{ |
72
|
|
|
if ($alias === '') { |
73
|
|
|
return null; |
74
|
|
|
} |
75
|
|
|
foreach (array_reverse($this->invocations) as $reference) { |
76
|
|
|
if ($reference->getAlias() === $alias) { |
77
|
|
|
return $reference; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $alias |
85
|
|
|
* @return PharInvocation[] |
86
|
|
|
*/ |
87
|
|
|
public function findAllByAlias(string $alias): array |
88
|
|
|
{ |
89
|
|
|
if ($alias === '') { |
90
|
|
|
return []; |
91
|
|
|
} |
92
|
|
|
return array_filter( |
93
|
|
|
$this->invocations, |
94
|
|
|
function (PharInvocation $reference) use ($alias) { |
95
|
|
|
return $reference->getAlias() === $alias; |
96
|
|
|
} |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|