Application.php
4.9 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace Codeception;
use Codeception\Exception\ConfigurationException;
use Symfony\Component\Console\Application as BaseApplication;
use Symfony\Component\Console\Input\InputDefinition;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Console\Output\OutputInterface;
class Application extends BaseApplication
{
/**
* @var ArgvInput
*/
protected $coreArguments = null;
/**
* Register commands from config file
*
* extensions:
* commands:
* - Project\Command\MyCustomCommand
*
*/
public function registerCustomCommands()
{
try {
$this->readCustomCommandsFromConfig();
} catch (ConfigurationException $e) {
if ($e->getCode() === 404) {
return;
}
$this->renderException($e, new ConsoleOutput());
exit(1);
} catch (\Exception $e) {
$this->renderException($e, new ConsoleOutput());
exit(1);
}
}
/**
* Search custom commands and register them.
*
* @throws ConfigurationException
*/
protected function readCustomCommandsFromConfig()
{
$this->getCoreArguments(); // Maybe load outside configfile
$config = Configuration::config();
if (empty($config['extensions']['commands'])) {
return;
}
foreach ($config['extensions']['commands'] as $commandClass) {
$commandName = $this->getCustomCommandName($commandClass);
$this->add(new $commandClass($commandName));
}
}
/**
* Validate and get the name of the command
*
* @param CustomCommandInterface $commandClass
*
* @throws ConfigurationException
*
* @return string
*/
protected function getCustomCommandName($commandClass)
{
if (!class_exists($commandClass)) {
throw new ConfigurationException("Extension: Command class $commandClass not found");
}
$interfaces = class_implements($commandClass);
if (!in_array('Codeception\CustomCommandInterface', $interfaces)) {
throw new ConfigurationException("Extension: Command {$commandClass} must implement " .
"the interface `Codeception\\CustomCommandInterface`");
}
return $commandClass::getCommandName();
}
/**
* To cache Class ArgvInput
*
* @inheritDoc
*/
public function run(InputInterface $input = null, OutputInterface $output = null)
{
if ($input === null) {
$input = $this->getCoreArguments();
}
return parent::run($input, $output);
}
/**
* Add global a --config option.
*
* @return InputDefinition
*/
protected function getDefaultInputDefinition()
{
$inputDefinition = parent::getDefaultInputDefinition();
$inputDefinition->addOption(
new InputOption('config', 'c', InputOption::VALUE_OPTIONAL, 'Use custom path for config')
);
return $inputDefinition;
}
/**
* Search for --config Option and if found will be loaded
*
* example:
* -c file.yml|dir
* -cfile.yml|dir
* --config file.yml|dir
* --config=file.yml|dir
*
* @return ArgvInput
*/
protected function getCoreArguments()
{
if ($this->coreArguments !== null) {
return $this->coreArguments;
}
$argv = $_SERVER['argv'];
$argvWithoutConfig = array();
for ($i = 0; $i < count($argv); $i++) {
if (preg_match('/^(?:-([^c-]*)?c|--config(?:=|$))(.*)$/', $argv[$i], $match)) {
if (!empty($match[2])) { //same index
$this->preloadConfigration($match[2]);
} elseif (isset($argv[$i + 1])) { //next index
$this->preloadConfigration($argv[++$i]);
}
if (!empty($match[1])) {
$argvWithoutConfig[] = "-".$match[1]; //rest comands
}
continue;
}
$argvWithoutConfig[] = $argv[$i];
}
return $this->coreArguments = new ArgvInput($argvWithoutConfig);
}
/**
* Pre load Configuration, the config option is use.
*
* @param string $configFile Path to Configuration
*
* @throws ConfigurationException
*/
protected function preloadConfigration($configFile)
{
try {
Configuration::config($configFile);
} catch (ConfigurationException $e) {
if ($e->getCode() == 404) {
throw new ConfigurationException("Your configuration file `{$configFile}` could not be found.", 405);
}
throw $e;
}
}
}