Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / vendor / drush / drush / src / Backend / BackendPathEvaluator.php
1 <?php
2 namespace Drush\Backend;
3
4 use Consolidation\SiteAlias\AliasRecord;
5 use Consolidation\SiteAlias\HostPath;
6
7 class BackendPathEvaluator
8 {
9     /**
10      * Evaluate will check to see if the provided host path
11      * contains a path alias. If it does, the alias will
12      * be resolved, and the result of the resolution will be
13      * injected into the HostPath, replacing the alias.
14      *
15      * @param HostPath $path The host and path to evaluate aliases on.
16      */
17     public function evaluate(HostPath $path)
18     {
19         $resolvedPath = $this->resolve($path);
20         if (!$resolvedPath) {
21             return;
22         }
23
24         $path->replacePathAlias($resolvedPath);
25     }
26
27     /**
28      * Resolve will check to see if the provided host path
29      * contains a path alias. If it does, the alias will
30      * be resolved, and the result of the resolution will be
31      * returned.
32      *
33      * @param HostPath $path The host and path to resolve aliases on.
34      * @return string
35      */
36     public function resolve(HostPath $path)
37     {
38         if (!$path->hasPathAlias()) {
39             return false;
40         }
41
42         // If HostPath is `@site:%files`, then the path alias is `files`.
43         $pathAlias = $path->getPathAlias();
44         return $this->lookup($path->getAliasRecord(), $pathAlias);
45     }
46
47     /**
48      * Lookup will use the provided alias record to look up and return
49      * the value of a path alias.
50      *
51      * @param AliasRecord $aliasRecord the host to use for lookups
52      * @param $pathAlias the alias to look up (`files`, not `%files`)
53      * @return string
54      */
55     public function lookup(AliasRecord $aliasRecord, $pathAlias)
56     {
57         if ($aliasRecord->has("paths.$pathAlias")) {
58             return $aliasRecord->get("paths.$pathAlias");
59         }
60
61         return $this->request($aliasRecord, $pathAlias);
62     }
63
64     /**
65      * Request the value of the path alias from the site associated with
66      * the alias record.
67      *
68      * @param AliasRecord $aliasRecord the host to use for lookups
69      * @param string $pathAlias the alias to look up (`files`, not `%files`)
70      * @return string
71      */
72     public function request(AliasRecord $aliasRecord, $pathAlias)
73     {
74         // The drupal:directory command uses a path evaluator, which
75         // calls this function, so we cannot use dd here, as that
76         // would be recursive.
77         $values = drush_invoke_process($aliasRecord, "core-status", [], ['project' => $pathAlias], ['integrate' => false, 'override-simulated' => true]);
78         $statusValues = $values['object'];
79         if (isset($statusValues[$pathAlias])) {
80             return $statusValues[$pathAlias];
81         }
82         throw new \Exception(dt('Cannot evaluate path alias %{path} for site alias {site}', ['path' => $pathAlias, 'site' => $aliasRecord->name()]));
83     }
84 }