|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PlinCode\LaravelCleanArchitecture\Commands; |
| 4 | + |
| 5 | +use Illuminate\Console\Command; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | +use Illuminate\Support\Str; |
| 8 | + |
| 9 | +class GeneratePackageCommand extends Command |
| 10 | +{ |
| 11 | + protected $signature = 'clean-arch:generate-package {name : The name of the package} |
| 12 | + {vendor : The vendor name} |
| 13 | + {--path= : Custom path for the package} |
| 14 | + {--force : Overwrite existing files}'; |
| 15 | + |
| 16 | + protected $description = 'Generate a new Laravel package with Clean Architecture structure'; |
| 17 | + |
| 18 | + protected Filesystem $files; |
| 19 | + |
| 20 | + public function __construct(Filesystem $files) |
| 21 | + { |
| 22 | + parent::__construct(); |
| 23 | + $this->files = $files; |
| 24 | + } |
| 25 | + |
| 26 | + public function handle(): int |
| 27 | + { |
| 28 | + $packageName = $this->argument('name'); |
| 29 | + $vendor = $this->argument('vendor'); |
| 30 | + $customPath = $this->option('path'); |
| 31 | + $force = $this->option('force'); |
| 32 | + |
| 33 | + $this->info("🚀 Generating package: {$vendor}/{$packageName}"); |
| 34 | + |
| 35 | + $path = $customPath ?: base_path("packages/{$vendor}/{$packageName}"); |
| 36 | + $studlyName = Str::studly($packageName); |
| 37 | + $namespace = Str::studly($vendor) . '\\' . $studlyName; |
| 38 | + |
| 39 | + $this->createPackageStructure($path, $packageName, $studlyName, $namespace, $vendor); |
| 40 | + |
| 41 | + $this->info("✅ Package {$vendor}/{$packageName} generated successfully!"); |
| 42 | + $this->info("Package location: {$path}"); |
| 43 | + $this->newLine(); |
| 44 | + $this->info('Next steps:'); |
| 45 | + $this->info('1. Add the package to your composer.json repositories'); |
| 46 | + $this->info("2. Run: composer require {$vendor}/{$packageName}"); |
| 47 | + |
| 48 | + return self::SUCCESS; |
| 49 | + } |
| 50 | + |
| 51 | + protected function createPackageStructure(string $path, string $packageName, string $studlyName, string $namespace, string $vendor): void |
| 52 | + { |
| 53 | + // Create directories |
| 54 | + $directories = [ |
| 55 | + 'src', |
| 56 | + 'src/Commands', |
| 57 | + 'stubs', |
| 58 | + 'config', |
| 59 | + 'tests', |
| 60 | + 'tests/Feature', |
| 61 | + 'tests/Unit', |
| 62 | + ]; |
| 63 | + |
| 64 | + foreach ($directories as $directory) { |
| 65 | + $fullPath = "{$path}/{$directory}"; |
| 66 | + if (! $this->files->isDirectory($fullPath)) { |
| 67 | + $this->files->makeDirectory($fullPath, 0755, true); |
| 68 | + $this->info("Created directory: {$directory}"); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Create files |
| 73 | + $this->createPackageComposer($path, $packageName, $studlyName, $namespace, $vendor); |
| 74 | + $this->createPackageServiceProvider($path, $studlyName, $namespace); |
| 75 | + $this->createPackageModel($path, $studlyName, $namespace); |
| 76 | + $this->createPackageService($path, $studlyName, $namespace); |
| 77 | + $this->createPackageReadme($path, $packageName, $studlyName, $vendor); |
| 78 | + } |
| 79 | + |
| 80 | + protected function createPackageComposer(string $path, string $packageName, string $studlyName, string $namespace, string $vendor): void |
| 81 | + { |
| 82 | + $content = json_encode([ |
| 83 | + 'name' => "{$vendor}/{$packageName}", |
| 84 | + 'description' => "Laravel package for {$studlyName}", |
| 85 | + 'type' => 'library', |
| 86 | + 'license' => 'MIT', |
| 87 | + 'authors' => [ |
| 88 | + [ |
| 89 | + 'name' => 'Your Name', |
| 90 | + 'email' => 'your.email@example.com', |
| 91 | + ], |
| 92 | + ], |
| 93 | + 'require' => [ |
| 94 | + 'php' => '^8.3', |
| 95 | + 'illuminate/support' => '^11.0|^12.0', |
| 96 | + 'illuminate/console' => '^11.0|^12.0', |
| 97 | + 'illuminate/filesystem' => '^11.0|^12.0', |
| 98 | + ], |
| 99 | + 'autoload' => [ |
| 100 | + 'psr-4' => [ |
| 101 | + str_replace('\\', '\\\\', $namespace) . '\\' => 'src/', |
| 102 | + ], |
| 103 | + ], |
| 104 | + 'autoload-dev' => [ |
| 105 | + 'psr-4' => [ |
| 106 | + str_replace('\\', '\\\\', $namespace) . '\\Tests\\' => 'tests/', |
| 107 | + ], |
| 108 | + ], |
| 109 | + 'extra' => [ |
| 110 | + 'laravel' => [ |
| 111 | + 'providers' => [ |
| 112 | + str_replace('\\', '\\\\', $namespace) . '\\' . $studlyName . 'ServiceProvider', |
| 113 | + ], |
| 114 | + ], |
| 115 | + ], |
| 116 | + 'minimum-stability' => 'stable', |
| 117 | + 'prefer-stable' => true, |
| 118 | + ], JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
| 119 | + |
| 120 | + $this->files->put("{$path}/composer.json", $content); |
| 121 | + $this->info('Created: composer.json'); |
| 122 | + } |
| 123 | + |
| 124 | + protected function createPackageServiceProvider(string $path, string $studlyName, string $namespace): void |
| 125 | + { |
| 126 | + $stub = $this->getStub('package-service-provider'); |
| 127 | + $content = str_replace( |
| 128 | + ['{{Namespace}}', '{{StudlyName}}'], |
| 129 | + [$namespace, $studlyName], |
| 130 | + $stub |
| 131 | + ); |
| 132 | + |
| 133 | + $this->files->put("{$path}/src/{$studlyName}ServiceProvider.php", $content); |
| 134 | + $this->info("Created: src/{$studlyName}ServiceProvider.php"); |
| 135 | + } |
| 136 | + |
| 137 | + protected function createPackageModel(string $path, string $studlyName, string $namespace): void |
| 138 | + { |
| 139 | + $stub = $this->getStub('package-model'); |
| 140 | + $content = str_replace( |
| 141 | + ['{{Namespace}}', '{{StudlyName}}'], |
| 142 | + [$namespace, $studlyName], |
| 143 | + $stub |
| 144 | + ); |
| 145 | + |
| 146 | + $this->files->put("{$path}/src/{$studlyName}.php", $content); |
| 147 | + $this->info("Created: src/{$studlyName}.php"); |
| 148 | + } |
| 149 | + |
| 150 | + protected function createPackageService(string $path, string $studlyName, string $namespace): void |
| 151 | + { |
| 152 | + $stub = $this->getStub('package-service'); |
| 153 | + $content = str_replace( |
| 154 | + ['{{Namespace}}', '{{StudlyName}}'], |
| 155 | + [$namespace, $studlyName], |
| 156 | + $stub |
| 157 | + ); |
| 158 | + |
| 159 | + $this->files->put("{$path}/src/{$studlyName}Service.php", $content); |
| 160 | + $this->info("Created: src/{$studlyName}Service.php"); |
| 161 | + } |
| 162 | + |
| 163 | + protected function createPackageReadme(string $path, string $packageName, string $studlyName, string $vendor): void |
| 164 | + { |
| 165 | + $stub = $this->getStub('package-readme'); |
| 166 | + $content = str_replace( |
| 167 | + ['{{PackageName}}', '{{StudlyName}}', '{{VendorName}}'], |
| 168 | + [$packageName, $studlyName, $vendor], |
| 169 | + $stub |
| 170 | + ); |
| 171 | + |
| 172 | + $this->files->put("{$path}/README.md", $content); |
| 173 | + $this->info('Created: README.md'); |
| 174 | + } |
| 175 | + |
| 176 | + protected function getStub(string $stub): string |
| 177 | + { |
| 178 | + $stubPath = __DIR__ . "/../../stubs/{$stub}.stub"; |
| 179 | + |
| 180 | + if (! $this->files->exists($stubPath)) { |
| 181 | + throw new \Exception("Stub file not found: {$stubPath}"); |
| 182 | + } |
| 183 | + |
| 184 | + return $this->files->get($stubPath); |
| 185 | + } |
| 186 | +} |
0 commit comments