Security update for permissions_by_term
[yaffs-website] / vendor / behat / behat / src / Behat / Testwork / Cli / Application.php
1 <?php
2
3 /*
4  * This file is part of the Behat Testwork.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
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 Behat\Testwork\Cli;
12
13 use Behat\Testwork\ServiceContainer\Configuration\ConfigurationLoader;
14 use Behat\Testwork\ServiceContainer\ContainerLoader;
15 use Behat\Testwork\ServiceContainer\Exception\ConfigurationLoadingException;
16 use Behat\Testwork\ServiceContainer\ExtensionManager;
17 use Symfony\Component\Console\Application as BaseApplication;
18 use Symfony\Component\Console\Command\Command as SymfonyCommand;
19 use Symfony\Component\Console\Input\ArrayInput;
20 use Symfony\Component\Console\Input\InputDefinition;
21 use Symfony\Component\Console\Input\InputInterface;
22 use Symfony\Component\Console\Input\InputOption;
23 use Symfony\Component\Console\Output\OutputInterface;
24 use Symfony\Component\DependencyInjection\ContainerBuilder;
25 use Symfony\Component\DependencyInjection\ContainerInterface;
26
27 /**
28  * Extends Symfony console application with testwork functionality.
29  *
30  * @author Konstantin Kudryashov <ever.zet@gmail.com>
31  */
32 final class Application extends BaseApplication
33 {
34     /**
35      * @var ConfigurationLoader
36      */
37     private $configurationLoader;
38     /**
39      * @var ExtensionManager
40      */
41     private $extensionManager;
42
43     /**
44      * Initializes application.
45      *
46      * @param string              $name
47      * @param string              $version
48      * @param ConfigurationLoader $configLoader
49      * @param ExtensionManager    $extensionManager
50      */
51     public function __construct($name, $version, ConfigurationLoader $configLoader, ExtensionManager $extensionManager)
52     {
53         putenv('COLUMNS=9999');
54
55         $this->configurationLoader = $configLoader;
56         $this->extensionManager = $extensionManager;
57
58         parent::__construct($name, $version);
59     }
60
61     /**
62      * Gets the default input definition.
63      *
64      * @return InputDefinition An InputDefinition instance
65      */
66     public function getDefaultInputDefinition()
67     {
68         return new InputDefinition(array(
69             new InputOption('--profile', '-p', InputOption::VALUE_REQUIRED, 'Specify config profile to use.'),
70             new InputOption('--config', '-c', InputOption::VALUE_REQUIRED, 'Specify config file to use.'),
71             new InputOption(
72                 '--verbose', '-v', InputOption::VALUE_OPTIONAL,
73                 'Increase verbosity of exceptions.' . PHP_EOL .
74                 'Use -vv or --verbose=2 to display backtraces in addition to exceptions.'
75             ),
76             new InputOption('--help', '-h', InputOption::VALUE_NONE, 'Display this help message.'),
77             new InputOption('--config-reference', null, InputOption::VALUE_NONE, 'Display the configuration reference.'),
78             new InputOption('--debug', null, InputOption::VALUE_NONE, 'Provide debugging information about current environment.'),
79             new InputOption('--version', '-V', InputOption::VALUE_NONE, 'Display version.'),
80             new InputOption('--no-interaction', '-n', InputOption::VALUE_NONE, 'Do not ask any interactive question.'),
81             new InputOption(
82                 '--colors', null, InputOption::VALUE_NONE,
83                 'Force ANSI color in the output. By default color support is' . PHP_EOL .
84                 'guessed based on your platform and the output if not specified.'
85             ),
86             new InputOption('--no-colors', null, InputOption::VALUE_NONE, 'Force no ANSI color in the output.'),
87         ));
88     }
89
90     /**
91      * Runs the current application.
92      *
93      * @param InputInterface  $input  An Input instance
94      * @param OutputInterface $output An Output instance
95      *
96      * @return integer 0 if everything went fine, or an error code
97      */
98     public function doRun(InputInterface $input, OutputInterface $output)
99     {
100         // xdebug's default nesting level of 100 is not enough
101         if (extension_loaded('xdebug')
102             && false === strpos(ini_get('disable_functions'), 'ini_set')
103         ) {
104             $oldValue = ini_get('xdebug.max_nesting_level');
105             if ($oldValue === false || $oldValue < 256) {
106                 ini_set('xdebug.max_nesting_level', 256);
107             }
108         }
109
110         if ($input->hasParameterOption(array('--config-reference'))) {
111             $input = new ArrayInput(array('--config-reference' => true));
112         }
113
114         if ($path = $input->getParameterOption(array('--config', '-c'))) {
115             if (!is_file($path)) {
116                 throw new ConfigurationLoadingException("The requested config file does not exist");
117             }
118
119             $this->configurationLoader->setConfigurationFilePath($path);
120         }
121
122         $this->add($this->createCommand($input, $output));
123
124         return parent::doRun($input, $output);
125     }
126
127     protected function getDefaultCommands()
128     {
129         $commands = parent::getDefaultCommands();
130
131         $commands[] = new DumpReferenceCommand($this->extensionManager);
132         $commands[] = new DebugCommand($this, $this->configurationLoader, $this->extensionManager);
133
134         return $commands;
135     }
136
137     /**
138      * Configures container based on provided config file and profile.
139      *
140      * @param InputInterface $input
141      *
142      * @return array
143      */
144     private function loadConfiguration(InputInterface $input)
145     {
146         $profile = $input->getParameterOption(array('--profile', '-p')) ? : 'default';
147
148         return $this->configurationLoader->loadConfiguration($profile);
149     }
150
151     /**
152      * Creates main command for application.
153      *
154      * @param InputInterface  $input
155      * @param OutputInterface $output
156      *
157      * @return SymfonyCommand
158      */
159     private function createCommand(InputInterface $input, OutputInterface $output)
160     {
161         return $this->createContainer($input, $output)->get('cli.command');
162     }
163
164     /**
165      * Creates container instance, loads extensions and freezes it.
166      *
167      * @param InputInterface  $input
168      * @param OutputInterface $output
169      *
170      * @return ContainerInterface
171      */
172     private function createContainer(InputInterface $input, OutputInterface $output)
173     {
174         $basePath = rtrim($this->getBasePath(), DIRECTORY_SEPARATOR);
175
176         $container = new ContainerBuilder();
177
178         $container->setParameter('cli.command.name', $this->getName());
179         $container->setParameter('paths.base', $basePath);
180
181         $container->set('cli.input', $input);
182         $container->set('cli.output', $output);
183
184         $extension = new ContainerLoader($this->extensionManager);
185         $extension->load($container, $this->loadConfiguration($input));
186         $container->addObjectResource($extension);
187         $container->compile(true);
188
189         return $container;
190     }
191
192     /**
193      * Returns base path.
194      *
195      * @return string
196      */
197     private function getBasePath()
198     {
199         if ($configPath = $this->configurationLoader->getConfigurationFilePath()) {
200             return realpath(dirname($configPath));
201         }
202
203         return realpath(getcwd());
204     }
205
206     /**
207      * Gets the name of the command based on input.
208      *
209      * @param InputInterface $input The input interface
210      *
211      * @return string The command name
212      */
213     protected function getCommandName(InputInterface $input)
214     {
215         if ($input->hasParameterOption(array('--config-reference'))) {
216             return 'dump-reference';
217         }
218
219         if ($input->hasParameterOption(array('--debug'))) {
220             return 'debug';
221         }
222
223         return $this->getName();
224     }
225
226     protected function configureIO(InputInterface $input, OutputInterface $output)
227     {
228         if (true === $input->hasParameterOption(array('--colors'))) {
229             $output->setDecorated(true);
230         } elseif (true === $input->hasParameterOption(array('--no-colors'))) {
231             $output->setDecorated(false);
232         }
233
234         parent::configureIO($input, $output);
235     }
236 }