My Artisan Command

By: Gio Dela Peña:


use Illuminate\Support\Facades\File;


Artisan::command('make:service-impl-controller {param}', function ($param) {
    $serviceInterfaceDirectory = app_path("Http/Src/v1/Controllers");
    $serviceInterfacePath = "{$serviceInterfaceDirectory}/Controller.php";

    if (!File::exists($serviceInterfaceDirectory)) {
        File::makeDirectory($serviceInterfaceDirectory, 0755, true);

        $serviceInterfaceContent = "<?php\n\n";
        $serviceInterfaceContent .= "namespace App\Http\Src\\v1\Controllers;\n\n";
        $serviceInterfaceContent .= "use App\Http\Src\\v1\Services\\{$param}Service;\n\n";
        $serviceInterfaceContent .= "use Illuminate\Foundation\Auth\Access\AuthorizesRequests;\n";
        $serviceInterfaceContent .= "use Illuminate\Foundation\Validation\ValidatesRequests;\n";
        $serviceInterfaceContent .= "use Illuminate\Routing\Controller as BaseController;\n\n";
        $serviceInterfaceContent .= "class Controller extends BaseController\n";
        $serviceInterfaceContent .= "{\n";
        $serviceInterfaceContent .= "    use AuthorizesRequests, ValidatesRequests;\n";
        $serviceInterfaceContent .= "}\n";
        File::put($serviceInterfacePath, $serviceInterfaceContent);

        $this->info("Base Controller generated successfully: {$serviceInterfacePath}");
    }


    $serviceInterfaceDirectory = app_path("Http/Src/v1/Controllers");
    $serviceInterfacePath = "{$serviceInterfaceDirectory}/{$param}Controller.php";

    if (!File::exists($serviceInterfaceDirectory)) {
        File::makeDirectory($serviceInterfaceDirectory, 0755, true);
    }

    $injection = '$'.strtolower($param);

    $serviceInterfaceContent = "<?php\nnamespace App\Http\Src\\v1\Controllers;\n\n";
    $serviceInterfaceContent .= "use App\Http\Src\\v1\Services\\{$param}Service;\n";
    $serviceInterfaceContent .= "use App\Http\Src\\v1\Controllers\Controller;\n";
    $serviceInterfaceContent .= "class {$param}Controller extends Controller\n";
    $serviceInterfaceContent .= "{\n";
    $serviceInterfaceContent .= "    public function __construct(public {$param}Service {$injection}Service){\n";
    $serviceInterfaceContent .= "       \n";
    $serviceInterfaceContent .= "    }\n\n";
    $serviceInterfaceContent .= "    public function exampleMethod(){\n";
    $serviceInterfaceContent .= "       return $ this->{$injection}Service->exampleMethod();\n";
    $serviceInterfaceContent .= "    }\n";
    $serviceInterfaceContent .= "}\n";
    File::put($serviceInterfacePath, $serviceInterfaceContent);

    $this->info("Controller generated successfully: {$serviceInterfacePath}");


    $serviceInterfaceDirectory = app_path("Http/Src/v1/Services");
    $serviceInterfacePath = "{$serviceInterfaceDirectory}/{$param}Service.php";

    if (!File::exists($serviceInterfaceDirectory)) {
        File::makeDirectory($serviceInterfaceDirectory, 0755, true);
    }

    $serviceInterfaceContent = "<?php\n\nnamespace App\Http\Src\\v1\Services;\n\ninterface {$param}Service\n{\n";
    $serviceInterfaceContent .= "    public function exampleMethod();\n";
    $serviceInterfaceContent .= "}\n";
    File::put($serviceInterfacePath, $serviceInterfaceContent);

    $this->info("Service interface generated successfully: {$serviceInterfacePath}");


    $serviceInterfaceDirectory = app_path("Http/Src/v1/Implementations");
    $serviceInterfacePath = "{$serviceInterfaceDirectory}/{$param}ServiceImpl.php";

    if (!File::exists($serviceInterfaceDirectory)) {
        File::makeDirectory($serviceInterfaceDirectory, 0755, true);
    }

    $serviceInterfaceContent = "<?php\n\nnamespace App\Http\Src\\v1\Implementations;\n\n\nuse App\Http\Src\\v1\Services\\{$param}Service;\n\nClass {$param}ServiceImpl implements {$param}Service\n{\n";
    $serviceInterfaceContent .= "    public function exampleMethod(){\n";
    $serviceInterfaceContent .= "       try{\n\n";
    $serviceInterfaceContent .= "       }\n";
    $serviceInterfaceContent .= "       catch (\Exception $ error) {\n";
    $serviceInterfaceContent .= "           return response()->json([\n";
    $serviceInterfaceContent .= "               'success' => false,\n";
    $serviceInterfaceContent .= "               'message' => $ error->getMessage()\n";
    $serviceInterfaceContent .= "           ], 500);\n";
    $serviceInterfaceContent .= "       }\n";
    $serviceInterfaceContent .= "    }\n";
    $serviceInterfaceContent .= "}\n";
    File::put($serviceInterfacePath, $serviceInterfaceContent);

    $this->info("Service Implementation generated successfully: {$serviceInterfacePath}");
});