我正在开发一个包含一些计划任务的程序包 - 是否有注册/发布它们的方式而不会影响已设置计划任务的基本应用程序?
我不想覆盖它,App/Console/Kernel.php因为基础应用程序可能已经拥有它自己的计划任务等.
小智 10
你当然可以,通过一些基本的面向对象编程的力量!
让我们在包的Console目录中创建一个Kernal类,我们将在其中进行扩展App\Console\Kernel.
<?php
namespace Acme\Package\Console;
use App\Console\Kernel as ConsoleKernel;
use Illuminate\Console\Scheduling\Schedule;
class Kernel extends ConsoleKernel
{
//
}
Run Code Online (Sandbox Code Playgroud)
schedule方法由于我们正在扩展App Console内核,因此我们需要添加相关的调度方法并调用父类的实现.这将确保任何先前安排的任务完成.
<?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);
//
}
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以按常规添加自己的计划任务.
$schedule->command('')->daily();
Run Code Online (Sandbox Code Playgroud)
我们想要将类绑定到容器,并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');
Run Code Online (Sandbox Code Playgroud)
这应该是所有需要的!
有些事情要考虑到这一点:
| 归档时间: |
|
| 查看次数: |
1414 次 |
| 最近记录: |