Last active
January 8, 2019 13:01
-
-
Save tobiashm/879b3264b4d5a4f677f68b97c9d976a4 to your computer and use it in GitHub Desktop.
Revisions
-
tobiashm revised this gist
Jan 8, 2019 . 1 changed file with 10 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -29,10 +29,10 @@ private function autoload($paths) return; } $autoloadNamespaces = $this->getAutoloadNamespaces(); foreach ((new Finder())->in($paths)->files() as $file) { $path = $file->getRealPath(); foreach ($autoloadNamespaces as $basePath => $namespace) { if (substr($path, 0, strlen($basePath)) !== $basePath) { continue; } @@ -47,12 +47,15 @@ private function autoload($paths) } } private function getAutoloadNamespaces(): array { $result = [app_path() . DIRECTORY_SEPARATOR => app()->getNamespace()]; $composer = json_decode(file_get_contents(base_path('composer.json')), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { $path = realpath(base_path($path)) ?: realpath($path); if ($path) { $result[$path . DIRECTORY_SEPARATOR] = $namespace; } } return $result; -
tobiashm revised this gist
Jan 8, 2019 . 1 changed file with 11 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,11 @@ namespace App\Console; use Illuminate\Console\Application as Artisan; use Illuminate\Console\Command; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; use ReflectionClass; use Symfony\Component\Finder\Finder; class Kernel extends ConsoleKernel { @@ -26,16 +30,16 @@ private function autoload($paths) } $pathsToNamespaces = $this->getPathsToNamespaces(); foreach ((new Finder())->in($paths)->files() as $command) { $path = $command->getRealPath(); foreach ($pathsToNamespaces as $basePath => $namespace) { if (substr($path, 0, strlen($basePath)) !== $basePath) { continue; } $class = $namespace . str_replace(['/', '.php'], ['\\', ''], substr($path, strlen($basePath))); if (is_subclass_of($class, Command::class) && !(new ReflectionClass($class))->isAbstract()) { Artisan::starting(function ($artisan) use ($class) { $artisan->resolve($class); }); } @@ -45,12 +49,12 @@ private function autoload($paths) private function getPathsToNamespaces(): array { $result = []; $composer = json_decode(file_get_contents(base_path('composer.json')), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { $result[base_path($path)] = $namespace; } return $result; } } -
tobiashm created this gist
Jan 8, 2019 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,56 @@ <?php namespace App\Console; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; class Kernel extends ConsoleKernel { public function commands() { $this->autoload([ base_path('project/Commands') ]); } /** * @throws \ReflectionException */ private function autoload($paths) { $paths = array_unique(is_array($paths) ? $paths : [$paths]); $paths = array_filter($paths, 'is_dir'); if (empty($paths)) { return; } $pathsToNamespaces = $this->getPathsToNamespaces(); foreach ((new \Symfony\Component\Finder\Finder())->in($paths)->files() as $command) { $path = $command->getRealPath(); foreach ($pathsToNamespaces as $basePath => $namespace) { if (substr($path, 0, strlen($basePath)) !== $basePath) { continue; } $class = $namespace . str_replace(['/', '.php'], ['\\', ''], substr($path, strlen($basePath))); if (is_subclass_of($class, \Illuminate\Console\Command::class) && !(new \ReflectionClass($class))->isAbstract()) { \Illuminate\Console\Application::starting(function ($artisan) use ($class) { $artisan->resolve($class); }); } } } } private function getPathsToNamespaces(): array { $pathsToNamespaces = []; $composer = json_decode(file_get_contents(base_path('composer.json')), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { $pathsToNamespaces[base_path($path)] = $namespace; } return $pathsToNamespaces; } }