forked from drush-ops/drush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PreflightSiteLocator.php
131 lines (115 loc) · 4.38 KB
/
PreflightSiteLocator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
namespace Drush\Preflight;
use Drush\Config\Environment;
use Drush\Preflight\PreflightArgsInterface;
use Consolidation\SiteAlias\SiteAlias;
use Consolidation\SiteAlias\SiteAliasManager;
use Consolidation\SiteAlias\SiteAliasName;
use Consolidation\SiteAlias\SiteSpecParser;
class PreflightSiteLocator
{
/**
* @var SiteAliasManager
*/
protected $siteAliasManager;
public function __construct(SiteAliasManager $siteAliasManager)
{
$this->siteAliasManager = $siteAliasManager;
}
/**
* During bootstrap, finds the currently selected site from the parameters
* provided on the commandline.
*
* If 'false' is returned, that indicates that there was an alias name
* provided on the commandline that is either missing or invalid.
*
* @param PreflightArgsInterface $preflightArgs An alias name or site specification
* @param Environment $environment
* @param string $root The default Drupal root (from site:set, --root or cwd)
*
* @return SiteAlias|false
*/
public function findSite(PreflightArgsInterface $preflightArgs, Environment $environment, string $root)
{
$self = $this->determineSelf($preflightArgs, $environment, $root);
// If the user provided a uri on the commandline, inject it
// into the alias that we found.
if ($preflightArgs->hasUri()) {
$self->setUri($preflightArgs->uri());
}
return $self;
}
/**
* Either look up the specified alias name / site spec,
* or, if those are invalid, then generate one from
* the provided root and URI.
*
* @param PreflightArgsInterface $preflightArgs
* @param Environment $environment
* @param $root
*
* @return SiteAlias|false
*/
protected function determineSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
{
if ($preflightArgs->hasAlias()) {
$aliasName = $preflightArgs->alias();
// If the user specified an @alias, that takes precedence.
if (SiteAliasName::isAliasName($aliasName)) {
// TODO: Should we do something about `@self` here? At the moment that will cause getAlias to
// call getSelf(), but we haven't built @self yet.
return $this->siteAliasManager->getAlias($aliasName);
}
// Ditto for a site spec (/path/to/drupal#uri)
$specParser = new SiteSpecParser();
if ($specParser->validSiteSpec($aliasName)) {
return new SiteAlias($specParser->parse($aliasName, $root), $aliasName);
}
}
// If the user provides the --root parameter then we don't want to use
// the site-set alias.
$selectedRoot = $preflightArgs->selectedSite();
if (!$selectedRoot) {
$aliasName = $environment->getSiteSetAliasName();
if (!empty($aliasName)) {
$alias = $this->siteAliasManager->getAlias($aliasName);
if ($alias) {
return $alias;
}
}
}
return $this->buildSelf($preflightArgs, $root);
}
/**
* Generate @self from the provided root and URI.
*
* @param PreflightArgsInterface $preflightArgs
* @param $root
*/
protected function buildSelf(PreflightArgsInterface $preflightArgs, $root): SiteAlias
{
// If there is no root, then return '@none'
if (!$root) {
return new SiteAlias([], '@none');
}
// If there is no URI specified, we will allow it to
// remain empty for now. We will refine it later via
// Application::refineUriSelection(), which is called
// in Preflight::doRun(). This method will set it to
// 'default' if no better directory can be devined.
// Create the 'self' alias record. Note that the self
// record will be named '@self' if it is manually constructed
// here, and will otherwise have the name of the
// alias or site specification used by the user. Also note that if we
// pass in a falsy uri the drush config (i.e drush.yml) can not override
// it.
$uri = $preflightArgs->uri();
$data = [
'root' => $root,
];
if ($uri) {
$data['uri'] = $uri;
}
return new SiteAlias($data, '@self');
}
}