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