Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[yaffs-website] / vendor / drush / drush / src / Commands / core / DrupalDirectoryCommands.php
1 <?php
2 namespace Drush\Commands\core;
3
4 use Consolidation\OutputFormatters\StructuredData\PropertyList;
5 use Drush\Commands\DrushCommands;
6 use Drush\Drush;
7 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
8 use Consolidation\SiteAlias\HostPath;
9 use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
10 use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
11
12 use Drush\Backend\BackendPathEvaluator;
13
14 class DrupalDirectoryCommands extends DrushCommands implements SiteAliasManagerAwareInterface
15 {
16
17     use SiteAliasManagerAwareTrait;
18
19     /** @var BackendPathEvaluator */
20     protected $pathEvaluator;
21
22     public function __construct()
23     {
24         // TODO: once the BackendInvoke service exists, inject it here
25         // and use it to get the path evaluator
26         $this->pathEvaluator = new BackendPathEvaluator();
27     }
28
29     /**
30      * Return the filesystem path for modules/themes and other key folders.
31      *
32      * @command drupal:directory
33      * @param string $target A module/theme name, or special names like root, files, private, or an alias : path alias string such as @alias:%files. Defaults to root.
34      * @option component The portion of the evaluated path to return.  Defaults to 'path'; 'name' returns the site alias of the target.
35      * @option local-only Reject any target that specifies a remote site.
36      * @usage cd `drush dd devel`
37      *   Navigate into the devel module directory
38      * @usage cd `drush dd`
39      *   Navigate to the root of your Drupal site
40      * @usage cd `drush dd files`
41      *   Navigate to the files directory.
42      * @usage drush dd @alias:%files
43      *   Print the path to the files directory on the site @alias.
44      * @usage edit `drush dd devel`/devel.module
45      *   Open devel module in your editor (customize 'edit' for your editor)
46      * @aliases dd,drupal-directory
47      */
48     public function drupalDirectory($target = 'root', $options = ['local-only' => false])
49     {
50         $path = $this->getPath($target, $options['local-only']);
51
52         // If getPath() is working right, it will turn
53         // %blah into the path to the item referred to by the key 'blah'.
54         // If there is no such key, then no replacement is done.  In the
55         // case of the dd command, we will consider it an error if
56         // any keys are -not- replaced in _drush_core_directory.
57         if ($path && (strpos($path, '%') === false)) {
58             return $path;
59         } else {
60             throw new \Exception(dt("Target '{target}' not found.", ['target' => $target]));
61         }
62     }
63
64     /**
65      * Given a target (e.g. @site:%modules), return the evaluated directory path.
66      *
67      * @param $target
68      *   The target to evaluate.  Can be @site or /path or @site:path
69      *   or @site:%pathalias, etc. (just like rsync parameters)
70      * @param $local_only
71      *   When true, fail if the site alias is remote.
72      */
73     protected function getPath($target = 'root', $local_only = false)
74     {
75         // In the dd command, if the path does not begin with / or % or @ ett.,
76         // then we will assume an implicit "%".
77         if (preg_match('#^[a-zA-Z0-9_-]*$#', $target)) {
78             $target = "%$target";
79         }
80         // Set up the evaluated path; fail if --local-only and the site alias is remote
81         $evaluatedPath = HostPath::create($this->siteAliasManager(), $target);
82         if ($local_only && $evaluatedPath->isRemote()) {
83             throw new \Exception(dt('{target} was remote, and --local-only was specified', ['target' => $target]));
84         }
85         $this->pathEvaluator->evaluate($evaluatedPath);
86         return $evaluatedPath->fullyQualifiedPath();
87     }
88 }