forked from infinum/eightshift-libs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConfigCli.php
More file actions
108 lines (90 loc) · 2.22 KB
/
ConfigCli.php
File metadata and controls
108 lines (90 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* Class that registers WPCLI command for Config.
*
* @package EightshiftLibs\Config
*/
declare(strict_types=1);
namespace EightshiftLibs\Config;
use EightshiftLibs\Cli\AbstractCli;
/**
* Class ConfigCli
*/
class ConfigCli extends AbstractCli
{
/**
* Output dir relative path.
*/
public const OUTPUT_DIR = 'src/Config';
/**
* Define default develop props.
*
* @param array $args WPCLI eval-file arguments.
*
* @return array
*/
public function getDevelopArgs(array $args): array
{
return [
'name' => $args[1] ?? 'Boilerplate',
'version' => $args[2] ?? '1',
'routes_version' => $args[5] ?? 'v2',
];
}
/**
* Get WPCLI command doc.
*
* @return array
*/
public function getDoc(): array
{
return [
'shortdesc' => 'Generates project config class.',
'synopsis' => [
[
'type' => 'assoc',
'name' => 'name',
'description' => 'Define project name.',
'optional' => true,
],
[
'type' => 'assoc',
'name' => 'version',
'description' => 'Define project version.',
'optional' => true,
],
[
'type' => 'assoc',
'name' => 'routes_version',
'description' => 'Define project REST version.',
'optional' => true,
],
],
];
}
public function __invoke(array $args, array $assocArgs) // phpcs:ignore
{
// Get Props.
$name = $assocArgs['name'] ?? '';
$version = $assocArgs['version'] ?? '';
$routesVersion = $assocArgs['routes_version'] ?? '';
$className = $this->getClassShortName();
// Read the template contents, and replace the placeholders with provided variables.
$class = $this->getExampleTemplate(__DIR__, $className);
// Replace stuff in file.
$class = $this->renameClassName($className, $class);
$class = $this->renameNamespace($assocArgs, $class);
$class = $this->renameUse($assocArgs, $class);
if (!empty($name)) {
$class = str_replace('eightshift-libs', $name, $class);
}
if (!empty($version)) {
$class = str_replace('1.0.0', $version, $class);
}
if (!empty($routesVersion)) {
$class = str_replace('v1', $routesVersion, $class);
}
// Output final class to new file/folder and finish.
$this->outputWrite(static::OUTPUT_DIR, $className, $class, $assocArgs);
}
}