Skip to content

Conversation

helmutschneider
Copy link

Sometimes database migrations have dependencies. Currently migration classes are assumed to have no constructor dependencies at all and are instantiated with new $class. This change instead resolves the migration with the global app($class). Dependencies are therefore auto-injected by the DI-container.

interface FooInterface
{
}

class Migration extends \Illuminate\Database\Migrations\Migration
{

    function __construct(FooInterface $foo)
    {
        // code
    }

    public function up() {}
    public function down() {}

}

Ideally the container itself would be injected into the migrator so we don't need to use app(), but I guess that's a little too ambitious.

Feedback is appreciated.

@GrahamCampbell
Copy link
Collaborator

Sorry, but you cannot call a foundation function from the database package.

@GrahamCampbell GrahamCampbell changed the title Inject migration dependencies from container [5.3] Inject migration dependencies from container Apr 7, 2016
@GrahamCampbell
Copy link
Collaborator

This won't be accepted anyway I'm afraid, and infact previous PRs for the same idea have already been rejected. This is because there is no real use case here, since there's little point unit testing migrations. Instead of using dependency injection, you can just actually write app(FooInterface::class)->... in your code.

@helmutschneider
Copy link
Author

Ok I see. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants