feat: command
Some checks are pending
Build Docker / build (push) Waiting to run
Some checks are pending
Build Docker / build (push) Waiting to run
This commit is contained in:
57
app/Command/CronTaskCommand.php
Normal file
57
app/Command/CronTaskCommand.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
/**
|
||||
* This file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Devtool\Generator\GeneratorCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
#[Command]
|
||||
class CronTaskCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* 构造方法
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('gen:cron');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成
|
||||
* @return void
|
||||
*/
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setDescription('Create a new cron task class');
|
||||
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取stub文件
|
||||
* @return string
|
||||
*/
|
||||
protected function getStub(): string
|
||||
{
|
||||
return __DIR__ . '/stubs/cron.stub';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认命名空间
|
||||
* @return string
|
||||
*/
|
||||
protected function getDefaultNamespace(): string
|
||||
{
|
||||
return 'App\\Cron';
|
||||
}
|
||||
}
|
||||
49
app/Command/ServiceCommand.php
Normal file
49
app/Command/ServiceCommand.php
Normal file
@@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
namespace App\Command;
|
||||
|
||||
use Hyperf\Command\Annotation\Command;
|
||||
use Hyperf\Devtool\Generator\GeneratorCommand;
|
||||
use Psr\Container\ContainerInterface;
|
||||
|
||||
#[Command]
|
||||
class ServiceCommand extends GeneratorCommand
|
||||
{
|
||||
/**
|
||||
* 构造方法
|
||||
* @param ContainerInterface $container
|
||||
*/
|
||||
public function __construct(protected ContainerInterface $container)
|
||||
{
|
||||
parent::__construct('gen:service');
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成
|
||||
* @return void
|
||||
*/
|
||||
public function configure(): void
|
||||
{
|
||||
$this->setDescription('Create a new service class');
|
||||
|
||||
parent::configure();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取stub文件
|
||||
* @return string
|
||||
*/
|
||||
protected function getStub(): string
|
||||
{
|
||||
return __DIR__ . '/stubs/service.stub';
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取默认命名空间
|
||||
* @return string
|
||||
*/
|
||||
protected function getDefaultNamespace(): string
|
||||
{
|
||||
return 'App\\Service';
|
||||
}
|
||||
}
|
||||
23
app/Command/stubs/cron.stub
Normal file
23
app/Command/stubs/cron.stub
Normal file
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
/**
|
||||
* This crontab file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace %NAMESPACE%;
|
||||
|
||||
use Hyperf\Crontab\Annotation\Crontab;
|
||||
|
||||
#[Crontab(rule: "* * * * *", name: "%CLASS%", singleton: true , callback: "execute", memo: "这是一个示例的定时任务")]
|
||||
class %CLASS%
|
||||
{
|
||||
public function execute()
|
||||
{
|
||||
//todo Write logic
|
||||
var_dump(date('Y-m-d H:i:s', time()));
|
||||
}
|
||||
}
|
||||
19
app/Command/stubs/service.stub
Normal file
19
app/Command/stubs/service.stub
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
/**
|
||||
* This service file is part of item.
|
||||
*
|
||||
* @author ctexthuang
|
||||
* @contact ctexthuang@qq.com
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace %NAMESPACE%;
|
||||
|
||||
class %CLASS% extends
|
||||
{
|
||||
public function handle()
|
||||
{
|
||||
//todo Write logic
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user