44
55use WPKirk \WPBones \Support \Traits \HasAttributes ;
66
7+ // Standard Color Definitions
8+ define ('WPBONES_KERNEL_COLOR_BLACK ' , "\033[0;30m " );
9+ define ('WPBONES_KERNEL_COLOR_RED ' , "\033[0;31m " );
10+ define ('WPBONES_KERNEL_COLOR_GREEN ' , "\033[0;32m " );
11+ define ('WPBONES_KERNEL_COLOR_YELLOW ' , "\033[0;33m " );
12+ define ('WPBONES_KERNEL_COLOR_BLUE ' , "\033[0;34m " );
13+ define ('WPBONES_KERNEL_COLOR_MAGENTA ' , "\033[0;35m " );
14+ define ('WPBONES_KERNEL_COLOR_CYAN ' , "\033[0;36m " );
15+ define ('WPBONES_KERNEL_COLOR_WHITE ' , "\033[0;37m " );
16+
17+ // Definition of bold colors
18+ define ('WPBONES_KERNEL_COLOR_BOLD_BLACK ' , "\033[1;30m " );
19+ define ('WPBONES_KERNEL_COLOR_BOLD_RED ' , "\033[1;31m " );
20+ define ('WPBONES_KERNEL_COLOR_BOLD_GREEN ' , "\033[1;32m " );
21+ define ('WPBONES_KERNEL_COLOR_BOLD_YELLOW ' , "\033[1;33m " );
22+ define ('WPBONES_KERNEL_COLOR_BOLD_BLUE ' , "\033[1;34m " );
23+ define ('WPBONES_KERNEL_COLOR_BOLD_MAGENTA ' , "\033[1;35m " );
24+ define ('WPBONES_KERNEL_COLOR_BOLD_CYAN ' , "\033[1;36m " );
25+ define ('WPBONES_KERNEL_COLOR_BOLD_WHITE ' , "\033[1;37m " );
26+
27+ // Definition of Light Colors
28+ define ('WPBONES_KERNEL_COLOR_LIGHT_BLACK ' , "\033[0;38;5;240m " );
29+ define ('WPBONES_KERNEL_COLOR_LIGHT_RED ' , "\033[0;38;5;203m " );
30+ define ('WPBONES_KERNEL_COLOR_LIGHT_GREEN ' , "\033[0;38;5;82m " );
31+ define ('WPBONES_KERNEL_COLOR_LIGHT_YELLOW ' , "\033[0;38;5;227m " );
32+ define ('WPBONES_KERNEL_COLOR_LIGHT_BLUE ' , "\033[0;38;5;117m " );
33+ define ('WPBONES_KERNEL_COLOR_LIGHT_MAGENTA ' , "\033[0;38;5;213m " );
34+ define ('WPBONES_KERNEL_COLOR_LIGHT_CYAN ' , "\033[0;38;5;159m " );
35+ define ('WPBONES_KERNEL_COLOR_LIGHT_WHITE ' , "\033[0;38;5;15m " );
36+
37+ // Definition for color reset
38+ define ('WPBONES_KERNEL_COLOR_RESET ' , "\033[0m " );
39+
740abstract class Command
841{
942 use HasAttributes;
@@ -60,7 +93,10 @@ public function __construct()
6093 // check "=" params
6194 if (str_ends_with ($ option , '= ' )) {
6295 $ option = rtrim ($ option , '= ' );
63- $ this ->options [$ option ] = ['description ' => $ description , 'param ' => true ];
96+ $ this ->options [$ option ] = [
97+ 'description ' => $ description ,
98+ 'param ' => true ,
99+ ];
64100 } else {
65101 $ this ->options [$ option ] = ['description ' => $ description ];
66102 }
@@ -71,23 +107,83 @@ public function __construct()
71107 }
72108
73109 /**
74- * Make a request to end user in the console and return the user input or the default value .
110+ * Commodity to display a message in the console with color .
75111 *
76- * @param string $str The question string.
77- * @param string $default Optional. A default value whether the user press return.
112+ * @param string $str The message to display.
113+ * @param string $color The color to use. Default is 'white'.
114+ */
115+ protected function color ($ str , $ color = WPBONES_KERNEL_COLOR_YELLOW )
116+ {
117+ echo $ color . $ str . WPBONES_KERNEL_COLOR_RESET ;
118+ }
119+
120+ /**
121+ * Commodity to display a message in the console.
78122 *
79- * @return string
123+ * @param string $str The message to display.
124+ * @param bool $newLine Optional. Whether to add a new line at the end.
125+ */
126+ protected function info (string $ str , $ newLine = true )
127+ {
128+ $ this ->color ($ str , WPBONES_KERNEL_COLOR_LIGHT_MAGENTA );
129+ echo $ newLine ? "\n" : '' ;
130+ }
131+
132+ /**
133+ * Commodity to display a message in the console.
134+ *
135+ * @param string $str The message to display.
136+ * @param bool $newLine Optional. Whether to add a new line at the end.
137+ */
138+ protected function line (string $ str , $ newLine = true )
139+ {
140+ $ this ->color ($ str , WPBONES_KERNEL_COLOR_LIGHT_GREEN );
141+ echo $ newLine ? "\n" : '' ;
142+ }
143+
144+ /* Commodity to display an error message in the console. */
145+ protected function error (string $ str , $ newLine = true )
146+ {
147+ echo '❌ ' ;
148+ $ this ->color ($ str , WPBONES_KERNEL_COLOR_BOLD_RED );
149+ echo $ newLine ? "\n" : '' ;
150+ }
151+
152+ /* Commodity to display an info message in the console. */
153+ protected function warning (string $ str , $ newLine = true )
154+ {
155+ echo '❗ ' ;
156+ $ this ->color ($ str , WPBONES_KERNEL_COLOR_BOLD_YELLOW );
157+ echo $ newLine ? "\n" : '' ;
158+ }
159+
160+ /* Commodity to display a success message in the console. */
161+ protected function success ($ str )
162+ {
163+ $ this ->color ('✅ ' . $ str , WPBONES_KERNEL_COLOR_GREEN );
164+ }
165+
166+ /**
167+ * Get input from console
168+ *
169+ * @param string $str The question to ask
170+ * @param string|null $default The default value
80171 */
81- protected function ask (string $ str , string $ default = '' ): string
172+ protected function ask (string $ str , ? string $ default = '' ): string
82173 {
83- echo "\n\e[38;5;88m $ str " . (empty ($ default ) ? '' : " (default: {$ default }) " ) . "\e[0m " ;
174+ $ str =
175+ WPBONES_KERNEL_COLOR_GREEN .
176+ "❓ $ str " .
177+ (empty ($ default ) ? ': ' : " (default: {$ default }): " ) .
178+ WPBONES_KERNEL_COLOR_RESET ;
84179
85- $ handle = fopen ( ' php://stdin ' , ' r ' );
86- $ line = fgets ( $ handle );
180+ // Use readline to get the user input
181+ $ line = readline ( $ str );
87182
88- fclose ($ handle );
183+ // Trim the input to remove extra spaces or newlines
184+ $ line = trim ($ line );
89185
90- return trim ( $ line, " \n\r" ) ;
186+ return $ line ?: $ default ;
91187 }
92188
93189 /**
@@ -134,17 +230,6 @@ public function options(string $value)
134230 return false ;
135231 }
136232
137- /**
138- * Display a colored line in the console.
139- *
140- * @param $str String to display.
141- */
142- protected function info (string $ str ): void
143- {
144- echo "\033[38;5;213m " . $ str ;
145- echo "\033[0m \n" ;
146- }
147-
148233 /**
149234 * Display the help well formatted.
150235 *
@@ -169,17 +254,6 @@ public function displayHelp(): void
169254 }
170255 }
171256
172- /**
173- * Display a colored line in the console.
174- *
175- * @param $str String to display.
176- */
177- protected function line (string $ str ): void
178- {
179- echo "\033[38;5;82m " . $ str ;
180- echo "\033[0m \n" ;
181- }
182-
183257 /**
184258 * Return the description of command console.
185259 *
@@ -200,6 +274,61 @@ public function setArgvAttribute($value): void
200274 $ this ->argv = $ value ;
201275 }
202276
277+ /**
278+ * Load WordPress environment.
279+ *
280+ * @return void
281+ */
282+ protected function loadWordPress ()
283+ {
284+ try {
285+ // We have to load the WordPress environment.
286+ $ currentDir = $ _SERVER ['PWD ' ] ?? __DIR__ ;
287+ $ wpLoadPath = dirname (dirname (dirname ($ currentDir ))) . '/wp-load.php ' ;
288+
289+ if (!file_exists ($ wpLoadPath )) {
290+ $ this ->wpLoaded = false ;
291+ return ;
292+ }
293+
294+ require $ wpLoadPath ;
295+ $ this ->wpLoaded = true ;
296+ } catch (\Exception $ e ) {
297+ $ this ->error ("Error! Can't load WordPress ( " . $ e ->getMessage () . ') ' );
298+ }
299+
300+ try {
301+ /**
302+ * --------------------------------------------------------------------------
303+ * Register The Auto Loader
304+ * --------------------------------------------------------------------------
305+ * Composer provides an auto-generated class loader for our app. We just
306+ * need to use it! Requiring it here means we don't have to load classes
307+ * manually. Feels great to relax.
308+ */
309+ if (file_exists (__DIR__ . '/vendor/autoload.php ' )) {
310+ require __DIR__ . '/vendor/autoload.php ' ;
311+ }
312+ } catch (\Exception $ e ) {
313+ $ this ->error ("Error! Can't load Composer autoload ( " . $ e ->getMessage () . ') ' );
314+ exit ();
315+ }
316+
317+ try {
318+ /**
319+ * --------------------------------------------------------------------------
320+ * Load this plugin env
321+ * --------------------------------------------------------------------------
322+ */
323+ if (file_exists (__DIR__ . '/bootstrap/plugin.php ' )) {
324+ require_once __DIR__ . '/bootstrap/plugin.php ' ;
325+ }
326+ } catch (\Exception $ e ) {
327+ $ this ->error ("Error! Can't load the plugin env ( " . $ e ->getMessage () . ') ' );
328+ exit ();
329+ }
330+ }
331+
203332 /**
204333 * Description
205334 *
0 commit comments