Laravel包自定义迁移命令

诺古尔

我想制作一个使用自定义存根创建我的程序包所需的迁移的程序包。更准确地说,运行命令应该为具有特定特征的模型制作数据透视表。

如果我执行“正常”命令,则可以在服务提供商中注册它:

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

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

但是在这种情况下,我想重用Laravel的编码,从而省去了重新发明轮子的麻烦。所以我的命令看起来像这样:

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

    protected $description = '...';
}

由于MigrateMakeCommand未定义存根,而是依赖关系MigrationCreator因此,我需要找到一种方法来为其提供自定义存根路径,而又不会破坏“常规”迁移存根。

我试图做这样的事情,但失败了:

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);
    });
}

我知道注册命令不应该像这样运行,因为我只是将单例注册到Laravelapp实例,但是我不知道如何在确保正确版本的同时通过类注册它MigrationCreator我有点卡在这里,有办法吗?

诺古尔

事实证明一切正常,我只需要更换

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章