From f49908724108b2192734e497da74c6afb9f057c9 Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Tue, 28 Feb 2023 01:13:43 +0100 Subject: [PATCH] Cli: add core_mode() method, set it in handle_args() and add an hook --- eesyphp | 2 +- src/Cli.php | 52 ++++++++++++++++++++++++++++++++++++++++------------ 2 files changed, 41 insertions(+), 13 deletions(-) diff --git a/eesyphp b/eesyphp index bc7269f..0b084c3 100755 --- a/eesyphp +++ b/eesyphp @@ -16,4 +16,4 @@ else die("Fail to find composer vendor/autoload.php file\n"); App::init(null, null, __DIR__); -Cli :: handle_args(); +Cli :: handle_args(null, true); diff --git a/src/Cli.php b/src/Cli.php index fa7f955..7abc260 100644 --- a/src/Cli.php +++ b/src/Cli.php @@ -6,22 +6,48 @@ use Exception; class Cli { + /** + * EesyPHP core mode + */ + private static $core_mode = false; + /** * Initialize * @return void */ public static function init() { - if (php_sapi_name() != 'cli') - return; - self :: add_command( - 'new_project', - array('\\EesyPHP\\Cli', 'cli_new_project'), - I18n :: ___("Create a new project using EesyPHP framework"), - null, - I18n :: ___( - "This command could be used to easily build the structure of a new project using the ". - "EesyPHP framework.") - ); + Hook :: register('cli_set_core_mode', array('\\EesyPHP\\Cli', 'on_cli_set_core_mode')); + } + + /** + * 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( + 'new_project', + array('\\EesyPHP\\Cli', 'cli_new_project'), + I18n :: ___("Create a new project using EesyPHP framework"), + null, + I18n :: ___( + "This command could be used to easily build the structure of a new project using the ". + "EesyPHP framework.") + ); + } } /** @@ -127,10 +153,12 @@ class Cli { /** * Handle command line arguments * @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 */ - public static function handle_args($args=null) { + public static function handle_args($args=null, $core_mode=null) { global $argv; + self :: core_mode($core_mode); $args = is_array($args)?$args:array_slice($argv, 1); $log_level_set = false; self :: $command = null;