2 namespace Drush\Backend;
4 use Consolidation\SiteAlias\AliasRecord;
5 use Consolidation\SiteAlias\HostPath;
7 class BackendPathEvaluator
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.
15 * @param HostPath $path The host and path to evaluate aliases on.
17 public function evaluate(HostPath $path)
19 $resolvedPath = $this->resolve($path);
24 $path->replacePathAlias($resolvedPath);
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
33 * @param HostPath $path The host and path to resolve aliases on.
36 public function resolve(HostPath $path)
38 if (!$path->hasPathAlias()) {
42 // If HostPath is `@site:%files`, then the path alias is `files`.
43 $pathAlias = $path->getPathAlias();
44 return $this->lookup($path->getAliasRecord(), $pathAlias);
48 * Lookup will use the provided alias record to look up and return
49 * the value of a path alias.
51 * @param AliasRecord $aliasRecord the host to use for lookups
52 * @param $pathAlias the alias to look up (`files`, not `%files`)
55 public function lookup(AliasRecord $aliasRecord, $pathAlias)
57 if ($aliasRecord->has("paths.$pathAlias")) {
58 return $aliasRecord->get("paths.$pathAlias");
61 return $this->request($aliasRecord, $pathAlias);
65 * Request the value of the path alias from the site associated with
68 * @param AliasRecord $aliasRecord the host to use for lookups
69 * @param string $pathAlias the alias to look up (`files`, not `%files`)
72 public function request(AliasRecord $aliasRecord, $pathAlias)
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];
82 throw new \Exception(dt('Cannot evaluate path alias %{path} for site alias {site}', ['path' => $pathAlias, 'site' => $aliasRecord->name()]));