Completed
Push — multiple-paths ( d245b5...f2cd79 )
by Erin
01:49
created

Application::getDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 7
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
        $runner = $this->getRunner();
81
        $factory = new ReporterFactory($this->configuration, $output, $this->environment->getEventEmitter());
82
83
        $this->loadDsl($this->configuration->getDsl());
84
        $this->add(new Command(
85
            $runner,
86
            $this->configuration,
87
            $factory,
88
            $this->environment->getEventEmitter(),
89
            $this->environment->getDefinition()
90
        ));
91
92
        $exitCode = parent::doRun($input, $output);
93
94
        $this->environment->getEventEmitter()->emit('peridot.end', [$exitCode, $input, $output]);
95
96
        return $exitCode;
97
    }
98
99
    /**
100
     * Fetch the ArgvInput used by Peridot. If any exceptions are thrown due to
101
     * a mismatch between the option or argument requested and the input definition, the
102
     * exception will be rendered and Peridot will exit with an error code.
103
     *
104
     * @param array $argv An array of parameters from the CLI in the argv format.
105
     * @return ArgvInput
106
     */
107
    public function getInput(array $argv = null)
108
    {
109
        try {
110
            return new ArgvInput($argv, $this->environment->getDefinition());
111
        } catch (\Exception $e) {
112
            $this->renderException($e, new ConsoleOutput());
113
            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...
114
        }
115
    }
116
117
    /**
118
     * Return's peridot as the sole command used by Peridot
119
     *
120
     * @param  InputInterface $input
121
     * @return string
122
     */
123
    public function getCommandName(InputInterface $input)
124
    {
125
        return 'peridot';
126
    }
127
128
    /**
129
     * Load the configured DSL.
130
     *
131
     * @param $dsl
132
     */
133
    public function loadDsl($dslPath)
134
    {
135
        if (file_exists($dslPath)) {
136
            include_once $dslPath;
137
        }
138
    }
139
140
    /**
141
     * Set the runner used by the Peridot application.
142
     *
143
     * @param RunnerInterface $runner
144
     * @return $this
145
     */
146
    public function setRunner(RunnerInterface $runner)
147
    {
148
        $this->runner = $runner;
149
        return $this;
150
    }
151
152
    /**
153
     * Get the RunnerInterface being used by the Peridot application.
154
     * If one is not set, a default Runner will be used.
155
     *
156
     * @return RunnerInterface
157
     */
158
    public function getRunner()
159
    {
160
        if ($this->runner === null) {
161
            $this->runner = new Runner(
162
                Context::getInstance()->getCurrentSuite(),
163
                $this->getConfiguration(),
164
                $this->environment->getEventEmitter()
165
            );
166
        }
167
        return $this->runner;
168
    }
169
170
    /**
171
     * Return the Environment used by the Peridot application.
172
     *
173
     * @return Environment
174
     */
175
    public function getEnvironment()
176
    {
177
        return $this->environment;
178
    }
179
180
    /**
181
     * Return the configuration used by the Peridot application.
182
     *
183
     * @return Configuration
184
     */
185
    public function getConfiguration()
186
    {
187
        return $this->configuration;
188
    }
189
190
    /**
191
     * Set the configuration object used by the Peridot application.
192
     *
193
     * @param Configuration $configuration
194
     * @return $this
195
     */
196
    public function setConfiguration(Configuration $configuration)
197
    {
198
        $this->configuration = $configuration;
199
        return $this;
200
    }
201
202
    /**
203
     * Return an empty application input definition.
204
     *
205
     * @return InputDefinition
206
     */
207
    public function getDefinition()
208
    {
209
        $inputDefinition = parent::getDefinition();
210
        $inputDefinition->setArguments();
211
212
        return $inputDefinition;
213
    }
214
215
    /**
216
     * Validate that a supplied configuration exists.
217
     *
218
     * @return void
219
     */
220
    protected function validateConfiguration()
221
    {
222
        if (!$this->environment->load(getcwd() . DIRECTORY_SEPARATOR . 'peridot.php')) {
223
            fwrite(STDERR, "Configuration file specified but does not exist" . PHP_EOL);
224
            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...
225
        }
226
    }
227
}
228