1
|
|
|
<?php |
2
|
|
|
namespace Peridot\Console; |
3
|
|
|
|
4
|
|
|
use Peridot\Configuration; |
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* The ConfigurationReader is responsible for building a Configuration |
9
|
|
|
* object from an InputInterface. |
10
|
|
|
* |
11
|
|
|
* @package Peridot\Console |
12
|
|
|
*/ |
13
|
|
|
class ConfigurationReader |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Symfony\Component\Console\Input\InputInterface |
17
|
|
|
*/ |
18
|
|
|
protected $input; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param InputInterface $input |
22
|
|
|
*/ |
23
|
|
|
public function __construct(InputInterface $input) |
24
|
|
|
{ |
25
|
|
|
$this->input = $input; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Read configuration information from input |
30
|
|
|
* |
31
|
|
|
* @return Configuration |
32
|
|
|
*/ |
33
|
|
|
public function read() |
34
|
|
|
{ |
35
|
|
|
$configuration = new Configuration(); |
36
|
|
|
$configuration->setPaths($this->readPaths()); |
37
|
|
|
|
38
|
|
|
$options = [ |
39
|
|
|
'focus' => [$configuration, 'setFocusPattern'], |
40
|
|
|
'skip' => [$configuration, 'setSkipPattern'], |
41
|
|
|
'grep' => [$configuration, 'setGrep'], |
42
|
|
|
'no-colors' => [$configuration, 'disableColors'], |
43
|
|
|
'force-colors' => [$configuration, 'enableColorsExplicit'], |
44
|
|
|
'bail' => [$configuration, 'stopOnFailure'], |
45
|
|
|
'configuration' => [$configuration, 'setConfigurationFile'] |
46
|
|
|
]; |
47
|
|
|
|
48
|
|
|
foreach ($options as $option => $callable) { |
49
|
|
|
$this->callForOption($option, $callable); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $configuration; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Static access to reader |
57
|
|
|
* |
58
|
|
|
* @param InputInterface $input |
59
|
|
|
* @return Configuration |
60
|
|
|
*/ |
61
|
|
|
public static function readInput(InputInterface $input) |
62
|
|
|
{ |
63
|
|
|
$reader = new static($input); |
64
|
|
|
|
65
|
|
|
return $reader->read(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Execute a callback if the input object has a value for the |
70
|
|
|
* given option name. |
71
|
|
|
* |
72
|
|
|
* @param string $optionName |
73
|
|
|
* @param callable $callable |
74
|
|
|
*/ |
75
|
|
|
protected function callForOption($optionName, callable $callable) |
76
|
|
|
{ |
77
|
|
|
$value = $this->input->getOption($optionName); |
78
|
|
|
if ($value) { |
79
|
|
|
call_user_func_array($callable, [$value]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
private function readPaths() |
84
|
|
|
{ |
85
|
|
|
$paths = $this->input->getArgument('path'); |
86
|
|
|
|
87
|
|
|
if (null === $this->input->getFirstArgument()) { |
88
|
|
|
$paths = array_filter($paths, 'file_exists'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $paths; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|