1
|
|
|
<?php |
2
|
|
|
namespace Consolidation\SiteAlias; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Site Alias manager |
6
|
|
|
*/ |
7
|
|
|
class SiteAliasManager |
8
|
|
|
{ |
9
|
|
|
protected $aliasLoader; |
10
|
|
|
protected $selfAliasRecord; |
11
|
|
|
protected $specParser; |
12
|
|
|
protected $root = ''; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructor for SiteAliasManager |
16
|
|
|
* |
17
|
|
|
* @param SiteAliasFileLoader|null $aliasLoader an alias loader |
18
|
|
|
*/ |
19
|
|
|
public function __construct($aliasLoader = null, $root = '') |
20
|
|
|
{ |
21
|
|
|
$this->aliasLoader = $aliasLoader ?: new SiteAliasFileLoader(); |
22
|
|
|
$this->specParser = new SiteSpecParser(); |
23
|
|
|
$this->selfAliasRecord = new AliasRecord(); |
24
|
|
|
$this->root = $root; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Allow configuration data to be used in replacements in the alias file. |
29
|
|
|
*/ |
30
|
|
|
public function setReferenceData($data) |
31
|
|
|
{ |
32
|
|
|
$this->aliasLoader->setReferenceData($data); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Inject the root of the selected site |
37
|
|
|
* |
38
|
|
|
* @param string $root |
39
|
|
|
* @return $this |
40
|
|
|
*/ |
41
|
|
|
public function setRoot($root) |
42
|
|
|
{ |
43
|
|
|
$this->root = $root; |
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Add a search location to our site alias discovery object. |
49
|
|
|
* |
50
|
|
|
* @param string $path |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function addSearchLocation($path) |
55
|
|
|
{ |
56
|
|
|
$this->aliasLoader->discovery()->addSearchLocation($path); |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Add search locations to our site alias discovery object. |
62
|
|
|
* |
63
|
|
|
* @param array $paths Any path provided in --alias-path option |
64
|
|
|
* or drush.path.alias-path configuration item. |
65
|
|
|
* |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function addSearchLocations(array $paths) |
69
|
|
|
{ |
70
|
|
|
foreach ($paths as $path) { |
71
|
|
|
$this->aliasLoader->discovery()->addSearchLocation($path); |
72
|
|
|
} |
73
|
|
|
return $this; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Return all of the paths where alias files may be found. |
78
|
|
|
* @return string[] |
79
|
|
|
*/ |
80
|
|
|
public function searchLocations() |
81
|
|
|
{ |
82
|
|
|
return $this->aliasLoader->discovery()->searchLocations(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get an alias record by name, or convert a site specification |
87
|
|
|
* into an alias record via the site alias spec parser. If a |
88
|
|
|
* simple alias name is provided (e.g. '@alias'), it is interpreted |
89
|
|
|
* as a sitename, and the default environment for that site is returned. |
90
|
|
|
* |
91
|
|
|
* @param string $name Alias name or site specification |
92
|
|
|
* |
93
|
|
|
* @return AliasRecord|false |
94
|
|
|
*/ |
95
|
|
|
public function get($name) |
96
|
|
|
{ |
97
|
|
|
if (SiteAliasName::isAliasName($name)) { |
|
|
|
|
98
|
|
|
return $this->getAlias($name); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
if ($this->specParser->validSiteSpec($name)) { |
102
|
|
|
return new AliasRecord($this->specParser->parse($name, $this->root), $name); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the '@self' alias record. |
110
|
|
|
* |
111
|
|
|
* @return AliasRecord |
112
|
|
|
*/ |
113
|
|
|
public function getSelf() |
114
|
|
|
{ |
115
|
|
|
return $this->selfAliasRecord; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Force-set the current @self alias. |
120
|
|
|
* |
121
|
|
|
* @param AliasRecord $selfAliasRecord |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
|
|
public function setSelf(AliasRecord $selfAliasRecord) |
125
|
|
|
{ |
126
|
|
|
$this->selfAliasRecord = $selfAliasRecord; |
127
|
|
|
$this->setRoot($selfAliasRecord->localRoot()); |
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get an alias record from a name. Does not accept site specifications. |
133
|
|
|
* |
134
|
|
|
* @param string $aliasName alias name |
135
|
|
|
* |
136
|
|
|
* @return AliasRecord |
137
|
|
|
*/ |
138
|
|
|
public function getAlias($aliasName) |
139
|
|
|
{ |
140
|
|
|
$aliasName = SiteAliasName::parse($aliasName); |
141
|
|
|
|
142
|
|
|
if ($aliasName->isSelf()) { |
143
|
|
|
return $this->getSelf(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if ($aliasName->isNone()) { |
147
|
|
|
return new AliasRecord([], '@none'); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
// Search through all search locations, load |
151
|
|
|
// matching and potentially-matching alias files, |
152
|
|
|
// and return the alias matching the provided name. |
153
|
|
|
return $this->aliasLoader->load($aliasName); |
|
|
|
|
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Given a simple alias name, e.g. '@alias', returns all of the |
158
|
|
|
* environments in the specified site. |
159
|
|
|
* |
160
|
|
|
* If the provided name is a site specification et. al., |
161
|
|
|
* then this method will return 'false'. |
162
|
|
|
* |
163
|
|
|
* @param string $name Alias name or site specification |
164
|
|
|
* @return AliasRecord[]|false |
165
|
|
|
*/ |
166
|
|
|
public function getMultiple($name) |
167
|
|
|
{ |
168
|
|
|
if (empty($name)) { |
169
|
|
|
return $this->aliasLoader->loadAll(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (!SiteAliasName::isAliasName($name)) { |
|
|
|
|
173
|
|
|
return false; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// Trim off the '@' and load all that match |
177
|
|
|
$result = $this->aliasLoader->loadMultiple(ltrim($name, '@')); |
178
|
|
|
|
179
|
|
|
// Special checking for @self |
180
|
|
|
if ($name == '@self') { |
181
|
|
|
$self = $this->getSelf(); |
182
|
|
|
$result = array_merge( |
183
|
|
|
['@self' => $self], |
184
|
|
|
$result |
185
|
|
|
); |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
return $result; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Return the paths to all alias files in all search locations known |
193
|
|
|
* to the alias manager. |
194
|
|
|
* |
195
|
|
|
* @return string[] |
196
|
|
|
*/ |
197
|
|
|
public function listAllFilePaths() |
198
|
|
|
{ |
199
|
|
|
return $this->aliasLoader->listAll(); |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: