Cli: add core_mode() method, set it in handle_args() and add an hook

This commit is contained in:
Benjamin Renard 2023-02-28 01:13:43 +01:00
parent 6aa4113310
commit f499087241
2 changed files with 41 additions and 13 deletions

View file

@ -16,4 +16,4 @@ else
die("Fail to find composer vendor/autoload.php file\n"); die("Fail to find composer vendor/autoload.php file\n");
App::init(null, null, __DIR__); App::init(null, null, __DIR__);
Cli :: handle_args(); Cli :: handle_args(null, true);

View file

@ -6,13 +6,38 @@ use Exception;
class Cli { class Cli {
/**
* EesyPHP core mode
*/
private static $core_mode = false;
/** /**
* Initialize * Initialize
* @return void * @return void
*/ */
public static function init() { public static function init() {
if (php_sapi_name() != 'cli') Hook :: register('cli_set_core_mode', array('\\EesyPHP\\Cli', 'on_cli_set_core_mode'));
return; }
/**
* Get/set core mode
* @return bool
*/
public static function core_mode($enable=null) {
if (!is_null($enable)) {
self :: $core_mode = boolval($enable);
Hook :: trigger('cli_set_core_mode', array('enabled' => self :: $core_mode));
}
return self :: $core_mode;
}
/**
* On CLI set core mode hook
* @param \EesyPHP\HookEvent $event
* @return void
*/
public static function on_cli_set_core_mode($event) {
if ($event->enabled) {
self :: add_command( self :: add_command(
'new_project', 'new_project',
array('\\EesyPHP\\Cli', 'cli_new_project'), array('\\EesyPHP\\Cli', 'cli_new_project'),
@ -23,6 +48,7 @@ class Cli {
"EesyPHP framework.") "EesyPHP framework.")
); );
} }
}
/** /**
* Registered commands * Registered commands
@ -127,10 +153,12 @@ class Cli {
/** /**
* Handle command line arguments * Handle command line arguments
* @param array|null $args Command line argurment to handle (optional, default: $argv) * @param array|null $args Command line argurment to handle (optional, default: $argv)
* @param bool|null $core_mode Force enable/disable EesyPHP core mode (optional, default: null)
* @return void * @return void
*/ */
public static function handle_args($args=null) { public static function handle_args($args=null, $core_mode=null) {
global $argv; global $argv;
self :: core_mode($core_mode);
$args = is_array($args)?$args:array_slice($argv, 1); $args = is_array($args)?$args:array_slice($argv, 1);
$log_level_set = false; $log_level_set = false;
self :: $command = null; self :: $command = null;