diff --git a/app/Commands/DefaultTask.php b/app/Commands/DefaultTask.php index a6c3519..0f99481 100644 --- a/app/Commands/DefaultTask.php +++ b/app/Commands/DefaultTask.php @@ -20,6 +20,9 @@ class DefaultTask extends \Hleb\Scheme\App\Commands\MainTask /* Короткое название действия для команды */ const DESCRIPTION = "Default task"; + /** + * @param string|int|null $arg - argument description + */ protected function execute($arg = null) { // Your code here. diff --git a/app/Commands/RotateLogsTask.php b/app/Commands/RotateLogsTask.php index 385ce39..ade7608 100644 --- a/app/Commands/RotateLogsTask.php +++ b/app/Commands/RotateLogsTask.php @@ -10,14 +10,17 @@ class RotateLogsTask extends \Hleb\Scheme\App\Commands\MainTask { - /** php console rotate-logs-task **/ + /** php console rotate-logs-task [arg] **/ const DESCRIPTION = "Delete old logs"; - protected function execute() { - // Delete earlier than this time in seconds. - // Удаление ранее этого времени в секундах. - $prescriptionForRotation = 60 * 60 * 24 * 3; + /** + * Delete earlier than this time in days. + * Удаление ранее этого времени в днях. + * @param int $days + */ + protected function execute(int $days = 3) { + $prescriptionForRotation = 60 * 60 * 24 * $days; $total = 0; $logs = new \RecursiveIteratorIterator( diff --git a/routes/main.php b/routes/main.php index 52170dc..db2b6ea 100644 --- a/routes/main.php +++ b/routes/main.php @@ -3,11 +3,11 @@ /* * Main file for creating a routing map. * The routes change when the files in this folder are changed. If there is a time difference between the servers, - * you must execute "php console -cc" or delete the cached "routes.txt" file after making the changes. + * you must execute "php console -routes-cc" or delete the cached "routes.txt" file after making the changes. * * Основной файл для создания карты маршрутизации. * Маршруты перерасчитываются при изменении файлов в этой папке. Если есть разница во времени между серверами, необходимо выполнить - * «php console --clear-cache» или удалить кешированный файл «routes.txt» после внесения изменений. + * «php console -routes-cc» или удалить кешированный файл «routes.txt» после внесения изменений. */ Route::get("/", view("default")); diff --git a/vendor/phphleb/framework/Scheme/App/Commands/MainTask.php b/vendor/phphleb/framework/Scheme/App/Commands/MainTask.php index bf10f7e..df04104 100644 --- a/vendor/phphleb/framework/Scheme/App/Commands/MainTask.php +++ b/vendor/phphleb/framework/Scheme/App/Commands/MainTask.php @@ -4,8 +4,6 @@ class MainTask { - public function __construct() {} - public function createTask($arguments) { if(method_exists($this, 'execute')) { $this->execute(...$arguments); diff --git a/vendor/phphleb/framework/console.php b/vendor/phphleb/framework/console.php index d5bffaa..becb03b 100644 --- a/vendor/phphleb/framework/console.php +++ b/vendor/phphleb/framework/console.php @@ -1,5 +1,4 @@ [--help]" . PHP_EOL . + " --logs or -lg (prints multiple trailing lines from a log file)" . PHP_EOL . + " --new-task (сreates a new command)" . PHP_EOL . + " --new-task example-task \"Short description\"" . PHP_EOL . + (HL_TWIG_CONNECTED ? " --clear-cache--twig or -cc-twig" . PHP_EOL . " --forced-cc-twig" . PHP_EOL : ''); echo PHP_EOL; break; case '--routes': @@ -125,7 +124,7 @@ function hleb_require(string $path) { case '-l': hlUploadAll(); echo $fn->listing(); - echo PHP_EOL . PHP_EOL; + echo PHP_EOL; break; case '--info': case '-i': @@ -143,10 +142,12 @@ function hleb_require(string $path) { $file = $fn->convertCommandToTask($arguments); if (file_exists(HLEB_GLOBAL_DIRECTORY . "/app/Commands/$file.php")) { - hlUploadAll(); - - hlCreateUsersTask(HLEB_GLOBAL_DIRECTORY, $file, $setArguments, $fn); + if (end($argv) === '--help') { + hlShowCommandHelp(HLEB_GLOBAL_DIRECTORY, $file, $fn); + } else { + hlCreateUsersTask(HLEB_GLOBAL_DIRECTORY, $file, $setArguments, $fn); + } } else { echo "Missing required arguments after `console`. Add --help to display more options.", PHP_EOL; @@ -207,6 +208,13 @@ function hl_main_autoloader($class) { } function hlCreateUsersTask($path, $class, $arg, Hleb\Main\Console\MainConsole $fn) { + $task = hlCreateTaskClass($path, $class, $fn); + if($task) { + $task->createTask($arg); + } +} + +function hlCreateTaskClass($path, $class, Hleb\Main\Console\MainConsole $fn) { $realPath = $path . DIRECTORY_SEPARATOR . 'app' . DIRECTORY_SEPARATOR . 'Commands' . DIRECTORY_SEPARATOR . $class . ".php"; include_once "$realPath"; @@ -215,11 +223,43 @@ function hlCreateUsersTask($path, $class, $arg, Hleb\Main\Console\MainConsole $f foreach ($searchNames as $search_name) { if (class_exists('App\Commands\\' . $search_name)) { $className = 'App\Commands\\' . $search_name; - (new $className())->createTask($arg); - break; + return new $className(); + } + } + } + return null; +} + + + +function hlShowCommandHelp($path, $class, Hleb\Main\Console\MainConsole $fn) { + /** @var object|null $task */ + $task = hlCreateTaskClass($path, $class, $fn); + if (!is_null($task)) { + print PHP_EOL . 'DESCRIPTION: ' . $task::DESCRIPTION . PHP_EOL . PHP_EOL; + try { + $reflector = new ReflectionClass(get_class($task)); + $comment = str_replace(' ', '', $reflector->getMethod('execute')->getDocComment()); + if(!empty($comment)) { + print $comment . PHP_EOL; } + } catch (Throwable $e) { + print '#' . $e->getMessage(); + } + } + + $content = file_get_contents(HLEB_GLOBAL_DIRECTORY . "/app/Commands/$class.php"); + preg_match('/function( *)execute\(([^)]*?)\)/', $content, $match_1); + + if (!empty($match_1[2])) { + $args = explode(',', $match_1[2]); + foreach ($args as $arg) { + $item = array_map('trim', explode('=', $arg)); + print PHP_EOL . ' - ' . $item[0] . (isset($item[1]) ? ' default ' . $item[1] : '') . PHP_EOL; } + return; } + print PHP_EOL . "No arguments." . PHP_EOL; }