forked from wp-cli/wp-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-wp-cli-command.php
96 lines (84 loc) · 2.9 KB
/
class-wp-cli-command.php
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
<?php
/**
* Base class for WP-CLI commands
*
* @package wp-cli
* @author Andreas Creten
*/
abstract class WP_CLI_Command {
/**
* Construct for this class, transfers the cli arguments to the right class
*
* @param Array $args
*/
function __construct($args = array()) {
// The first command is the sub command
$sub_command = array_shift($args);
// If the method exists, try to load it
if(method_exists($this, $sub_command)) {
$this->$sub_command($args);
}
// If a dummy method exists, use it. This if for reserved keywords in php (like list, isset)
elseif(method_exists($this, '_'.$sub_command)) {
$sub_command = '_'.$sub_command;
$this->$sub_command($args);
}
// Otherwise, show the help for this command
else {
$this->help($args);
}
}
/**
* General help function for this command
*
* @param Array $args
* @return void
*/
public function help($args = array()) {
// Get the cli arguments
$arguments = $GLOBALS['argv'];
// Remove the first entry
array_shift($arguments);
// Get the command
$used_command = array_shift($arguments);
// Show the list of sub-commands for this command
WP_CLI::line('Example usage:');
WP_CLI::out(' wp '.$used_command);
$methods = WP_CLI_Command::getMethods($this);
if(!empty($methods)) {
WP_CLI::out(' ['.implode('|', $methods).']');
}
WP_CLI::line(' ...');
WP_CLI::line();
// Send a warning to the user because there is no custom help function defined in the command
// Make usure there always is a help method in your command class
WP_CLI::warning('The command has no dedicated help function, ask the creator to fix it.');
}
/**
* Get the filtered list of methods for a class
*
* @param string $class
* @return Array The list of methods
*/
static function getMethods($class) {
// Methods that don't need to be included in the method list
$blacklist = array('__construct', 'getMethods');
// Get all the methods of the class
$methods = get_class_methods($class);
// Remove the blacklisted methods
foreach($blacklist as $method) {
$in_array = array_search($method, $methods);
if($in_array !== false) {
unset($methods[$in_array]);
}
}
// Fix dummy function names
foreach($methods as $key => $method) {
if(strpos($method, '_') === 0) {
$methods[$key] = substr($method, 1, strlen($method));
}
}
// Only return the values, to fill up the gaps
return array_values($methods);
}
}