Framework: Container failing to resolve $customStubPath on MigrationCreator

Created on 15 Mar 2020  路  8Comments  路  Source: laravel/framework

  • Laravel Version: 7.1.13
  • PHP Version: 7.4.1
  • Database Driver & Version: MySQL 8.0.18

Description:

Apologies if this is my misunderstanding rather than an error, but there's a weird issue when creating custom versions of certain migration commands, causing the container to throw a dependency error due to a failure to resolve $customStubPath for MigrationCreator:

Unresolvable dependency resolving [Parameter #1 [ <required> $customStubPath ]] in class Illuminate\Database\Migrations\MigrationCreator

I'm able to address the issue by adding the following to my AppServiceProvider, though I'm not sure if I'm missing a more appropriate way to handle this:

$this->app->when(MigrationCreator::class)
    ->needs('$customStubPath')
    ->give(function ($app) {
        return $app->basePath('stubs');
    });

Since MigrationCreator is being registered in the MigrationServiceProvider, I'm thinking the resolution failure is an error and that the above snippet should not be necessary, though I'm not sure what's actually behind the failed resolution.

Steps To Reproduce:

  • Create a custom (even if just empty) app/Console/Commands/MigrateMakeCommand.php extending Illuminate\Database\Console\Migrations\MigrateMakeCommand
  • Run any Artisan command; this should be enough to trigger the error

All 8 comments

Your solution is the correct one. Since we bind the migrator as a singleton to the container you'll explicitly have to tell how to resolve the path if you overwrite the command.

This code doesn't work for me. On the other hand, I made 2 different packages that extends that Class so I don't think that this would be the correct approach, one would overwrite the other or am I missing something?

Me too, that code doest not work for me... The error persist...

@gmutinel I found a solution.
In my case the error message was:
Unresolvable dependency resolving [Parameter #1 [ $customStubPath ]] in class App\Database\MigrationsMigrationCreator

which IS NOT Illuminate\Database\MigrationsMigrationCreator

In my MigrationCreator, I just put $customStubPath=null on constructor since I overwrited the stubPath method and I don't need the attribute to have a "valid" value.

So checkout if you have a custom MigrationCreator class.

NOTE: I don't needed to do the solution which @thechrisroberts said. Maybe because I'm using Laravel 7.2

Hope this help to you.

Here is my MigrationCreator:

image

Damn, thanks, I was almost there!

The magic here is due to the fallback in the Illuminate\Container\Container.php file

protected function resolvePrimitive(ReflectionParameter $parameter)
    {
        if (! is_null($concrete = $this->getContextualConcrete('$'.$parameter->name))) {
            return $concrete instanceof Closure ? $concrete($this) : $concrete;
        }

        if ($parameter->isDefaultValueAvailable()) {
            return $parameter->getDefaultValue();
        }

        $this->unresolvablePrimitive($parameter);
    }

That NULL works because of that isDefaulValueAvailable().

@driesvints what do you think about adding a default value like this to the Core function in order to fix this at the base (or add a tip to the Docs at least)?

@gmutinel feel free to send in a pr

is it ok for you @didix16 ? After all, the fix is yours

Yeah, no problem. If PR pasts the tests it is ok

Was this page helpful?
0 / 5 - 0 ratings