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