|
1
|
|
|
<?php |
|
2
|
|
|
namespace Peridot\Console; |
|
3
|
|
|
|
|
4
|
|
|
use Peridot\Configuration; |
|
5
|
|
|
use Peridot\Reporter\ReporterFactory; |
|
6
|
|
|
use Peridot\Runner\Context; |
|
7
|
|
|
use Peridot\Runner\Runner; |
|
8
|
|
|
use Peridot\Runner\RunnerInterface; |
|
9
|
|
|
use Symfony\Component\Console\Application as ConsoleApplication; |
|
10
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
|
|
|
|
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* The main Peridot application class. |
|
18
|
|
|
* |
|
19
|
|
|
* @package Peridot\Console |
|
20
|
|
|
*/ |
|
21
|
|
|
class Application extends ConsoleApplication |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var Environment |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $environment; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var RunnerInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $runner; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var Configuration |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $configuration; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param Environment $environment |
|
40
|
|
|
*/ |
|
41
|
|
|
public function __construct(Environment $environment) |
|
42
|
|
|
{ |
|
43
|
|
|
$this->environment = $environment; |
|
44
|
|
|
$this->validateConfiguration(); |
|
45
|
|
|
$this->environment->getEventEmitter()->emit('peridot.start', [$this->environment, $this]); |
|
46
|
|
|
parent::__construct(Version::NAME, Version::NUMBER); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* {@inheritdoc} |
|
51
|
|
|
* |
|
52
|
|
|
* @param InputInterface $input |
|
53
|
|
|
* @param OutputInterface $output |
|
54
|
|
|
* @return int |
|
55
|
|
|
*/ |
|
56
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
57
|
|
|
{ |
|
58
|
|
|
if ($input !== null) { |
|
59
|
|
|
$in = $input; |
|
60
|
|
|
} else { |
|
61
|
|
|
$in = $this->getInput(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return parent::run($in, $output); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Run the Peridot application |
|
69
|
|
|
* |
|
70
|
|
|
* @param InputInterface $input |
|
71
|
|
|
* @param OutputInterface $output |
|
72
|
|
|
* @return int |
|
73
|
|
|
*/ |
|
74
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
75
|
|
|
{ |
|
76
|
|
|
$this->configuration = ConfigurationReader::readInput($input); |
|
77
|
|
|
$this->environment->getEventEmitter()->emit('peridot.configure', [$this->configuration, $this]); |
|
78
|
|
|
|
|
79
|
|
|
$this->loadDsl($this->configuration->getDsl()); |
|
80
|
|
|
$this->add($this->createCommand($this->configuration, $output)); |
|
81
|
|
|
|
|
82
|
|
|
$exitCode = parent::doRun($input, $output); |
|
83
|
|
|
|
|
84
|
|
|
$this->environment->getEventEmitter()->emit('peridot.end', [$exitCode, $input, $output]); |
|
85
|
|
|
|
|
86
|
|
|
return $exitCode; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Fetch the ArgvInput used by Peridot. If any exceptions are thrown due to |
|
91
|
|
|
* a mismatch between the option or argument requested and the input definition, the |
|
92
|
|
|
* exception will be rendered and Peridot will exit with an error code. |
|
93
|
|
|
* |
|
94
|
|
|
* @param array $argv An array of parameters from the CLI in the argv format. |
|
95
|
|
|
* @return ArgvInput |
|
96
|
|
|
*/ |
|
97
|
|
|
public function getInput(array $argv = null) |
|
98
|
|
|
{ |
|
99
|
|
|
try { |
|
100
|
|
|
return new ArgvInput($argv, $this->environment->getDefinition()); |
|
101
|
|
|
} catch (\Exception $e) { |
|
102
|
|
|
$this->renderException($e, new ConsoleOutput()); |
|
103
|
|
|
exit(1); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* Return's peridot as the sole command used by Peridot |
|
109
|
|
|
* |
|
110
|
|
|
* @param InputInterface $input |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
|
|
public function getCommandName(InputInterface $input) |
|
114
|
|
|
{ |
|
115
|
|
|
return 'peridot'; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Load the configured DSL. |
|
120
|
|
|
* |
|
121
|
|
|
* @param $dsl |
|
122
|
|
|
*/ |
|
123
|
|
|
public function loadDsl($dslPath) |
|
124
|
|
|
{ |
|
125
|
|
|
if (file_exists($dslPath)) { |
|
126
|
|
|
include_once $dslPath; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Set the runner used by the Peridot application. |
|
132
|
|
|
* |
|
133
|
|
|
* @param RunnerInterface $runner |
|
134
|
|
|
* @return $this |
|
135
|
|
|
*/ |
|
136
|
|
|
public function setRunner(RunnerInterface $runner) |
|
137
|
|
|
{ |
|
138
|
|
|
$this->runner = $runner; |
|
139
|
|
|
return $this; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Get the RunnerInterface being used by the Peridot application. |
|
144
|
|
|
* If one is not set, a default Runner will be used. |
|
145
|
|
|
* |
|
146
|
|
|
* @return RunnerInterface |
|
147
|
|
|
*/ |
|
148
|
|
|
public function getRunner() |
|
149
|
|
|
{ |
|
150
|
|
|
if ($this->runner === null) { |
|
151
|
|
|
$this->runner = new Runner( |
|
152
|
|
|
Context::getInstance()->getCurrentSuite(), |
|
153
|
|
|
$this->getConfiguration(), |
|
154
|
|
|
$this->environment->getEventEmitter() |
|
155
|
|
|
); |
|
156
|
|
|
} |
|
157
|
|
|
return $this->runner; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Return the Environment used by the Peridot application. |
|
162
|
|
|
* |
|
163
|
|
|
* @return Environment |
|
164
|
|
|
*/ |
|
165
|
|
|
public function getEnvironment() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->environment; |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* Return the configuration used by the Peridot application. |
|
172
|
|
|
* |
|
173
|
|
|
* @return Configuration |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getConfiguration() |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->configuration; |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Set the configuration object used by the Peridot application. |
|
182
|
|
|
* |
|
183
|
|
|
* @param Configuration $configuration |
|
184
|
|
|
* @return $this |
|
185
|
|
|
*/ |
|
186
|
|
|
public function setConfiguration(Configuration $configuration) |
|
187
|
|
|
{ |
|
188
|
|
|
$this->configuration = $configuration; |
|
189
|
|
|
return $this; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
/** |
|
193
|
|
|
* Return an empty application input definition. |
|
194
|
|
|
* |
|
195
|
|
|
* @return InputDefinition |
|
196
|
|
|
*/ |
|
197
|
|
|
public function getDefinition() |
|
198
|
|
|
{ |
|
199
|
|
|
return new InputDefinition(); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Validate that a supplied configuration exists. |
|
204
|
|
|
* |
|
205
|
|
|
* @return void |
|
206
|
|
|
*/ |
|
207
|
|
|
protected function validateConfiguration() |
|
208
|
|
|
{ |
|
209
|
|
|
if (!$this->environment->load(getcwd() . DIRECTORY_SEPARATOR . 'peridot.php')) { |
|
210
|
|
|
fwrite(STDERR, "Configuration file specified but does not exist" . PHP_EOL); |
|
211
|
|
|
exit(1); |
|
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
private function createCommand(Configuration $configuration, OutputInterface $output) |
|
216
|
|
|
{ |
|
217
|
|
|
$runner = $this->getRunner(); |
|
218
|
|
|
$factory = new ReporterFactory($configuration, $output, $this->environment->getEventEmitter()); |
|
219
|
|
|
|
|
220
|
|
|
return new Command( |
|
221
|
|
|
$runner, |
|
222
|
|
|
$configuration, |
|
223
|
|
|
$factory, |
|
224
|
|
|
$this->environment->getEventEmitter(), |
|
225
|
|
|
$this->environment->getDefinition() |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
Let’s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let’s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: