PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Console / CommandBase.php
kirki / libraries / framework / Console Last commit date
Commands 3 weeks ago stubs 3 weeks ago CommandBase.php 3 weeks ago CommandManager.php 3 weeks ago Synopsis.php 3 weeks ago
CommandBase.php
257 lines
1 <?php
2
3 /**
4 * Abstract base for WP-CLI commands registered through the framework.
5 * Defines synopsis, description, and lifecycle hooks including prepare and run.
6 * Normalizes argument handling so concrete commands focus on business logic rather than CLI plumbing.
7 *
8 * @package Framework
9 * @subpackage Console
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Console;
13
14 \defined('ABSPATH') || exit;
15 use Kirki\Framework\Collections\Collection;
16 use function Kirki\Framework\collection;
17 abstract class CommandBase
18 {
19 /**
20 * The summary.
21 *
22 * @var string
23 *
24 * @since 1.0.0
25 */
26 protected $summary;
27 /**
28 * The description.
29 *
30 * @var string
31 *
32 * @since 1.0.0
33 */
34 protected $description;
35 /**
36 * The args.
37 *
38 * @var array
39 *
40 * @since 1.0.0
41 */
42 protected $args = [];
43 /**
44 * The when.
45 *
46 * @var 'after_wp_load'
47 *
48 * @since 1.0.0
49 */
50 protected $when;
51 /**
52 * The synopsis.
53 *
54 * @var Collection<Synopsis>
55 *
56 * @since 1.0.0
57 */
58 protected $synopsis;
59 /**
60 * Initialize the command
61 *
62 * @return void
63 *
64 * @since 1.0.0
65 */
66 public function __construct()
67 {
68 $this->synopsis = collection();
69 $this->prepare();
70 }
71 /**
72 * Run the command
73 *
74 * @param mixed $args The positional arguments.
75 * @param mixed $assoc The associative arguments.
76 *
77 * @return void
78 *
79 * @since 1.0.0
80 */
81 protected abstract function run($args, $assoc);
82 /**
83 * Prepare the command
84 *
85 * @return void
86 *
87 * @since 1.0.0
88 */
89 protected function prepare()
90 {
91 //
92 }
93 /**
94 * Check if the command passed the validation
95 *
96 * @param mixed $args The positional arguments.
97 * @param mixed $assoc The associative arguments.
98 *
99 * @return bool
100 *
101 * @since 1.0.0
102 */
103 protected function passed($args, $assoc)
104 {
105 return \true;
106 }
107 /**
108 * Get the stub path
109 *
110 * @return string
111 *
112 * @since 1.0.0
113 */
114 protected function stub_path()
115 {
116 return __DIR__ . '/stubs';
117 }
118 /**
119 * Cli error.
120 *
121 * @param string $message The message.
122 *
123 * @return void
124 *
125 * @since 1.0.0
126 */
127 protected function cli_error(string $message) : void
128 {
129 \call_user_func(['\\WP_CLI', 'error'], $message);
130 }
131 /**
132 * Cli success.
133 *
134 * @param string $message The message.
135 *
136 * @return void
137 *
138 * @since 1.0.0
139 */
140 protected function cli_success(string $message) : void
141 {
142 \call_user_func(['\\WP_CLI', 'success'], $message);
143 }
144 /**
145 * Cli line.
146 *
147 * @param string $message The message.
148 *
149 * @return void
150 *
151 * @since 1.0.0
152 */
153 protected function cli_line(string $message) : void
154 {
155 \call_user_func(['\\WP_CLI', 'line'], $message);
156 }
157 /**
158 * Set the command summary
159 *
160 * @param mixed $summary The summary.
161 *
162 * @return $this
163 *
164 * @since 1.0.0
165 */
166 protected function summary($summary)
167 {
168 $this->summary = $summary;
169 return $this;
170 }
171 /**
172 * Set the command description
173 *
174 * @param mixed $description The description.
175 *
176 * @return $this
177 *
178 * @since 1.0.0
179 */
180 protected function description($description)
181 {
182 $this->description = $description;
183 return $this;
184 }
185 /**
186 * Set the command synopsis
187 *
188 * @param Synopsis $synopsis The synopsis.
189 *
190 * @return $this
191 *
192 * @since 1.0.0
193 */
194 protected function synopsis(Synopsis $synopsis)
195 {
196 $this->synopsis->push($synopsis);
197 return $this;
198 }
199 /**
200 * Set the command when
201 *
202 * @param mixed $when The when.
203 *
204 * @return $this
205 *
206 * @since 1.0.0
207 */
208 protected function when($when)
209 {
210 $this->when = $when;
211 return $this;
212 }
213 /**
214 * Get the command arguments
215 *
216 * @return array
217 *
218 * @since 1.0.0
219 */
220 public function args()
221 {
222 $args = [];
223 if ($this->summary) {
224 $args['shortdesc'] = $this->summary;
225 }
226 if ($this->description) {
227 $args['longdesc'] = $this->description;
228 }
229 if (!$this->synopsis->empty()) {
230 $args['synopsis'] = $this->synopsis->map(function (Synopsis $synopsis) {
231 return $synopsis->to_array();
232 })->all();
233 }
234 return $args;
235 }
236 /**
237 * Run the command
238 *
239 * @param mixed $args The positional arguments.
240 * @param mixed $assoc The associative arguments.
241 *
242 * @return void
243 *
244 * @since 1.0.0
245 */
246 public function __invoke($args, $assoc)
247 {
248 if (!$this->passed($args, $assoc)) {
249 $this->cli_error('Command failed to pass validation. Please check the arguments and try again.');
250 }
251 $start = \microtime(\true);
252 $this->run($args, $assoc);
253 $runtime = \number_format((\microtime(\true) - $start) * 1000);
254 $this->cli_line(\sprintf("Time: %sms", $runtime));
255 }
256 }
257