我正在开发一个包含一些计划任务的程序包 - 是否有注册/发布它们的方式而不会影响已设置计划任务的基本应用程序?
我不想覆盖App/Console/Kernel.php
,因为基本应用程序可能已经拥有了自己的计划任务等。
答案 0 :(得分:10)
你当然可以,通过一些基本的面向对象编程的力量!
让我们在您的软件包的控制台目录中创建一个Kernal类,我们将在其中扩展App\Console\Kernel
。
<?php
namespace Acme\Package\Console;
use App\Console\Kernel as ConsoleKernel;
use Illuminate\Console\Scheduling\Schedule;
class Kernel extends ConsoleKernel
{
//
}
schedule
方法由于我们正在扩展App Console内核,因此我们要添加相关的调度方法并调用父类&#39;实施它。这将确保任何先前安排的任务完成。
<?php
namespace Acme\Package\Console;
use App\Console\Kernel as ConsoleKernel;
use Illuminate\Console\Scheduling\Schedule;
class Kernel extends ConsoleKernel
{
/**
* Define the package's command schedule.
*
* @param \Illuminate\Console\Scheduling\Schedule $schedule
* @return void
*/
protected function schedule(Schedule $schedule)
{
parent::schedule($schedule);
//
}
}
现在您可以按常规添加自己的计划任务。
$schedule->command('')->daily();
我们希望将类绑定到容器,并在我们的包的服务提供商的make
方法中register
:
$this->app->singleton('acme.package.console.kernel', function($app) {
$dispatcher = $app->make(\Illuminate\Contracts\Events\Dispatcher::class);
return new \Acme\Package\Console\Kernel($app, $dispatcher);
});
$this->app->make('acme.package.console.kernel');
这应该是所有必需的!
有些事情要考虑到这一点:
答案 1 :(得分:-1)
在您的包裹服务提供商中,执行以下操作:
/** @var array list of commands to be registered in the service provider */
protected $moreCommands = [
\My\Package\CommandOne::class,
\My\Package\CommandTwo::class,
\My\Package\CommandThree::class,
];
然后在服务提供者的boot()方法中执行以下操作:
$this->commands($this->moreCommands);
非常好的问题,顺便说一下。让我搜索Laravel API文档以找到答案,当我找到它时,我在我自己的软件包中实现了它。