2 namespace Drush\Commands\core;
4 use Consolidation\OutputFormatters\StructuredData\PropertyList;
5 use Drush\Commands\DrushCommands;
7 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
8 use Consolidation\SiteAlias\HostPath;
9 use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
10 use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
12 use Drush\Backend\BackendPathEvaluator;
14 class DrupalDirectoryCommands extends DrushCommands implements SiteAliasManagerAwareInterface
17 use SiteAliasManagerAwareTrait;
19 /** @var BackendPathEvaluator */
20 protected $pathEvaluator;
22 public function __construct()
24 // TODO: once the BackendInvoke service exists, inject it here
25 // and use it to get the path evaluator
26 $this->pathEvaluator = new BackendPathEvaluator();
30 * Return the filesystem path for modules/themes and other key folders.
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
48 public function drupalDirectory($target = 'root', $options = ['local-only' => false])
50 $path = $this->getPath($target, $options['local-only']);
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)) {
60 throw new \Exception(dt("Target '{target}' not found.", ['target' => $target]));
65 * Given a target (e.g. @site:%modules), return the evaluated directory path.
68 * The target to evaluate. Can be @site or /path or @site:path
69 * or @site:%pathalias, etc. (just like rsync parameters)
71 * When true, fail if the site alias is remote.
73 protected function getPath($target = 'root', $local_only = false)
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)) {
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]));
85 $this->pathEvaluator->evaluate($evaluatedPath);
86 return $evaluatedPath->fullyQualifiedPath();