summaryrefslogtreecommitdiffstats
path: root/src/Provision/Console/ArgvInput.php
blob: 72392eff7aaf3e9f4b2fc6ec0a15ed3e75c92bd1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php

namespace Aegir\Provision\Console;

use \Symfony\Component\Console\Input\ArgvInput as ArgvInputBase;
use \Symfony\Component\Console\Input\InputDefinition;

class ArgvInput extends ArgvInputBase {

    /**
     * @var string The name of the active context, extracted from the first argument if it has "@" prefix.
     */
    public $activeContextName = NULL;

    /**
    * @param array|null           $argv       An array of parameters from the CLI (in the argv format)
    * @param InputDefinition|null $definition A InputDefinition instance
    */
    public function __construct(array $argv = null, InputDefinition $definition = null)
    {
        // If @alias is used, swap it out with --context=
        if (isset($argv[1]) && strpos($argv[1], '@') === 0) {
            $context_name = ltrim($argv[1], '@');
            $argv[1] = "--context={$context_name}";
            $this->activeContextName = $context_name;
        }
        // If --context option is used, use that.
        elseif ($argv_filtered = array_filter($argv, function ($key) {
            return strpos($key, '--context') === 0;
        })) {
            $context_option = array_pop($argv_filtered);
            $context_name = substr($context_option, strlen('--context='));
            $this->activeContextName = $context_name;
        }
        parent::__construct($argv, $definition);
    }
}