|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace ItvisionSy\Laravel\Modules\Commands; |
| 4 | + |
| 5 | +use Illuminate\Contracts\Bus\SelfHandling; |
| 6 | +use Illuminate\Filesystem\Filesystem; |
| 7 | + |
| 8 | +class MakeModule extends \Illuminate\Console\Command implements SelfHandling |
| 9 | +{ |
| 10 | + |
| 11 | + protected $signature = 'modules:make |
| 12 | + {id : the ID of the module. Should be unique across modules} |
| 13 | + {name : the display name of the module} |
| 14 | + {--url= : the URL/route-names part for the module} |
| 15 | + '; |
| 16 | + protected $description = 'Makes a new module'; |
| 17 | + |
| 18 | + /** |
| 19 | + * @var Filesystem |
| 20 | + */ |
| 21 | + protected $fileSystem; |
| 22 | + |
| 23 | + /** |
| 24 | + * Create a new command instance. |
| 25 | + * @param Filesystem $fileSystem |
| 26 | + */ |
| 27 | + public function __construct(Filesystem $fileSystem) |
| 28 | + { |
| 29 | + parent::__construct(); |
| 30 | + $this->fileSystem = $fileSystem; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * Execute the command. |
| 35 | + * |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function handle() |
| 39 | + { |
| 40 | + //input |
| 41 | + $id = $this->argument('id'); |
| 42 | + $name = $this->argument('name'); |
| 43 | + $url = $this->option('url') ?: str_slug($id); |
| 44 | + |
| 45 | + //subs |
| 46 | + $path = rtrim(config('modules.directory'), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR . $id . DIRECTORY_SEPARATOR; |
| 47 | + $className = config('modules.class_name'); |
| 48 | + $ds = DIRECTORY_SEPARATOR; |
| 49 | + |
| 50 | + //stub data |
| 51 | + $stubData = [ |
| 52 | + "id" => $id, |
| 53 | + "name" => $name, |
| 54 | + "namespace" => trim(config("modules.namespace"), "\\"), |
| 55 | + "class" => $className, |
| 56 | + "url_name" => $url, |
| 57 | + ]; |
| 58 | + |
| 59 | + $this->makeDirectory($path); |
| 60 | + $this->makeDirectory("{$path}Views"); |
| 61 | + $this->makeDirectory("{$path}Models"); |
| 62 | + $this->makeDirectory("{$path}Http{$ds}Controllers"); |
| 63 | + $this->copyStub("Module.php.stub", "{$path}{$className}.php", $stubData + []); |
| 64 | + $this->copyStub("routes.php.stub", "{$path}Http{$ds}routes.php", $stubData + []); |
| 65 | + $this->copyStub("Controller.php.stub", "{$path}Http{$ds}Controllers{$ds}WelcomeController.php", $stubData + []); |
| 66 | + $this->copyStub("index.blade.php.stub", "{$path}Views{$ds}index.blade.php", $stubData + []); |
| 67 | + |
| 68 | + $this->info("Module {$id} has been created in {$path}"); |
| 69 | + |
| 70 | + } |
| 71 | + |
| 72 | + protected function makeDirectory($path, $mode = 0777) |
| 73 | + { |
| 74 | + if (!$this->fileSystem->isDirectory($path)) { |
| 75 | + $this->fileSystem->makeDirectory($path, $mode, true, true); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + protected function copyStub($stubName, $filePath, array $values) |
| 80 | + { |
| 81 | + $path = __DIR__ . DIRECTORY_SEPARATOR . 'stubs' . DIRECTORY_SEPARATOR . 'MakeModule' . DIRECTORY_SEPARATOR . $stubName; |
| 82 | + $content = preg_replace_callback("/\{\{([a-zA-Z_\-]+)\}\}/", function ($matches) use ($values) { |
| 83 | + return $values[$matches[1]]; |
| 84 | + }, file_get_contents($path)); |
| 85 | + if ($this->fileSystem->exists($filePath)) { |
| 86 | + $this->fileSystem->delete($filePath); |
| 87 | + } |
| 88 | + $this->fileSystem->put($filePath, $content); |
| 89 | + } |
| 90 | +} |
0 commit comments