3
0

PHPのWebアプリケーション向けのフレームワークのLaravelは便利な機能が多いですが、その中でも特に誰得だと感じたのがphp artisan inspireコマンドです。

試しに実行してみると

$ php artisan inspire
I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison

このコマンドがしていることをソースコードリードをして理解してみます。

(ソースコードリードの一例として生暖かい目で見守ってください)

Laravelのコマンド

11.xのLaravelのソースコードにはroutes/console.phpに以下のように記述されています。

use Illuminate\Foundation\Inspiring;
use Illuminate\Support\Facades\Artisan;

Artisan::command('inspire', function () {
    $this->comment(Inspiring::quote());
})->purpose('Display an inspiring quote');

このInspiringクラスはIlluminate\Foundation\Inspiringに定義されています。

次にInspiringクラスのファイルを見てみます。

<?php

namespace Illuminate\Foundation;

use Illuminate\Support\Collection;

class Inspiring
{
    public static function quote()
    {
        return static::quotes()
            ->map(fn ($quote) => static::formatForConsole($quote))
            ->random();
    }

    public static function quotes()
    {
        return Collection::make([
            'Act only according to that maxim whereby you can, at the same time, will that it should become a universal law. - Immanuel Kant',
            ~~~
            'I have not failed. I\'ve just found 10,000 ways that won\'t work. - Thomas Edison',
            ~~~
        ]);
    }

    protected static function formatForConsole($quote)
    {
        [$text, $author] = str($quote)->explode('-');

        return sprintf(
            "\n  <options=bold>“ %s ”</>\n  <fg=gray>— %s</>\n",
            trim($text),
            trim($author),
        );
    }
}

(途中のコメントや一部の配列内の文字列は省略しています)

staticメソッドのquoteメソッドはquotesメソッドで返されるCollectionの中からランダムに一つの引用を返します。

(ちなみにこのファイルにはアスキーアートが含まれていた。誰得)

最後に

Laravel開発者の方へ、たまにはこのコマンドを実行してインスパイアされてください。

3
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0