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
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Interop\Config\Tool; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Command to dump a configuration from a factory class |
16
|
|
|
* |
17
|
|
|
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com) |
18
|
|
|
*/ |
19
|
|
|
class ConfigDumperCommand extends AbstractCommand |
20
|
|
|
{ |
21
|
|
|
const COMMAND_CLI_NAME = 'generate-config'; |
22
|
|
|
|
23
|
|
|
const HELP_TEMPLATE = <<< EOH |
24
|
|
|
<info>Usage:</info> |
25
|
|
|
%s [options] [<configFile>] [<className>] |
26
|
|
|
|
27
|
|
|
<info>Options:</info> |
28
|
|
|
<value>-h, --help, help</value> Display this help message |
29
|
|
|
|
30
|
|
|
<info>Arguments:</info> |
31
|
|
|
<value>configFile</value> Path to a config file or php://stdout for which to generate options. |
32
|
|
|
<value>className</value> Name of the class to reflect and for which to generate options. |
33
|
|
|
|
34
|
|
|
Reads the provided configuration file (creating it if it does not exist), and injects it with options for the provided |
35
|
|
|
class name, writing the changes back to the file. |
36
|
|
|
EOH; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ConfigDumper |
40
|
|
|
*/ |
41
|
|
|
private $configDumper; |
42
|
|
|
|
43
|
9 |
|
public function __construct(ConsoleHelper $helper = null, ConfigDumper $configReader = null) |
44
|
|
|
{ |
45
|
9 |
|
parent::__construct($helper); |
46
|
9 |
|
$this->configDumper = $configReader ?: new ConfigDumper($this->helper); |
47
|
9 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param array $args Argument list, minus script name |
51
|
|
|
* @return int Exit status |
52
|
|
|
*/ |
53
|
9 |
|
public function __invoke(array $args): int |
54
|
|
|
{ |
55
|
9 |
|
$arguments = $this->parseArgs($args); |
56
|
|
|
|
57
|
9 |
|
switch ($arguments->command) { |
58
|
9 |
|
case self::COMMAND_HELP: |
59
|
2 |
|
$this->help(); |
60
|
2 |
|
return 0; |
61
|
7 |
|
case self::COMMAND_ERROR: |
62
|
5 |
|
$this->helper->writeErrorLine($arguments->message); |
63
|
5 |
|
$this->help(); |
64
|
5 |
|
return 1; |
65
|
2 |
|
case self::COMMAND_DUMP: |
66
|
|
|
// fall-through |
67
|
|
|
default: |
68
|
2 |
|
break; |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
$config = $this->configDumper->createConfig($arguments->config, $arguments->class); |
72
|
|
|
|
73
|
2 |
|
$fileHeader = ''; |
74
|
|
|
|
75
|
2 |
|
if (file_exists($arguments->configFile)) { |
76
|
1 |
|
foreach (token_get_all(file_get_contents($arguments->configFile)) as $token) { |
77
|
1 |
|
if (is_array($token)) { |
78
|
1 |
|
if (isset($token[1]) && 'return' === $token[1]) { |
79
|
1 |
|
break; |
80
|
|
|
} |
81
|
1 |
|
$fileHeader .= $token[1]; |
82
|
1 |
|
continue; |
83
|
|
|
} |
84
|
1 |
|
$fileHeader .= $token; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
if (empty($fileHeader)) { |
89
|
1 |
|
$fileHeader = ConfigDumper::CONFIG_TEMPLATE; |
90
|
|
|
} |
91
|
|
|
|
92
|
2 |
|
file_put_contents($arguments->configFile, $fileHeader . $this->configDumper->dumpConfigFile($config) . PHP_EOL); |
93
|
|
|
|
94
|
2 |
|
$this->helper->writeLine(sprintf('<value>[DONE]</value> Changes written to %s', $arguments->configFile)); |
95
|
2 |
|
return 0; |
96
|
|
|
} |
97
|
|
|
|
98
|
2 |
|
protected function checkFile(string $configFile): ?\stdClass |
99
|
|
|
{ |
100
|
2 |
|
if (!is_writable(dirname($configFile)) && 'php://stdout' !== $configFile) { |
101
|
1 |
|
return $this->createErrorArgument(sprintf( |
102
|
1 |
|
'Cannot create configuration at path "%s"; not writable.', |
103
|
|
|
$configFile |
104
|
|
|
)); |
105
|
|
|
} |
106
|
1 |
|
return null; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|