0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

リポジトリを作成するコマンドの作成

Posted at

stubsの作成

スタブディレクトリの作成

mkdir -p /src/api/stubs

スタブファイルの作成

touch /src/api/stubs/repository.interface.stub
touch /src/api/stubs/repository.stub

repository.interface.stub

<?php

namespace {{ namespace }};

interface {{ class }}
{
    //
}

repository.stub

<?php

namespace {{ namespace }};

use {{ namespace }}\Interfaces\{{ class }}Interface;

class {{ class }} implements {{ class }}Interface
{
    //
}

chmod 644 プロジェクトルート/stubs/repository.stub
chmod 644 プロジェクトルート/stubs/repository.interface.stub

コマンド作成

php artisan make:command MakeRepositoryCommand

app/Console/Commands/MakeRepositoryCommand.php

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\File;

class MakeRepositoryCommand extends Command
{
    protected $signature = 'make:repository {name}';
    protected $description = 'Create a new repository and interface';

    public function handle()
    {
        $name = $this->argument('name');
        $interfaceName = "{$name}Interface";
        
        $this->createInterface($interfaceName);
        $this->createRepository($name, $interfaceName);
        
        $this->info('Repository created successfully!');
    }

    private function createInterface($name)
    {
        $stub = File::get(base_path('stubs/repository.interface.stub'));
        $stub = str_replace(
            ['{{ namespace }}', '{{ class }}'],
            ['App\\Repositories\\Interfaces', $name],
            $stub
        );

        if (!File::exists(app_path('Repositories/Interfaces'))) {
            File::makeDirectory(app_path('Repositories/Interfaces'), 0755, true);
        }

        File::put(app_path("Repositories/Interfaces/{$name}.php"), $stub);
    }

    private function createRepository($name, $interfaceName)
    {
        $stub = File::get(base_path('stubs/repository.stub'));
        $stub = str_replace(
            ['{{ namespace }}', '{{ class }}', '{{ interface }}'],
            ['App\\Repositories', $name, $interfaceName],
            $stub
        );

        if (!File::exists(app_path('Repositories'))) {
            File::makeDirectory(app_path('Repositories'), 0755, true);
        }

        File::put(app_path("Repositories/{$name}.php"), $stub);
    }
}

使用方法

サービスクラスの作成
php artisan make:repository リポジトリ名Repository

0
0
0

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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?