Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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 Consolidation\SiteAlias\AliasRecord;
8 use Consolidation\SiteAlias\SiteAliasManager;
9 use Consolidation\SiteAlias\SiteAliasName;
10 use Consolidation\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      * If 'false' is returned, that indicates that there was an alias name
29      * provided on the commandline that is either missing or invalid.
30      *
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)
34      *
35      * @return \Consolidation\SiteAlias\AliasRecord|false
36      */
37     public function findSite(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
38     {
39         $aliasName = $preflightArgs->alias();
40         return $this->determineSelf($preflightArgs, $environment, $root);
41     }
42
43     /**
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.
47      *
48      * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
49      * @param \Drush\Config\Environment $environment
50      * @param $root
51      *
52      * @return \Consolidation\SiteAlias\AliasRecord
53      */
54     protected function determineSelf(PreflightArgsInterface $preflightArgs, Environment $environment, $root)
55     {
56         $aliasName = $preflightArgs->alias();
57
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);
63         }
64
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);
69         }
70
71         // If the user provides the --root parameter then we don't want to use
72         // the site-set alias.
73         $selectedRoot = $preflightArgs->selectedSite();
74         if (!$selectedRoot) {
75             $aliasName = $environment->getSiteSetAliasName();
76             if (!empty($aliasName)) {
77                 $alias = $this->siteAliasManager->getAlias($aliasName);
78                 if ($alias) {
79                     return $alias;
80                 }
81             }
82         }
83
84         return $this->buildSelf($preflightArgs, $root);
85     }
86
87     /**
88      * Generate @self from the provided root and URI.
89      *
90      * @param \Drush\Preflight\PreflightArgsInterface $preflightArgs
91      * @param $root
92      *
93      * @return \Consolidation\SiteAlias\AliasRecord
94      */
95     protected function buildSelf(PreflightArgsInterface $preflightArgs, $root)
96     {
97         // If there is no root, then return '@none'
98         if (!$root) {
99             return new AliasRecord([], '@none');
100         }
101
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.
107
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
113         // it.
114         $uri = $preflightArgs->uri();
115         $data = [
116             'root' => $root,
117         ];
118         if ($uri) {
119             $data['uri'] = $uri;
120         }
121
122         return new AliasRecord($data, '@self');
123     }
124 }