3 namespace Drush\Runtime;
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;
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.
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.
20 class TildeExpansionHook implements ValidatorInterface, ConfigAwareInterface
24 public function validate(CommandData $commandData)
26 $input = $commandData->input();
27 $args = $input->getArguments();
28 $options = $input->getOptions();
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);
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);