久々に投稿
Wed May 29, 2019 · 367 words

モチベーション維持が大変

プログラミングが好きで今の仕事をやっている(半分正解)

技術の探求は大好きなんだけどもめんどくさがりすぎる。

それと人と微妙に感覚が異なっているのが自分的に納得いかない

ラバーダッキング的な学習方法が主だからより一層ね

それはさておき

最近職場で一人でもくもくと開発と PM 的なことを並行して行なっているんだけども その際に Laravel でちょっとしたコマンドを作成した

repository pattern を採用しているんだけど DDD に寄せている設計で書いてて Laravel の artisan command には repository file の作成 command なんてなくてただただだるい作業が必ず発生するからめんどくて作った話

ただ参考にしたサイトのコードにちょっと手を加えた程度

あるとないとではだいぶ違う

<?php
declare(strict_types=1);

namespace App\Console\Commands;

use Illuminate\Console\Command;

/**
 * CreateReposioryFileCommand class
 */
class CreateReposioryFileCommand extends Command
{
    /**
     * @const string repository dir path
     */
    const REPOSITORIES_PATH = 'app/Repositories/';

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'make:repository {repositoryName : The name of repository}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Create repository files';

    /**
     * @var string
     */
    private $fileName;

    /**
     * @var string
     */
    private $dirName;

    /**
     * @var string
     */
    private $repositroyFileName;

    /**
     * @var string
     */
    private $interfaceFileName;

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

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->fileName = $this->argument('repositoryName');

        if (is_null($this->fileName)) {
            $this->error('Repository Name invalid');
        }
        $this->dirName = $this->ask('new directory name. or use directory name');

        if (is_null($this->dirName)) {
            $this->error('Directory required!');
        }

        if (!$this->isExistDirectory()) {
            $this->createDirectory();
        }

        $this->repositroyFileName = self::REPOSITORIES_PATH . $this->dirName . '/' . $this->fileName . 'Repository.php';
        $this->interfaceFileName = self::REPOSITORIES_PATH . $this->dirName . '/' . $this->fileName . 'Interface.php';
        if ($this->isExistFiles()) {
            $this->error('already exist');
            return;
        }

        $this->creatRepositoryFile();
        $this->createInterFaceFile();
        $this->info('create successfully');
    }

    /**
     * Repositoryのfileを作成する
     * @return void
     */
    private function creatRepositoryFile(): void
    {
        $content = "<?php\ndeclare(strict_types=1);\n\nnamespace App\\Repositories\\$this->dirName;\n\nfinal class $this->fileName" . "Repository implements $this->fileName" . "Interface\n{\n}\n";
        file_put_contents($this->repositroyFileName, $content);
    }

    /**
     * Interfaceのfileを作成する
     * @return void
     */
    private function createInterFaceFile(): void
    {
        $content = "<?php\ndeclare(strict_type=1);\n\nnamespace App\\Repositories\\$this->dirName;\n\ninterface $this->fileName" . "Interface\n{\n}\n";
        file_put_contents($this->interfaceFileName, $content);
    }

    /**
     * 同名fileの確認
     *
     * @return bool
     */
    private function isExistFiles()
    {
        if (file_exists($this->repositroyFileName) && file_exists($this->interfaceFileName)) {
            return true;
        }
        return false;
    }

    /**
     * directoryの存在確認
     *
     * @return bool
     */
    private function isExistDirectory()
    {
        if (file_exists(self::REPOSITORIES_PATH . $this->dirName)) {
            return true;
        }
        return false;
    }

    /**
     * 指定名でdirectoryの作成
     *
     * @return void
     */
    private function createDirectory()
    {
        mkdir(self::REPOSITORIES_PATH . $this->dirName, 0755, true);
    }
}

です。

以上

あらかじめ、app/以下に Repository という directory はある前提になっているけども

あと repository と interface は分けるべきだって意見もあると思うんですがーー

個人的には、やりやすいっちゃやりやすいんでこっちにしても

file 数増えるってなるかもだけどもどうあがいても増えるものは増えるし

directory に関しては、機能単位で作成するでもよし repository 単位で作成するでもよし prefix 単位(機能単位と変わらんと思いますが)でわけてもよしって思います。

プログラミングって教的問題が多くまた時代の流れも多いし女子みたくやっぱあれがいいよねこれがいいよねって風潮もあるんで極論正解は、ないんじゃないかなって思っています。ただ間違いはあると思ってます。

なので一定水準以上の正解を超えるような設計でいいと思います。

ただ間違えはちゃんと排除しましょう。

設計書等書くくらいならコードで表現しましょう!

扱う道具のお作法は守りましょう!

ってのが最近常々思うことです。

ポエムは以上です。

ではまた。


back · Top · About · Resume · Posts ·