Completed
Pull Request — master (#200)
by Erin
03:42 queued 01:45
created

Application::getInput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 9
rs 9.6666
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\InputDefinition as ConsoleInputDefinition;
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);
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...
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 ConsoleInputDefinition
196
     */
197
    public function getDefinition()
198
    {
199
        return new ConsoleInputDefinition();
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);
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...
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