Search code examples
laravellaravel-5laravel-artisan

Laravel dependency injection in a command method


I am sorry if for someone the title of my question turns out to be very common, but the truth is that I have been trying for hours to obtain the expected result and I have not succeeded.

It happens, that I am developing a small package for Laravel, and I cannot perform a dependency injection in a method within a command that will contain the package.

Inside the directory structure of my package, I have the ServiceProvider

<?php

namespace Author\Package;

use Author\Package\Commands\BaseCommand;
use Author\Package\Contracts\MyInterface;
use Illuminate\Support\ServiceProvider;

class PackageServiceProvider extends ServiceProvider
{
    /**
     * The commands to be registered.
     *
     * @var array
     */
    protected $commands = [
        \Author\Package\Commands\ExampleCommand::class
    ];

    /**
     * Register services.
     *
     * @return void
     */
    public function register()
    {
        if (! $this->app->configurationIsCached()) {
            $this->mergeConfigFrom(__DIR__ . '/../config/package.php', 'package');
        }

        $this->app->bind(MyInterface::class, BaseCommand::class);
    }

    /**
     * Bootstrap services.
     *
     * @return void
     */
    public function boot()
    {
        if ($this->app->runningInConsole()) {
            $this->publishes([
                __DIR__ . '/../config/package.php' => config_path('package.php')
            ], 'package-config');

            $this->configureCommands();
        }

    }

    /**
     * Register the package's custom Artisan commands.
     *
     * @return void
     */
    public function configureCommands()
    {
        $this->commands($this->commands);
    }
}

As you can see from the register method, I am creating a binding for when it calls the MyInterface interface, it returns the concrete BaseCommand class

    public function register()
    {
        ...
        $this->app->bind(MyInterface::class, BaseCommand::class);
    }

The structure of the ExampleCommand file is as follows:

<?php

namespace Author\Package\Commands;

use Author\Package\Contracts\MyInterface;
use Illuminate\Console\Command;

class ExampleCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'my:command';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command Description';

    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle(MyInterface $interface)
    {
        // TODO
    }
}

But when I run the command, I get the following error:

TypeError 

Argument 1 passed to Author\Package\Commands\ExampleCommand::handle() must be an instance of Author\Package\Contracts\MyInterface, instance of Author\Package\Commands\BaseCommand given

I wonder why dependency injection is not working, in essence it should inject the concrete BaseCommand class into the handle method of the ExampleCommand class, but it isn't. I would appreciate any help you can give me.


Solution

  • Your BaseCommand must implement the interface that you have typehinted for that handle method. Dependency Injection happens before the method is called, so the container resolved your binding (as it is trying to pass an instance of BaseCommand to the method call, handle) but the binding does not return something that implements that contract so PHP won't allow that to be passed for that argument since it doesn't match the type of the argument in the signature (does not implement the contract).

    In short: if you are going to bind a concrete to an abstract, make sure the concrete is actually of the type you are binding it to.