Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / drupal / console-core / src / Command / Chain / BaseCommand.php
1 <?php
2 /**
3  * Created by PhpStorm.
4  * User: jmolivas
5  * Date: 1/16/18
6  * Time: 2:22 PM
7  */
8
9 namespace Drupal\Console\Core\Command\Chain;
10
11 use Drupal\Console\Core\Command\Command;
12 use Drupal\Console\Core\Utils\ChainDiscovery;
13 use Symfony\Component\Console\Input\InputInterface;
14 use Symfony\Component\Console\Input\InputOption;
15 use Symfony\Component\Console\Output\OutputInterface;
16
17 class BaseCommand extends Command
18 {
19     /**
20      * @var ChainDiscovery
21      */
22     protected $chainDiscovery;
23
24     /**
25      * BaseCommand constructor.
26      *
27      * @param ChainDiscovery $chainDiscovery
28      */
29     public function __construct(
30         ChainDiscovery $chainDiscovery
31     ) {
32         $this->chainDiscovery = $chainDiscovery;
33         parent::__construct();
34         $this->ignoreValidationErrors();
35     }
36
37     protected function initialize(
38         InputInterface $input,
39         OutputInterface $output
40     ) {
41         parent::initialize($input, $output);
42
43         $options = [];
44         foreach ($_SERVER['argv'] as $index => $element) {
45             if ($index<2) {
46                 continue;
47             }
48
49             if (substr($element, 0, 2) !== "--") {
50                 continue;
51             }
52
53             $element = substr($element, 2);
54             $exploded = explode("=", $element);
55
56             if (!$exploded) {
57                 $exploded = explode(" ", $element);
58             }
59
60             if (count($exploded)>1) {
61                 $options[trim($exploded[0])] = trim($exploded[1]);
62             }
63         }
64
65         $file = $input->getOption('file');
66         $file = calculateRealPath($file);
67         $content = $this->chainDiscovery->getFileContents($file);
68         $variables = $this->chainDiscovery->extractInlinePlaceHolderNames($content);
69
70         foreach ($variables as $variable) {
71             if (!array_key_exists($variable, $options)) {
72                 $options[$variable] = null;
73             }
74         }
75
76         foreach ($options as $optionName => $optionValue) {
77             if ($input->hasOption($optionName)) {
78                 continue;
79             }
80
81             $this->addOption(
82                 $optionName,
83                 null,
84                 InputOption::VALUE_OPTIONAL,
85                 $optionName,
86                 $optionValue
87             );
88         }
89     }
90
91     protected function getFileOption()
92     {
93         $input = $this->getIo()->getInput();
94         $file = $input->getOption('file');
95
96         if (!$file) {
97             $files = array_keys($this->chainDiscovery->getFiles());
98
99             $file = $this->getIo()->choice(
100                 $this->trans('commands.chain.questions.chain-file'),
101                 $files
102             );
103         }
104
105         $file = calculateRealPath($file);
106         $input->setOption('file', $file);
107
108         return $file;
109     }
110
111     protected function getOptionsAsArray()
112     {
113         $input = $this->getIo()->getInput();
114         $options = [];
115         foreach ($input->getOptions() as $option => $value) {
116             $options[$option] =  $value;
117         }
118
119         return $options;
120     }
121 }