4c051ece0a32cc822e8da79a090cd27e2181c6b6
[yaffs-website] / vendor / drush / drush / src / Preflight / PreflightSiteLocator.php
1 <?php
2
3 namespace Drush\Preflight;
4
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;
11
12 class PreflightSiteLocator
13 {
14     /**
15      * @var SiteAliasManager
16      */
17     protected $siteAliasManager;
18
19     public function __construct(SiteAliasManager $siteAliasManager)
20     {
21         $this->siteAliasManager = $siteAliasManager;
22     }
23
24     /**
25      * During bootstrap, finds the currently selected site from the parameters
26      * provided on the commandline.
27      *
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)
31      *
32      * @return \Drush\SiteAlias\AliasRecord
33      * @throws \Exception
34      */
35     public function findSite(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
36     {
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.");
41         }
42         return $selfAliasRecord;
43     }
44
45     /**
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.
49      *
50      * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
51      * @param \Drush\Config\Environment $environment
52      * @param $root
53      *
54      * @return \Drush\SiteAlias\AliasRecord
55      */
56     protected function determineSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
57     {
58         $aliasName = $preflightArgs->alias();
59
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);
65         }
66
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);
71         }
72
73         // If the user provides the --root parameter then we don't want to use
74         // the site-set alias.
75         $selectedRoot = $preflightArgs->selectedSite();
76         if (!$selectedRoot) {
77             $aliasName = $environment->getSiteSetAliasName();
78             if (!empty($aliasName)) {
79                 $alias = $this->siteAliasManager->getAlias($aliasName);
80                 if ($alias) {
81                     return $alias;
82                 }
83             }
84         }
85
86         return $this->buildSelf($preflightArgs, $root);
87     }
88
89     /**
90      * Generate @self from the provided root and URI.
91      *
92      * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
93      * @param $root
94      *
95      * @return \Drush\SiteAlias\AliasRecord
96      */
97     protected function buildSelf(PreflightArgsInterface $preflightArgs, $root)
98     {
99         // If there is no root, then return '@none'
100         if (!$root) {
101             return new AliasRecord([], '@none');
102         }
103
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.
109
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
115         // it.
116         $uri = $preflightArgs->uri();
117         $data = [
118             'root' => $root,
119         ];
120         if ($uri) {
121             $data['uri'] = $uri;
122         }
123
124         return new AliasRecord($data, '@self');
125     }
126 }