-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathShell_Command.php
More file actions
105 lines (94 loc) · 2.69 KB
/
Shell_Command.php
File metadata and controls
105 lines (94 loc) · 2.69 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
<?php
use WP_CLI\Utils;
class Shell_Command extends WP_CLI_Command {
/**
* Opens an interactive PHP console for running and testing PHP code.
*
* `wp shell` allows you to evaluate PHP statements and expressions
* interactively, from within a WordPress environment. Type a bit of code,
* hit enter, and see the code execute right before you. Because WordPress
* is loaded, you have access to all the functions, classes and globals
* that you can use within a WordPress plugin, for example.
*
* ## OPTIONS
*
* [--basic]
* : Force the use of WP-CLI's built-in PHP REPL, even if the Boris or
* PsySH PHP REPLs are available.
*
* [--hook=<hook>]
* : Ensure that a specific WordPress action hook has fired before starting the shell.
* This validates that the preconditions associated with that hook are met.
* Only hooks that have already been triggered can be used (e.g., init, plugins_loaded, wp_loaded).
* ---
* default: ''
* ---
*
* ## EXAMPLES
*
* # Call get_bloginfo() to get the name of the site.
* $ wp shell
* wp> get_bloginfo( 'name' );
* => string(6) "WP-CLI"
*
* # Start a shell, ensuring the 'init' hook has already fired.
* $ wp shell --hook=init
*/
public function __invoke( $_, $assoc_args ) {
$hook = Utils\get_flag_value( $assoc_args, 'hook', '' );
// No hook specified, start immediately.
if ( ! $hook ) {
$this->start_shell( $assoc_args );
return;
}
// Check if the hook has already fired.
if ( did_action( $hook ) ) {
// Hook already fired, start the shell immediately.
$this->start_shell( $assoc_args );
return;
}
// Hook hasn't fired yet.
WP_CLI::error(
sprintf(
"The '%s' hook has not fired yet. " .
'The shell command runs after WordPress is loaded, so only hooks that have already been triggered can be used. ' .
'Common hooks that are available include: init, plugins_loaded, wp_loaded.',
$hook
)
);
}
/**
* Start the shell REPL.
*
* @param array<string,bool|string> $assoc_args Associative arguments.
*/
private function start_shell( $assoc_args ) {
$class = WP_CLI\Shell\REPL::class;
$implementations = array(
\Psy\Shell::class,
\Boris\Boris::class,
WP_CLI\Shell\REPL::class,
);
if ( ! Utils\get_flag_value( $assoc_args, 'basic' ) ) {
foreach ( $implementations as $candidate ) {
if ( class_exists( $candidate ) ) {
$class = $candidate;
break;
}
}
}
/**
* @var class-string $class
*/
if ( \Psy\Shell::class === $class ) {
$shell = new Psy\Shell();
$shell->run();
} else {
/**
* @var class-string<WP_CLI\Shell\REPL> $class
*/
$repl = new $class( 'wp> ' );
$repl->start();
}
}
}