|
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 AliasMap |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var AliasReference[] |
|
19
|
|
|
*/ |
|
20
|
|
|
private $references = []; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param string $baseName |
|
24
|
|
|
* @param string $alias |
|
25
|
|
|
* @return bool |
|
26
|
|
|
*/ |
|
27
|
|
|
public function append(string $baseName, string $alias): bool |
|
28
|
|
|
{ |
|
29
|
|
|
if ($baseName === '' || $alias === '') { |
|
30
|
|
|
return false; |
|
31
|
|
|
} |
|
32
|
|
|
if ($this->findFirstByBaseName($baseName) !== null) { |
|
33
|
|
|
return false; |
|
34
|
|
|
} |
|
35
|
|
|
$this->references[] = new AliasReference($baseName, $alias); |
|
36
|
|
|
return true; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $baseName |
|
41
|
|
|
* @return bool |
|
42
|
|
|
*/ |
|
43
|
|
|
public function purgeByBaseName(string $baseName): bool |
|
44
|
|
|
{ |
|
45
|
|
|
if ($baseName === '') { |
|
46
|
|
|
return false; |
|
47
|
|
|
} |
|
48
|
|
|
$count = count($this->references); |
|
49
|
|
|
$this->references = array_filter( |
|
50
|
|
|
$this->references, |
|
51
|
|
|
function (AliasReference $reference) use ($baseName) { |
|
52
|
|
|
return $reference->getBaseName() !== $baseName; |
|
53
|
|
|
} |
|
54
|
|
|
); |
|
55
|
|
|
return count($this->references) < $count; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $baseName |
|
60
|
|
|
* @return null|AliasReference |
|
61
|
|
|
*/ |
|
62
|
|
|
public function findFirstByBaseName(string $baseName) |
|
63
|
|
|
{ |
|
64
|
|
|
if ($baseName === '') { |
|
65
|
|
|
return null; |
|
66
|
|
|
} |
|
67
|
|
|
foreach (array_reverse($this->references) as $reference) { |
|
68
|
|
|
if ($reference->getBaseName() === $baseName) { |
|
69
|
|
|
return $reference; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
return null; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $alias |
|
77
|
|
|
* @return null|AliasReference |
|
78
|
|
|
*/ |
|
79
|
|
|
public function findLastByAlias(string $alias) |
|
80
|
|
|
{ |
|
81
|
|
|
if ($alias === '') { |
|
82
|
|
|
return null; |
|
83
|
|
|
} |
|
84
|
|
|
foreach (array_reverse($this->references) as $reference) { |
|
85
|
|
|
if ($reference->getAlias() === $alias) { |
|
86
|
|
|
return $reference; |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @param string $alias |
|
94
|
|
|
* @return AliasReference[] |
|
95
|
|
|
*/ |
|
96
|
|
|
public function findAllByAlias(string $alias): array |
|
97
|
|
|
{ |
|
98
|
|
|
if ($alias === '') { |
|
99
|
|
|
return []; |
|
100
|
|
|
} |
|
101
|
|
|
return array_filter( |
|
102
|
|
|
$this->references, |
|
103
|
|
|
function (AliasReference $reference) use ($alias) { |
|
104
|
|
|
return $reference->getAlias() === $alias; |
|
105
|
|
|
} |
|
106
|
|
|
); |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|