From 2e7ddca682214ea5ffd21aadc93d33b7a2805e94 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 17 Jul 2017 09:10:21 -0500 Subject: [PATCH] add method to auto register a directory of commands --- src/Illuminate/Foundation/Console/Kernel.php | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/Illuminate/Foundation/Console/Kernel.php b/src/Illuminate/Foundation/Console/Kernel.php index 7c7a1f45acae..c99d53ee3211 100644 --- a/src/Illuminate/Foundation/Console/Kernel.php +++ b/src/Illuminate/Foundation/Console/Kernel.php @@ -5,6 +5,8 @@ use Closure; use Exception; use Throwable; +use Illuminate\Support\Str; +use Symfony\Component\Finder\Finder; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Console\Application as Artisan; @@ -188,6 +190,33 @@ public function command($signature, Closure $callback) return $command; } + /** + * Register all of the commands in the given directory. + * + * @param string $path + * @return void + */ + protected function load($path) + { + if (! is_dir($path)) { + return; + } + + $namespace = $this->app->getNamespace(); + + foreach ((new Finder)->in($path)->files() as $command) { + $command = $namespace.str_replace( + ['/', '.php'], + ['\\', ''], + Str::after($command->getPathname(), app_path().'/') + ); + + Artisan::starting(function ($artisan) use ($command) { + $artisan->resolve($command); + }); + } + } + /** * Register the given command with the console application. *