From 733c6cda5cd65d5c6624f370b45ab7351ea77dbd Mon Sep 17 00:00:00 2001 From: Timoffei Bubigo Date: Wed, 23 Dec 2020 11:35:40 +0300 Subject: [PATCH] Dynamic commands loader --- core/app/Console/Kernel.php | 62 +++++++++++++++++++++++++++++++------ 1 file changed, 52 insertions(+), 10 deletions(-) diff --git a/core/app/Console/Kernel.php b/core/app/Console/Kernel.php index 0b2bb34..2bbcae6 100644 --- a/core/app/Console/Kernel.php +++ b/core/app/Console/Kernel.php @@ -8,23 +8,65 @@ use Laravel\Lumen\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { - /** - * The Artisan commands provided by your application. - * - * @var array - */ - protected $commands = [ - HelloWorld::class - ]; - /** * Define the application's command schedule. * - * @param \Illuminate\Console\Scheduling\Schedule $schedule + * @param \Illuminate\Console\Scheduling\Schedule $schedule * @return void */ protected function schedule(Schedule $schedule) { // } + + private function extractClass($file) + { + $fp = fopen($file, 'r'); + $class = $namespace = $buffer = ''; + $i = 0; + while (!$class) { + if (feof($fp)) break; + + $buffer .= fread($fp, 512); + $tokens = token_get_all($buffer); + + if (strpos($buffer, '{') === false) continue; + + for (;$iextractClass($commands_path . '/' . $file); + } + + $commands = parent::getCommands(); + return array_merge($commands, $classes); + } }