image.png
今天在 Laravel News 网站无意间看到「Laravel Zero」,主要被这个 Slogan 吸引住了。

像写 Laravel 代码那样优雅地写 console/command-line application;而且比 Laravel 和 Lumen 简单,极易上手。

下面我们开始写个简单的 demo 入手:

创建项目

// 1. 创建 notes 项目脚手架
composer create-project --prefer-dist laravel-zero/laravel-zero FanlyConsole

// 重命名
php application app:rename notes

// 创建新的命令
php notes make:command AddCommand

add memos

在项目路径下 app/Commands/MemosCommand.php

  • 修改 $signature 值,命令值为:add,一个参数名为:memo;
  • 修改 $description,描述每个 Command 含义;
  • 最后在 handle() 函数,写入执行 Command 的代码,其中在此函数中依赖注入我们的 ServiceProvider:

具体代码简单,直接命令:

php <your-app-name> make:command <NewCommand>

先写一个输出试试:

<?php  
  
namespace App\Commands;  
  
use Illuminate\Console\Scheduling\Schedule;  
use LaravelZero\Framework\Commands\Command;  
use function Termwind\render;  
  
class MemosCommand extends Command  
{  
    /**  
     * The signature of the command.     
     *     
     * @var string  
     */    
     protected $signature = 'memo:create {text: memo text}';  
  
    /**  
     * The description of the command.     
     *     
     * @var string  
     */    
     protected $description = 'create memo';  
  
    /**  
     * Execute the console command.     
     **/
     public function handle(): void  
     {  
        $this->info('Hello yemeishu.');  
     }  
  
    /**  
     * Define the command's schedule.     */    
     public function schedule(Schedule $schedule): void  
     {  
        // $schedule->command(static::class)->everyMinute();  
     }  
}

执行命令运行看看效果:

php application memo:create hello

image.png

接下来通过一系列笔记,记录手写一个 FanlyConsole 命令工具,包括:

  1. 手写 Service Providers。
  2. 手写网络请求。
  3. 将 Memos 同步进【叶梅树的碎碎念】 https://memos.coding01.cn/
  4. 打包。
  5. and so on

如果觉得文章内容对您有用 打赏

Buy Me A Coffee