Completed
Push — multiple-paths ( f2cd79...5f4126 )
by Erin
01:53
created

Application::createCommand()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 2
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
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\InputInterface;
12
use Symfony\Component\Console\Output\ConsoleOutput;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * The main Peridot application class.
17
 *
18
 * @package Peridot\Console
19
 */
20
class Application extends ConsoleApplication
21
{
22
    /**
23
     * @var Environment
24
     */
25
    protected $environment;
26
27
    /**
28
     * @var RunnerInterface
29
     */
30
    protected $runner;
31
32
    /**
33
     * @var Configuration
34
     */
35
    protected $configuration;
36
37
    /**
38
     * @param Environment $environment
39
     */
40
    public function __construct(Environment $environment)
41
    {
42
        $this->environment = $environment;
43
        $this->validateConfiguration();
44
        $this->environment->getEventEmitter()->emit('peridot.start', [$this->environment, $this]);
45
        parent::__construct(Version::NAME, Version::NUMBER);
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @param  InputInterface  $input
52
     * @param  OutputInterface $output
53
     * @return int
54
     */
55
    public function run(InputInterface $input = null, OutputInterface $output = null)
56
    {
57
        if ($input !== null) {
58
            $in = $input;
59
        } else {
60
            $in = $this->getInput();
61
        }
62
63
        return parent::run($in, $output);
64
    }
65
66
    /**
67
     * Run the Peridot application
68
     *
69
     * @param InputInterface $input
70
     * @param OutputInterface $output
71
     * @return int
72
     */
73
    public function doRun(InputInterface $input, OutputInterface $output)
74
    {
75
        $output->setVerbosity(OutputInterface::VERBOSITY_VERBOSE);
76
77
        $this->configuration = ConfigurationReader::readInput($input);
78
        $this->environment->getEventEmitter()->emit('peridot.configure', [$this->configuration, $this]);
79
80
        $this->loadDsl($this->configuration->getDsl());
81
        $this->add($this->createCommand($this->configuration, $output));
82
83
        $exitCode = parent::doRun($input, $output);
84
85
        $this->environment->getEventEmitter()->emit('peridot.end', [$exitCode, $input, $output]);
86
87
        return $exitCode;
88
    }
89
90
    /**
91
     * Fetch the ArgvInput used by Peridot. If any exceptions are thrown due to
92
     * a mismatch between the option or argument requested and the input definition, the
93
     * exception will be rendered and Peridot will exit with an error code.
94
     *
95
     * @param array $argv An array of parameters from the CLI in the argv format.
96
     * @return ArgvInput
97
     */
98
    public function getInput(array $argv = null)
99
    {
100
        try {
101
            return new ArgvInput($argv, $this->environment->getDefinition());
102
        } catch (\Exception $e) {
103
            $this->renderException($e, new ConsoleOutput());
104
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method getInput() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
105
        }
106
    }
107
108
    /**
109
     * Return's peridot as the sole command used by Peridot
110
     *
111
     * @param  InputInterface $input
112
     * @return string
113
     */
114
    public function getCommandName(InputInterface $input)
115
    {
116
        return 'peridot';
117
    }
118
119
    /**
120
     * Load the configured DSL.
121
     *
122
     * @param $dsl
123
     */
124
    public function loadDsl($dslPath)
125
    {
126
        if (file_exists($dslPath)) {
127
            include_once $dslPath;
128
        }
129
    }
130
131
    /**
132
     * Set the runner used by the Peridot application.
133
     *
134
     * @param RunnerInterface $runner
135
     * @return $this
136
     */
137
    public function setRunner(RunnerInterface $runner)
138
    {
139
        $this->runner = $runner;
140
        return $this;
141
    }
142
143
    /**
144
     * Get the RunnerInterface being used by the Peridot application.
145
     * If one is not set, a default Runner will be used.
146
     *
147
     * @return RunnerInterface
148
     */
149
    public function getRunner()
150
    {
151
        if ($this->runner === null) {
152
            $this->runner = new Runner(
153
                Context::getInstance()->getCurrentSuite(),
154
                $this->getConfiguration(),
155
                $this->environment->getEventEmitter()
156
            );
157
        }
158
        return $this->runner;
159
    }
160
161
    /**
162
     * Return the Environment used by the Peridot application.
163
     *
164
     * @return Environment
165
     */
166
    public function getEnvironment()
167
    {
168
        return $this->environment;
169
    }
170
171
    /**
172
     * Return the configuration used by the Peridot application.
173
     *
174
     * @return Configuration
175
     */
176
    public function getConfiguration()
177
    {
178
        return $this->configuration;
179
    }
180
181
    /**
182
     * Set the configuration object used by the Peridot application.
183
     *
184
     * @param Configuration $configuration
185
     * @return $this
186
     */
187
    public function setConfiguration(Configuration $configuration)
188
    {
189
        $this->configuration = $configuration;
190
        return $this;
191
    }
192
193
    /**
194
     * Return an empty application input definition.
195
     *
196
     * @return InputDefinition
197
     */
198
    public function getDefinition()
199
    {
200
        $inputDefinition = parent::getDefinition();
201
        $inputDefinition->setArguments();
202
203
        return $inputDefinition;
204
    }
205
206
    /**
207
     * Validate that a supplied configuration exists.
208
     *
209
     * @return void
210
     */
211
    protected function validateConfiguration()
212
    {
213
        if (!$this->environment->load(getcwd() . DIRECTORY_SEPARATOR . 'peridot.php')) {
214
            fwrite(STDERR, "Configuration file specified but does not exist" . PHP_EOL);
215
            exit(1);
0 ignored issues
show
Coding Style Compatibility introduced by
The method validateConfiguration() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
216
        }
217
    }
218
219
    private function createCommand(Configuration $configuration, OutputInterface $output)
220
    {
221
        $runner = $this->getRunner();
222
        $factory = new ReporterFactory($configuration, $output, $this->environment->getEventEmitter());
223
224
        return new Command(
225
            $runner,
226
            $configuration,
227
            $factory,
228
            $this->environment->getEventEmitter(),
229
            $this->environment->getDefinition()
230
        );
231
    }
232
}
233