laravel / lumen-framework

The Laravel Lumen Framework.
https://lumen.laravel.com
MIT License
1.48k stars 419 forks source link

Typehinting AuthManager for Injection throws `<required> $app` Exception #1089

Closed ElvenSpellmaker closed 4 years ago

ElvenSpellmaker commented 4 years ago

Description:

When I try to inject AuthManager into a class it throws an Exception.

Steps To Reproduce:

image

Try to use code like this.

It seems Lumen half registers the component:

Unresolvable dependency resolving [Parameter #0 [ <required> $app ]] in class Illuminate\\Auth\\AuthManager

The only way I can solve this is by adding:

        $this->app->singleton(AuthManager::class, function () {
            return $this->app->loadComponent('auth', 'Illuminate\Auth\AuthServiceProvider', 'auth');
        });

to my AuthServiceProvider.php.

For some reason the class is half loaded such that it's not throwing that the class isn't registered but also can't return the correct singleton instance without me registering it.

I say half register because it appears in app():

/c/src/project (master) [0] $ php artisan tinker
Psy Shell v0.10.4 (PHP 7.3.7 — cli) by Justin Hileman
>>> app()
=> Laravel\Lumen\Application {#11
     +router: Laravel\Lumen\Routing\Router {#12
       +app: Laravel\Lumen\Application {#11},
       +namedRoutes: [
         "graphql" => "/graphql",
         "graphql-playground" => "/graphql-playground",
       ],
     },
     +availableBindings: [
       "auth" => "registerAuthBindings",
       "auth.driver" => "registerAuthBindings",
       "Illuminate\Auth\AuthManager" => "registerAuthBindings",
       "Illuminate\Contracts\Auth\Guard" => "registerAuthBindings",
       "Illuminate\Contracts\Auth\Access\Gate" => "registerAuthBindings",
       "Illuminate\Contracts\Broadcasting\Broadcaster" => "registerBroadcastingBindings",
       "Illuminate\Contracts\Broadcasting\Factory" => "registerBroadcastingBindings",
       "Illuminate\Contracts\Bus\Dispatcher" => "registerBusBindings",
       "cache" => "registerCacheBindings",
       "cache.store" => "registerCacheBindings",
       "Illuminate\Contracts\Cache\Factory" => "registerCacheBindings",
       "Illuminate\Contracts\Cache\Repository" => "registerCacheBindings",
       "composer" => "registerComposerBindings",
       "config" => "registerConfigBindings",
       "db" => "registerDatabaseBindings",
       "Illuminate\Database\Eloquent\Factory" => "registerDatabaseBindings",
       "filesystem" => "registerFilesystemBindings",
       "filesystem.cloud" => "registerFilesystemBindings",
       "filesystem.disk" => "registerFilesystemBindings",
       "Illuminate\Contracts\Filesystem\Cloud" => "registerFilesystemBindings",
       "Illuminate\Contracts\Filesystem\Filesystem" => "registerFilesystemBindings",
       "Illuminate\Contracts\Filesystem\Factory" => "registerFilesystemBindings",
       "encrypter" => "registerEncrypterBindings",
       "Illuminate\Contracts\Encryption\Encrypter" => "registerEncrypterBindings",
       "events" => "registerEventBindings",
       "Illuminate\Contracts\Events\Dispatcher" => "registerEventBindings",
       "files" => "registerFilesBindings",
       "hash" => "registerHashBindings",
       "Illuminate\Contracts\Hashing\Hasher" => "registerHashBindings",
       "log" => "registerLogBindings",
       "Psr\Log\LoggerInterface" => "registerLogBindings",
       "queue" => "registerQueueBindings",
       "queue.connection" => "registerQueueBindings",
       "Illuminate\Contracts\Queue\Factory" => "registerQueueBindings",
       "Illuminate\Contracts\Queue\Queue" => "registerQueueBindings",
       "router" => "registerRouterBindings",
       "Psr\Http\Message\ServerRequestInterface" => "registerPsrRequestBindings",
       "Psr\Http\Message\ResponseInterface" => "registerPsrResponseBindings",
       "translator" => "registerTranslationBindings",
       "url" => "registerUrlGeneratorBindings",
       "validator" => "registerValidatorBindings",
       "Illuminate\Contracts\Validation\Factory" => "registerValidatorBindings",
       "view" => "registerViewBindings",
       "Illuminate\Contracts\View\Factory" => "registerViewBindings",
     ],
     +contextual: [],
   }
driesvints commented 4 years ago

Thanks for reporting. I sent in a PR here: https://github.com/laravel/lumen-framework/pull/1091