ProxyCommand::getForwardCommand()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/*
3
 * This file is part of the slince/composer-alias package.
4
 *
5
 * (c) Slince <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Slince\ComposerAlias\Command;
12
13
use Composer\Command\BaseCommand;
14
use Slince\ComposerAlias\ComposerAlias;
15
use Symfony\Component\Console\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\StringInput;
18
use Symfony\Component\Console\Output\OutputInterface;
19
20
class ProxyCommand extends BaseCommand
21
{
22
    /**
23
     * @var ComposerAlias
24
     */
25
    protected $composerAlias;
26
27
    /**
28
     * @var string
29
     */
30
    protected $forwardCommand;
31
32
    protected $withArguments = false;
33
34
    public function __construct($name, ComposerAlias $composerAlias)
35
    {
36
        $this->composerAlias = $composerAlias;
37
        $this->forwardCommand = $this->getForwardCommand($name);
38
        $this->withArguments = (bool) preg_match('#\s+#', $this->forwardCommand);
39
        parent::__construct($name);
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function configure()
46
    {
47
        if ($this->withArguments) {
48
            $helpCommand = preg_replace('#(?<=\s)\w#', '', $this->forwardCommand);
49
            $description = sprintf('The alias of "%s"',
50
                $this->forwardCommand, $helpCommand
51
            );
52
        } else {
53
            $helpCommand = $this->forwardCommand;
54
            $description = sprintf('The alias of "%s", please see "%s" for more help.',
55
                $this->forwardCommand, $helpCommand
56
            );
57
        }
58
        $this->setDescription($description);
59
        $this->addArgument('args', InputArgument::IS_ARRAY | InputArgument::OPTIONAL, '');
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function execute(InputInterface $input, OutputInterface $output)
66
    {
67
        $this->runOneProxyCommand($input, $output);
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    public function isProxyCommand()
74
    {
75
        return true;
76
    }
77
78
    /**
79
     * @param InputInterface  $input
80
     * @param OutputInterface $output
81
     *
82
     * @throws \Exception
83
     */
84
    protected function runOneProxyCommand($input, $output)
85
    {
86
        if ($this->withArguments) {
87
            $input = new StringInput($this->forwardCommand);
88
        } else {
89
            $input = new StringInput(preg_replace("#^{$this->getName()}#U", $this->forwardCommand, (string) $input));
90
        }
91
        $this->getApplication()->run($input, $output);
92
    }
93
94
    protected function getForwardCommand($name)
95
    {
96
        return $this->composerAlias->getAliases()->get($name);
97
    }
98
}
99