1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Sandro Keil (https://sandro-keil.de) |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/sandrokeil/interop-config for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017-2020 Sandro Keil |
7
|
|
|
* @license http://github.com/sandrokeil/interop-config/blob/master/LICENSE.md New BSD License |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Interop\Config\Tool; |
11
|
|
|
|
12
|
|
|
use Interop\Config\RequiresConfig; |
13
|
|
|
|
14
|
|
|
abstract class AbstractCommand |
15
|
|
|
{ |
16
|
|
|
const COMMAND_DUMP = 'dump'; |
17
|
|
|
const COMMAND_ERROR = 'error'; |
18
|
|
|
const COMMAND_HELP = 'help'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var ConsoleHelper |
22
|
|
|
*/ |
23
|
|
|
protected $helper; |
24
|
|
|
|
25
|
19 |
|
public function __construct(ConsoleHelper $helper = null) |
26
|
|
|
{ |
27
|
19 |
|
$this->helper = $helper ?: new ConsoleHelper(); |
28
|
19 |
|
} |
29
|
|
|
|
30
|
13 |
|
protected function help($resource = STDOUT): void |
|
|
|
|
31
|
|
|
{ |
32
|
13 |
|
$this->helper->writeErrorMessage(sprintf(static::HELP_TEMPLATE, static::COMMAND_CLI_NAME)); |
|
|
|
|
33
|
13 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $command |
37
|
|
|
* @param string $configFile File from which config originates, and to |
38
|
|
|
* which it will be written. |
39
|
|
|
* @param array $config Parsed configuration. |
40
|
|
|
* @param string $class Name of class to reflect. |
41
|
|
|
* @return \stdClass |
42
|
|
|
*/ |
43
|
6 |
|
protected function createArguments(string $command, string $configFile, array $config, string $class): \stdClass |
44
|
|
|
{ |
45
|
|
|
return (object)[ |
46
|
6 |
|
'command' => $command, |
47
|
6 |
|
'configFile' => $configFile, |
48
|
6 |
|
'config' => $config, |
49
|
6 |
|
'class' => $class, |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
protected function createErrorArgument(string $message): \stdClass |
54
|
|
|
{ |
55
|
|
|
return (object)[ |
56
|
9 |
|
'command' => static::COMMAND_ERROR, |
57
|
9 |
|
'message' => $message, |
58
|
|
|
]; |
59
|
|
|
} |
60
|
|
|
|
61
|
4 |
|
protected function createHelpArgument(): \stdClass |
62
|
|
|
{ |
63
|
|
|
return (object)[ |
64
|
4 |
|
'command' => static::COMMAND_HELP, |
65
|
|
|
]; |
66
|
|
|
} |
67
|
|
|
|
68
|
19 |
|
protected function parseArgs(array $args): \stdClass |
69
|
|
|
{ |
70
|
19 |
|
if (!count($args)) { |
71
|
2 |
|
return $this->createHelpArgument(); |
72
|
|
|
} |
73
|
|
|
|
74
|
17 |
|
$arg1 = array_shift($args); |
75
|
|
|
|
76
|
17 |
|
if (in_array($arg1, ['-h', '--help', 'help'], true)) { |
77
|
2 |
|
return $this->createHelpArgument(); |
78
|
|
|
} |
79
|
|
|
|
80
|
15 |
|
if (!count($args)) { |
81
|
2 |
|
return $this->createErrorArgument('<error>Missing class name</error>'); |
82
|
|
|
} |
83
|
|
|
|
84
|
13 |
|
$class = array_shift($args); |
85
|
|
|
|
86
|
13 |
|
if (!class_exists($class)) { |
87
|
2 |
|
return $this->createErrorArgument( |
88
|
2 |
|
sprintf('<error>Class "%s" does not exist or could not be autoloaded.</error>', $class) |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
11 |
|
$reflectionClass = new \ReflectionClass($class); |
93
|
|
|
|
94
|
11 |
|
if (!in_array(RequiresConfig::class, $reflectionClass->getInterfaceNames(), true)) { |
95
|
2 |
|
return $this->createErrorArgument( |
96
|
2 |
|
sprintf('<error>Class "%s" does not implement "%s".</error>', $class, RequiresConfig::class) |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
9 |
|
$configFile = $arg1; |
101
|
9 |
|
switch (file_exists($configFile)) { |
102
|
|
|
case true: |
103
|
6 |
|
$config = require $configFile; |
104
|
|
|
|
105
|
6 |
|
if ($config instanceof \Iterator) { |
106
|
1 |
|
$config = iterator_to_array($config); |
107
|
5 |
|
} elseif ($config instanceof \IteratorAggregate) { |
108
|
1 |
|
$config = iterator_to_array($config->getIterator()); |
109
|
|
|
} |
110
|
|
|
|
111
|
6 |
|
if (!is_array($config)) { |
112
|
1 |
|
return $this->createErrorArgument( |
113
|
1 |
|
sprintf('<error>Configuration at path "%s" does not return an array.</error>', $configFile) |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
5 |
|
break; |
118
|
3 |
|
case false: |
119
|
|
|
// fall-through |
120
|
|
|
default: |
121
|
3 |
|
if ($command = $this->checkFile($configFile)) { |
122
|
2 |
|
return $command; |
123
|
|
|
} |
124
|
|
|
|
125
|
1 |
|
$config = []; |
126
|
1 |
|
break; |
127
|
|
|
} |
128
|
|
|
|
129
|
6 |
|
return $this->createArguments(self::COMMAND_DUMP, $configFile, $config, $class); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
abstract protected function checkFile(string $configFile): ?\stdClass; |
133
|
|
|
} |
134
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.