3 namespace Drush\Preflight;
5 use Drush\Config\Environment;
6 use Drush\Preflight\PreflightArgsInterface;
7 use Consolidation\SiteAlias\AliasRecord;
8 use Consolidation\SiteAlias\SiteAliasManager;
9 use Consolidation\SiteAlias\SiteAliasName;
10 use Consolidation\SiteAlias\SiteSpecParser;
12 class PreflightSiteLocator
15 * @var SiteAliasManager
17 protected $siteAliasManager;
19 public function __construct(SiteAliasManager $siteAliasManager)
21 $this->siteAliasManager = $siteAliasManager;
25 * During bootstrap, finds the currently selected site from the parameters
26 * provided on the commandline.
28 * If 'false' is returned, that indicates that there was an alias name
29 * provided on the commandline that is either missing or invalid.
31 * @param PreflightArgsInterface $preflightArgs An alias name or site specification
32 * @param \Drush\Config\Environment $environment
33 * @param string $root The default Drupal root (from site:set, --root or cwd)
35 * @return \Consolidation\SiteAlias\AliasRecord|false
37 public function findSite(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
39 $aliasName = $preflightArgs->alias();
40 return $this->determineSelf($preflightArgs, $environment, $root);
44 * Either look up the specified alias name / site spec,
45 * or, if those are invalid, then generate one from
46 * the provided root and URI.
48 * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
49 * @param \Drush\Config\Environment $environment
52 * @return \Consolidation\SiteAlias\AliasRecord
54 protected function determineSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
56 $aliasName = $preflightArgs->alias();
58 // If the user specified an @alias, that takes precidence.
59 if (SiteAliasName::isAliasName($aliasName)) {
60 // TODO: Should we do something about `@self` here? At the moment that will cause getAlias to
61 // call getSelf(), but we haven't built @self yet.
62 return $this->siteAliasManager->getAlias($aliasName);
65 // Ditto for a site spec (/path/to/drupal#uri)
66 $specParser = new SiteSpecParser();
67 if ($specParser->validSiteSpec($aliasName)) {
68 return new AliasRecord($specParser->parse($aliasName, $root), $aliasName);
71 // If the user provides the --root parameter then we don't want to use
72 // the site-set alias.
73 $selectedRoot = $preflightArgs->selectedSite();
75 $aliasName = $environment->getSiteSetAliasName();
76 if (!empty($aliasName)) {
77 $alias = $this->siteAliasManager->getAlias($aliasName);
84 return $this->buildSelf($preflightArgs, $root);
88 * Generate @self from the provided root and URI.
90 * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
93 * @return \Consolidation\SiteAlias\AliasRecord
95 protected function buildSelf(PreflightArgsInterface $preflightArgs, $root)
97 // If there is no root, then return '@none'
99 return new AliasRecord([], '@none');
102 // If there is no URI specified, we will allow it to
103 // remain empty for now. We will refine it later via
104 // Application::refineUriSelection(), which is called
105 // in Preflight::doRun(). This method will set it to
106 // 'default' if no better directory can be devined.
108 // Create the 'self' alias record. Note that the self
109 // record will be named '@self' if it is manually constructed
110 // here, and will otherwise have the name of the
111 // alias or site specification used by the user. Also note that if we
112 // pass in a falsy uri the drush config (i.e drush.yml) can not override
114 $uri = $preflightArgs->uri();
122 return new AliasRecord($data, '@self');