Application::getDefaultInputDefinition()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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