-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCLI_Generator.php
More file actions
81 lines (65 loc) · 1.99 KB
/
Copy pathCLI_Generator.php
File metadata and controls
81 lines (65 loc) · 1.99 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
<?php
namespace Tribe\Libs\Generators;
class CLI_Generator extends Generator_Command {
protected $slug = '';
protected $class_name = '';
protected $assoc_args = [];
public function command() {
return 'generate cli';
}
public function description() {
return 'Generates a new CLI command';
}
public function arguments() {
return [
[
'type' => 'positional',
'name' => 'command',
'optional' => false,
'description' => __( 'The command slug.', 'tribe' ),
],
[
'type' => 'assoc',
'name' => 'description',
'optional' => true,
'description' => __( 'Command description.', 'tribe' ),
],
];
}
public function run_command( $args, $assoc_args ) {
$this->slug = $this->sanitize_slug( $args );
$this->class_name = $this->ucwords( $this->slug );
// Set up associate args with some defaults.
$this->assoc_args = $this->parse_assoc_args( $assoc_args );
$this->create_cli_file();
$this->update_subscriber();
}
protected function parse_assoc_args( $assoc_args ) {
$defaults = [
'description' => 'A generated CLI command.',
];
return wp_parse_args( $assoc_args, $defaults );
}
protected function create_cli_file() {
$new_cli_command = $this->src_path . 'CLI/' . $this->ucwords( $this->slug ) . '.php';
$this->file_system->write_file( $new_cli_command, $this->get_cli_file_contents() );
}
protected function get_cli_file_contents() {
$command_file = $this->file_system->get_file( $this->templates_path . 'cli/cli.php' );
return sprintf(
$command_file,
$this->class_name,
$this->assoc_args['description'],
$this->slug
);
}
protected function update_subscriber() {
$cli_subscriber = $this->src_path . 'CLI/CLI_Subscriber.php';
// Add to hook.
$this->file_system->insert_into_existing_file(
$cli_subscriber,
sprintf( "\t\t\t\$container->get( %s::class )->register();%s", $this->class_name, PHP_EOL ),
'->register()' // after the first command registration we find
);
}
}