2

I would like to make a package which uses a custom stub for creating migrations needed for my package. More precisely, running the command should make pivot tables for models which have a specific trait present.

If I make a "normal" command, I can register it within my service provider:

public function boot()
{
    if ($this->app->runningInConsole()) {

        $this->commands([
            MakeContainerMigration::class,
        ]);
    }
}

However in this case I wanted to reuse Laravel's coding and save myself the trouble to reinvent the wheel. So my command looks like this:

class MakeContainerMigration extends MigrateMakeCommand
{
    protected $name = 'custom:make-container';

    protected $description = '...';
}

Since MigrateMakeCommand doesn't have stubs defined, but rather it's dependency MigrationCreator, I needed to find a way to provide custom stub path to it without disrupting "regular" migration stubs.

I tried doing something like this but failed:

public function register()
{
    $this->registerCreator();
    $this->registerMigrateMakeCommand();

    if ($this->app->runningInConsole()) {

        $this->commands([
            //MakeContainerMigration::class,
            'custom.command.migrate.make'
        ]);
    }
}

protected function registerCreator()
{
    $this->app->singleton('custom.migration.creator', function ($app) {
        return new MigrationCreator($app['files'], __DIR__ . '/stubs');
    });
}

protected function registerMigrateMakeCommand()
{
    $this->app->singleton('custom.command.migrate.make', function ($app) {
        $creator = $app['custom.migration.creator'];
        $composer = $app['composer'];

        return new MakeContainerMigration($creator, $composer);
    });
}

I'm aware that registering the command shouldn't function like this as I am simply registering singletons to Laravel app instance, but I have no idea how to register it through the class while ensuring that the right version of MigrationCreator will be injected. I'm kinda stuck here, is there a way to do it?

Norgul
  • 4,613
  • 13
  • 61
  • 144

1 Answers1

0

Turns out everything is working, I just needed to replace

protected $name = 'custom:make-container';

with

protected $signature = 'custom:make-container';
Norgul
  • 4,613
  • 13
  • 61
  • 144