Skip to content

Instantly share code, notes, and snippets.

@tobiashm
Last active January 8, 2019 13:01
Show Gist options
  • Save tobiashm/879b3264b4d5a4f677f68b97c9d976a4 to your computer and use it in GitHub Desktop.
Save tobiashm/879b3264b4d5a4f677f68b97c9d976a4 to your computer and use it in GitHub Desktop.

Revisions

  1. tobiashm revised this gist Jan 8, 2019. 1 changed file with 10 additions and 7 deletions.
    17 changes: 10 additions & 7 deletions Kernel.php
    Original file line number Diff line number Diff line change
    @@ -29,10 +29,10 @@ private function autoload($paths)
    return;
    }

    $pathsToNamespaces = $this->getPathsToNamespaces();
    foreach ((new Finder())->in($paths)->files() as $command) {
    $path = $command->getRealPath();
    foreach ($pathsToNamespaces as $basePath => $namespace) {
    $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 getPathsToNamespaces(): array
    private function getAutoloadNamespaces(): array
    {
    $result = [];
    $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) {
    $result[base_path($path)] = $namespace;
    $path = realpath(base_path($path)) ?: realpath($path);
    if ($path) {
    $result[$path . DIRECTORY_SEPARATOR] = $namespace;
    }
    }

    return $result;
  2. tobiashm revised this gist Jan 8, 2019. 1 changed file with 11 additions and 7 deletions.
    18 changes: 11 additions & 7 deletions Kernel.php
    Original 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 \Symfony\Component\Finder\Finder())->in($paths)->files() as $command) {
    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, \Illuminate\Console\Command::class) && !(new \ReflectionClass($class))->isAbstract()) {
    \Illuminate\Console\Application::starting(function ($artisan) use ($class) {
    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
    {
    $pathsToNamespaces = [];
    $composer = json_decode(file_get_contents(base_path('composer.json')), true);
    $result = [];
    $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;
    $result[base_path($path)] = $namespace;
    }

    return $pathsToNamespaces;
    return $result;
    }
    }
  3. tobiashm created this gist Jan 8, 2019.
    56 changes: 56 additions & 0 deletions Kernel.php
    Original 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;
    }
    }