【发布时间】:2020-09-20 18:57:35
【问题描述】:
我想制作一个包,它使用自定义存根来创建我的包所需的迁移。更准确地说,运行该命令应该为具有特定特征的模型制作数据透视表。
如果我发出“正常”命令,我可以在我的服务提供商中注册它:
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);
});
}
我知道注册命令不应该像这样运行,因为我只是将单例注册到 Laravel app 实例,但我不知道如何通过类注册它,同时确保 @ 的正确版本987654327@ 将被注入。我有点卡在这里,有什么办法吗?
【问题讨论】:
标签: php laravel service-provider