2 namespace Drush\SiteAlias;
4 use Consolidation\Config\Loader\ConfigProcessor;
5 use Dflydev\DotAccessData\Util as DotAccessDataUtil;
6 use Drush\Internal\Config\Yaml\Yaml;
9 * Discover alias files:
11 * - sitename.site.yml: contains multiple aliases, one for each of the
12 * environments of 'sitename'.
14 class SiteAliasFileLoader
17 * @var SiteAliasFileDiscovery
24 protected $referenceData;
27 * SiteAliasFileLoader constructor
29 * @param SiteAliasFileDiscovery|null $discovery
31 public function __construct($discovery = null)
33 $this->discovery = $discovery ?: new SiteAliasFileDiscovery();
37 * Allow configuration data to be used in replacements in the alias file.
39 public function setReferenceData($data)
41 $this->referenceData = $data;
45 * Add a search location to our discovery object.
51 public function addSearchLocation($path)
53 $this->discovery()->addSearchLocation($path);
58 * Return our discovery object.
60 * @return SiteAliasFileDiscovery
62 public function discovery()
64 return $this->discovery;
68 * Load the file containing the specified alias name.
70 * @param SiteAliasName $aliasName
72 * @return AliasRecord|false
74 public function load(SiteAliasName $aliasName)
76 // First attempt to load a sitename.site.yml file for the alias.
77 $aliasRecord = $this->loadSingleAliasFile($aliasName);
82 // If aliasname was provides as @site.env and we did not find it,
84 if ($aliasName->hasSitename()) {
88 // If $aliasName was provided as `@foo` and defaulted to `@self.foo`,
89 // then make a new alias name `@foo.default` and see if we can find that.
90 // Note that at the moment, `foo` is stored in $aliasName->env().
91 $sitename = $aliasName->env();
92 return $this->loadDefaultEnvFromSitename($sitename);
96 * Given only a site name, load the default environment from it.
98 protected function loadDefaultEnvFromSitename($sitename)
100 $path = $this->discovery()->findSingleSiteAliasFile($sitename);
104 $data = $this->loadSiteDataFromPath($path);
108 $env = $this->getDefaultEnvironmentName($data);
110 $aliasName = new SiteAliasName($sitename, $env);
111 $processor = new ConfigProcessor();
112 return $this->fetchAliasRecordFromSiteAliasData($aliasName, $processor, $data);
116 * Return a list of all site aliases loadable from any findable path.
118 * @return AliasRecord[]
120 public function loadAll()
123 $paths = $this->discovery()->findAllSingleAliasFiles();
124 foreach ($paths as $path) {
125 $aliasRecords = $this->loadSingleSiteAliasFileAtPath($path);
126 foreach ($aliasRecords as $aliasRecord) {
127 $this->storeAliasRecordInResut($result, $aliasRecord);
135 * Return a list of all available alias files. Does not include
140 public function listAll()
142 return $this->discovery()->findAllSingleAliasFiles();
146 * Given an alias name that might represent multiple sites,
147 * return a list of all matching alias records. If nothing was found,
148 * or the name represents a single site + env, then we take
149 * no action and return `false`.
151 * @param string $sitename The site name to return all environments for.
152 * @return AliasRecord[]|false
154 public function loadMultiple($sitename)
156 if ($path = $this->discovery()->findSingleSiteAliasFile($sitename)) {
157 if ($siteData = $this->loadSiteDataFromPath($path)) {
158 // Convert the raw array into a list of alias records.
159 return $this->createAliasRecordsFromSiteData($sitename, $siteData);
166 * @param array $siteData list of sites with its respective data
168 * @param SiteAliasName $aliasName The name of the record being created
169 * @param $siteData An associative array of envrionment => site data
170 * @return AliasRecord[]
172 protected function createAliasRecordsFromSiteData($sitename, $siteData)
175 if (!is_array($siteData) || empty($siteData)) {
178 foreach ($siteData as $envName => $data) {
179 if (is_array($data)) {
180 $aliasName = new SiteAliasName($sitename, $envName);
182 $processor = new ConfigProcessor();
183 $oneRecord = $this->fetchAliasRecordFromSiteAliasData($aliasName, $processor, $siteData);
184 $this->storeAliasRecordInResut($result, $oneRecord);
191 * Store an alias record in a list. If the alias record has
192 * a known name, then the key of the list will be the record's name.
193 * Otherwise, append the record to the end of the list with
196 * @param &AliasRecord[] $result list of alias records
197 * @param AliasRecord $aliasRecord one more alias to store in the result
199 protected function storeAliasRecordInResut(&$result, AliasRecord $aliasRecord)
204 $key = $aliasRecord->name();
206 $result[] = $aliasRecord;
209 $result[$key] = $aliasRecord;
213 * If the alias name is '@sitename', or if it is '@sitename.env', then
214 * look for a sitename.site.yml file that contains it.
216 * @param SiteAliasName $aliasName
218 * @return AliasRecord|false
220 protected function loadSingleAliasFile(SiteAliasName $aliasName)
222 // Check to see if the appropriate sitename.alias.yml file can be
223 // found. Return if it cannot.
224 $path = $this->discovery()->findSingleSiteAliasFile($aliasName->sitename());
228 return $this->loadSingleAliasFileWithNameAtPath($aliasName, $path);
232 * Given only the path to an alias file `site.alias.yml`, return all
233 * of the alias records for every environment stored in that file.
235 * @param string $path
236 * @return AliasRecord[]
238 protected function loadSingleSiteAliasFileAtPath($path)
240 $sitename = $this->siteNameFromPath($path);
241 $siteData = $this->loadSiteDataFromPath($path);
242 return $this->createAliasRecordsFromSiteData($sitename, $siteData);
246 * Given the path to a single site alias file `site.alias.yml`,
247 * return the `site` part.
249 * @param string $path
251 protected function siteNameFromPath($path)
253 return $this->basenameWithoutExtension($path, '.site.yml');
257 * Chop off the `aliases.yml` or `alias.yml` part of a path. This works
258 * just like `basename`, except it will throw if the provided path
259 * does not end in the specified extension.
261 * @param string $path
262 * @param string $extension
266 protected function basenameWithoutExtension($path, $extension)
268 $result = basename($path, $extension);
269 // It is an error if $path does not end with site.yml
270 if ($result == basename($path)) {
271 throw new \Exception("$path must end with '$extension'");
277 * Given an alias name and a path, load the data from the path
278 * and process it as needed to generate the alias record.
280 * @param SiteAliasName $aliasName
281 * @param string $path
282 * @return AliasRecord|false
284 protected function loadSingleAliasFileWithNameAtPath(SiteAliasName $aliasName, $path)
286 $data = $this->loadSiteDataFromPath($path);
290 $processor = new ConfigProcessor();
291 return $this->fetchAliasRecordFromSiteAliasData($aliasName, $processor, $data);
295 * Load the yml from the given path
297 * @param string $path
300 protected function loadSiteDataFromPath($path)
302 $data = $this->loadYml($path);
306 $selfSiteAliases = $this->findSelfSiteAliases($data);
307 $data = array_merge($data, $selfSiteAliases);
312 * Given an array of site aliases, find the first one that is
313 * local (has no 'host' item) and also contains a 'self.site.yml' file.
317 protected function findSelfSiteAliases($site_aliases)
319 foreach ($site_aliases as $site => $data) {
320 if (!isset($data['host']) && isset($data['root'])) {
321 foreach (['.', '..'] as $relative_path) {
322 $candidate = $data['root'] . '/' . $relative_path . '/drush/sites/self.site.yml';
323 if (file_exists($candidate)) {
324 return $this->loadYml($candidate);
333 * Load the yaml contents of the specified file.
335 * @param string $path Path to file to load
338 protected function loadYml($path)
343 // TODO: Perhaps cache these alias files, as they may be read multiple times.
344 // TODO: Maybe use a YamlConfigLoader?
345 return (array) Yaml::parse(file_get_contents($path));
349 * Given an array containing site alias data, return an alias record
350 * containing the data for the requested record. If there is a 'common'
351 * section, then merge that in as well.
353 * @param SiteAliasName $aliasName the alias we are loading
356 * @return AliasRecord|false
358 protected function fetchAliasRecordFromSiteAliasData(SiteAliasName $aliasName, ConfigProcessor $processor, array $data)
360 $data = $this->adjustIfSingleAlias($data);
361 $env = $this->getEnvironmentName($aliasName, $data);
362 if (!$this->siteEnvExists($data, $env)) {
366 // Add the 'common' section if it exists.
367 if (isset($data['common']) && is_array($data['common'])) {
368 $processor->add($data['common']);
371 // Then add the data from the desired environment.
372 $processor->add($data[$env]);
374 // Export the combined data and create an AliasRecord object to manage it.
375 return new AliasRecord($processor->export($this->referenceData), '@' . $aliasName->sitename(), $env);
379 * Determine whether there is a valid-looking environment '$env' in the
380 * provided site alias data.
386 protected function siteEnvExists(array $data, $env)
390 isset($data[$env]) &&
391 is_array($data[$env])
396 * Adjust the alias data for a single-site alias. Usually, a .yml alias
397 * file will contain multiple entries, one for each of the environments
398 * of an alias. If there are no environments
403 protected function adjustIfSingleAlias($data)
405 if (!$this->detectSingleAlias($data)) {
417 * A single-environment alias looks something like this:
420 * root: /path/to/drupal
421 * uri: https://mysite.org
423 * A multiple-environment alias looks something like this:
429 * uri: https://dev.mysite.org
431 * root: /path/to/stage
432 * uri: https://stage.mysite.org
434 * The differentiator between these two is that the multi-environment
435 * alias always has top-level elements that are associative arrays, and
436 * the single-environment alias never does.
441 protected function detectSingleAlias($data)
443 foreach ($data as $key => $value) {
444 if (is_array($value) && DotAccessDataUtil::isAssoc($value)) {
452 * Return the name of the environment requested.
454 * @param SiteAliasName $aliasName the alias we are loading
459 protected function getEnvironmentName(SiteAliasName $aliasName, array $data)
461 // If the alias name specifically mentions the environment
462 // to use, then return it.
463 if ($aliasName->hasEnv()) {
464 return $aliasName->env();
466 return $this->getDefaultEnvironmentName($data);
470 * Given a data array containing site alias environments, determine which
471 * envirionmnet should be used as the default environment.
476 protected function getDefaultEnvironmentName(array $data)
478 // If there is an entry named 'default', it will either contain the
479 // name of the environment to use by default, or it will itself be
480 // the default environment.
481 if (isset($data['default'])) {
482 return is_array($data['default']) ? 'default' : $data['default'];
484 // If there is an environment named 'dev', it will be our default.
485 if (isset($data['dev'])) {
488 // If we don't know which environment to use, just take the first one.
489 $keys = array_keys($data);