このすみノート

Webエンジニアが技術や趣味を書くブログです。

Laravelには偉人の名言を出力する機能があるらしい

先日からLaravelを学習中なのですが、Laravelには『偉人の名言を表示する』という、不思議な機能が存在することを知りました。

気になって使ってみたので、紹介します。

まずはLaravelをインストールする

偉人の名言機能は、Laravelインストール後、すぐに使い始めることができます。 そのため、まずはLaravelをインストールします。

# Laravelを適当にインストールする
$ php composer.phar create-project laravel/laravel blog
Creating a "laravel/laravel" project at "./blog"
Installing laravel/laravel (v8.5.15)
Created project in //blog
# ...

# 作成したプロジェクト直下に移動する
$ cd blog

偉人の名言をCLIで表示する

偉人の名言を表示するには、プロジェクト直下でphp artisan inspireコマンドを実行します。

$ php artisan inspire
Smile, breathe, and go slowly. - Thich Nhat Hanh

# なお、偉人の名言は複数人登録されており、実行する度に結果が変化します
$ php artisan inspire
Happiness is not something readymade. It comes from your own actions. - Dalai Lama

$ php artisan inspire
Be present above all else. - Naval Ravikant

$ php artisan inspire
It is not the man who has too little, but the man who craves more, that is poor. - Seneca

クラス経由で実行する場合

クラス経由で実行するには、Illuminate\Foundation\Inspiringを使います。

# 試しに偉人の名言を表示するCLIを自作してみる
# まずは雛形を作成し、それを編集する
$ php artisan make:command SampleInspire
$ vim app/Console/Commands/SampleInspire.php

# 自作したCLIを実行してみる
$ php artisan command:sampleinspire
Because you are alive, everything is possible. - Thich Nhat HanhIt is not the man who has too little, but the man who craves more, that is poor. - Seneca

app/Console/Commands/SampleInspire.php

参考として、作成したコマンドを掲載します。

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
// Illuminate\Foundation\Inspiringを使います
use Illuminate\Foundation\Inspiring;

class SampleInspire extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    // 実行コマンド名を定義する
    protected $signature = 'command:sampleinspire';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        // 偉人の名言を出力する
        echo Inspiring::quote(). "\n";
        return 0;
    }
}

フレームワーク内の実装コードを確認してみる

偉人の名言はInspiring::quote()で表示できるので、それに対応したクラスが存在するはずです。 探してみたら、確かにありました。

以下が本記事でインストールしたLaravelにおける、./vendor/laravel/framework/src/Illuminate/Foundation/Inspiring.phpの実際のソースコードです。

ソースコードを確認すると、偉人の名言がハードコードされていることがわかります。

<?php

namespace Illuminate\Foundation;

use Illuminate\Support\Collection;

class Inspiring
{
    /**
     * Get an inspiring quote.
     *
     * Taylor & Dayle made this commit from Jungfraujoch. (11,333 ft.)
     *
     * May McGinnis always control the board. #LaraconUS2015
     *
     * RIP Charlie - Feb 6, 2018
     *
     * @return string
     */
    public static function quote()
    {
        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',
            'An unexamined life is not worth living. - Socrates',
            'Be present above all else. - Naval Ravikant',
            'Happiness is not something readymade. It comes from your own actions. - Dalai Lama',
            'He who is contented is rich. - Laozi',
            'I begin to speak only when I am certain what I will say is not better left unsaid - Cato the Younger',
            'If you do not have a consistent goal in life, you can not live it in a consistent way. - Marcus Aurelius',
            'It is not the man who has too little, but the man who craves more, that is poor. - Seneca',
            'It is quality rather than quantity that matters. - Lucius Annaeus Seneca',
            'Knowing is not enough; we must apply. Being willing is not enough; we must do. - Leonardo da Vinci',
            'Let all your things have their places; let each part of your business have its time. - Benjamin Franklin',
            'No surplus words or unnecessary actions. - Marcus Aurelius',
            'Order your soul. Reduce your wants. - Augustine',
            'People find pleasure in different ways. I find it in keeping my mind clear. - Marcus Aurelius',
            'Simplicity is an acquired taste. - Katharine Gerould',
            'Simplicity is the consequence of refined emotions. - Jean D\'Alembert',
            'Simplicity is the essence of happiness. - Cedric Bledsoe',
            'Simplicity is the ultimate sophistication. - Leonardo da Vinci',
            'Smile, breathe, and go slowly. - Thich Nhat Hanh',
            'The only way to do great work is to love what you do. - Steve Jobs',
            'The whole future lies in uncertainty: live immediately. - Seneca',
            'Very little is needed to make a happy life. - Marcus Antoninus',
            'Waste no more time arguing what a good man should be, be one. - Marcus Aurelius',
            'Well begun is half done. - Aristotle',
            'When there is no desire, all things are at peace. - Laozi',
            'Walk as if you are kissing the Earth with your feet. - Thich Nhat Hanh',
            'Because you are alive, everything is possible. - Thich Nhat Hanh',
            'Breathing in, I calm body and mind. Breathing out, I smile. - Thich Nhat Hanh',
            'Life is available only in the present moment. - Thich Nhat Hanh',
            'The best way to take care of the future is to take care of the present moment. - Thich Nhat Hanh',
        ])->random();
    }
}

なぜ偉人の名言を表示する機能があるのか

理由が気になるので調べてみたのですが、残念ながら答えにたどり着けませんでした。

ただ、この機能が実装されるにいたったモチベーションは興味があるので、再度調べてみようと思います。 続報を手に入れたら、改めて追記します。

まとめ

偉人の名言表示は、Laravelインストール後、すぐ使うことができるお手軽機能です。

Hello world!的な局面で、役に立つことがあるかもしれません。

参考サイト様

www.kokomite.me

neco.sakuratan.com