Skip to content

Commit

Permalink
upd console
Browse files Browse the repository at this point in the history
  • Loading branch information
phphleb committed Jun 11, 2021
1 parent 834873d commit a960ba1
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 34 deletions.
3 changes: 3 additions & 0 deletions app/Commands/DefaultTask.php
Expand Up @@ -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.
Expand Down
13 changes: 8 additions & 5 deletions app/Commands/RotateLogsTask.php
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions routes/main.php
Expand Up @@ -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"));
Expand Down
2 changes: 0 additions & 2 deletions vendor/phphleb/framework/Scheme/App/Commands/MainTask.php
Expand Up @@ -4,8 +4,6 @@

class MainTask
{
public function __construct() {}

public function createTask($arguments) {
if(method_exists($this, 'execute')) {
$this->execute(...$arguments);
Expand Down
90 changes: 65 additions & 25 deletions vendor/phphleb/framework/console.php
@@ -1,5 +1,4 @@
<?php

$arguments = $argv[1] ?? null;

// End of script execution (before starting the main project).
Expand Down Expand Up @@ -56,7 +55,6 @@ function hleb_require(string $path) {
$fn = new \Hleb\Main\Console\MainConsole();

if ($arguments) {

switch ($arguments) {
case '--version':
case '-v':
Expand All @@ -70,21 +68,20 @@ function hleb_require(string $path) {
" ╚═ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ══ ═╝ " . PHP_EOL;
echo PHP_EOL;
break;
case '--clear-cache':
case '-cc':
case '--clear-routes-cache':
case '-routes-cc':
if (file_exists(HLEB_STORAGE_CACHE_ROUTES_DIRECTORY . '/routes.txt')) {
unlink(HLEB_STORAGE_CACHE_ROUTES_DIRECTORY . '/routes.txt');
echo PHP_EOL . 'Route cache cleared.';
}
break;
case '--clear-cache':
case '-cc':
$files = glob(HLEB_GLOBAL_DIRECTORY . HLEB_TEMPLATE_CACHED_PATH . '/*/*.cache', GLOB_NOSORT);
hlClearCacheFiles($files, HLEB_TEMPLATE_CACHED_PATH, $fn, HLEB_TEMPLATE_CACHED_PATH . '/*/*.cache');
echo PHP_EOL, PHP_EOL;
break;
case '--forced-cc':
if(file_exists(HLEB_STORAGE_CACHE_ROUTES_DIRECTORY . '/routes.txt')) {
unlink(HLEB_STORAGE_CACHE_ROUTES_DIRECTORY . '/routes.txt');
echo PHP_EOL . 'Route cache cleared.';
}
hlForcedClearCacheFiles(HLEB_GLOBAL_DIRECTORY . HLEB_TEMPLATE_CACHED_PATH);
echo PHP_EOL;
break;
Expand All @@ -103,17 +100,19 @@ function hleb_require(string $path) {
case '--help':
case '-h':
echo PHP_EOL;
echo " --version or -v (displays the version of the framework)" . PHP_EOL .
" --clear-cache or -cc (clears the templates and routes cache)" . PHP_EOL .
" --forced-cc (forcefully clears the templates and routes cache)" . PHP_EOL .
" --info or -i (displays the values of the main settings)" . PHP_EOL .
" --help or -h (displays a list of default console actions)" . PHP_EOL .
" --routes or -r (forms a list of routes)" . PHP_EOL .
" --list or -l (forms a list of commands)" . 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 . PHP_EOL .
(HL_TWIG_CONNECTED ? " --clear-cache--twig or -cc-twig" . PHP_EOL . " --forced-cc-twig" . PHP_EOL : '');
echo " --version or -v (displays the version of the framework)" . PHP_EOL .
" --clear-cache or -cc (clears the templates)" . PHP_EOL .
" --forced-cc (forcefully clears the templates)" . PHP_EOL .
" --clear-routes-cache or -routes-cc (clear routes cache)" . PHP_EOL .
" --info or -i (displays the values of the main settings)" . PHP_EOL .
" --help or -h (displays a list of default console actions)" . PHP_EOL .
" --routes or -r (forms a list of routes)" . PHP_EOL .
" --list or -l (forms a list of commands)" . PHP_EOL .
" <command> [--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':
Expand All @@ -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':
Expand All @@ -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;
Expand Down Expand Up @@ -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";

Expand All @@ -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;
}


Expand Down

0 comments on commit a960ba1

Please sign in to comment.