befb55d8498c7aad0e89b1af0570103f63e4bb75
[yaffs-website] / vendor / drush / drush / src / Runtime / TildeExpansionHook.php
1 <?php
2
3 namespace Drush\Runtime;
4
5 use Consolidation\AnnotatedCommand\CommandData;
6 use Consolidation\AnnotatedCommand\Hooks\ValidatorInterface;
7 use Drush\Utils\StringUtils;
8 use Robo\Common\ConfigAwareTrait;
9 use Robo\Contract\ConfigAwareInterface;
10
11 /**
12  * The TildeExpansionHook is installed as a preValidate hook that runs before
13  * all commands. Argument or option values containing a leading tilde will be expanded
14  * to an absolute path.
15  *
16  * This is a pre-validate hook because we do not want to do tilde expansion
17  * for commands that are redispatched to a remote site. That happens in the
18  * RedispatchHook, which happens in hook init.
19  */
20 class TildeExpansionHook implements ValidatorInterface, ConfigAwareInterface
21 {
22     use ConfigAwareTrait;
23
24     public function validate(CommandData $commandData)
25     {
26         $input = $commandData->input();
27         $args = $input->getArguments();
28         $options = $input->getOptions();
29
30         foreach ($options as $name => $value) {
31             if (is_string($value)) {
32                 $replaced = StringUtils::replaceTilde($value, $this->getConfig()->home());
33                 if ($value != $replaced) {
34                     $input->setOption($name, $replaced);
35                 }
36             }
37         }
38         foreach ($args as $name => $value) {
39             if (is_string($value)) {
40                 $replaced = StringUtils::replaceTilde($value, $this->getConfig()->home());
41                 if ($value != $replaced) {
42                     $input->setArgument($name, $replaced);
43                 }
44             }
45         }
46     }
47 }