3 namespace Drush\Preflight;
5 use Drush\Config\Environment;
6 use Drush\Preflight\PreflightArgsInterface;
7 use Drush\SiteAlias\AliasRecord;
8 use Drush\SiteAlias\SiteAliasManager;
9 use Drush\SiteAlias\SiteAliasName;
10 use Drush\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 * @param PreflightArgsInterface $preflightArgs An alias name or site specification
29 * @param \Drush\Config\Environment $environment
30 * @param string $root The default Drupal root (from site:set, --root or cwd)
32 * @return \Drush\SiteAlias\AliasRecord
35 public function findSite(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
37 $aliasName = $preflightArgs->alias();
38 $selfAliasRecord = $this->determineSelf($preflightArgs, $environment, $root);
39 if (!$selfAliasRecord) {
40 throw new \Exception("The alias $aliasName could not be found.");
42 return $selfAliasRecord;
46 * Either look up the specified alias name / site spec,
47 * or, if those are invalid, then generate one from
48 * the provided root and URI.
50 * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
51 * @param \Drush\Config\Environment $environment
54 * @return \Drush\SiteAlias\AliasRecord
56 protected function determineSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
58 $aliasName = $preflightArgs->alias();
60 // If the user specified an @alias, that takes precidence.
61 if (SiteAliasName::isAliasName($aliasName)) {
62 // TODO: Should we do something about `@self` here? At the moment that will cause getAlias to
63 // call getSelf(), but we haven't built @self yet.
64 return $this->siteAliasManager->getAlias($aliasName);
67 // Ditto for a site spec (/path/to/drupal#uri)
68 $specParser = new SiteSpecParser();
69 if ($specParser->validSiteSpec($aliasName)) {
70 return new AliasRecord($specParser->parse($aliasName, $root), $aliasName);
73 // If the user provides the --root parameter then we don't want to use
74 // the site-set alias.
75 $selectedRoot = $preflightArgs->selectedSite();
77 $aliasName = $environment->getSiteSetAliasName();
78 if (!empty($aliasName)) {
79 $alias = $this->siteAliasManager->getAlias($aliasName);
86 return $this->buildSelf($preflightArgs, $root);
90 * Generate @self from the provided root and URI.
92 * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
95 * @return \Drush\SiteAlias\AliasRecord
97 protected function buildSelf(PreflightArgsInterface $preflightArgs, $root)
99 // If there is no root, then return '@none'
101 return new AliasRecord([], '@none');
104 // If there is no URI specified, we will allow it to
105 // remain empty for now. We will refine it later via
106 // Application::refineUriSelection(), which is called
107 // in Preflight::doRun(). This method will set it to
108 // 'default' if no better directory can be devined.
110 // Create the 'self' alias record. Note that the self
111 // record will be named '@self' if it is manually constructed
112 // here, and will otherwise have the name of the
113 // alias or site specification used by the user. Also note that if we
114 // pass in a falsy uri the drush config (i.e drush.yml) can not override
116 $uri = $preflightArgs->uri();
124 return new AliasRecord($data, '@self');