diff --git a/src/Console/MakeBuilderCommand.php b/src/Console/MakeBuilderCommand.php new file mode 100644 index 000000000..f09f86aa0 --- /dev/null +++ b/src/Console/MakeBuilderCommand.php @@ -0,0 +1,197 @@ +info('Verifying a few things...'); + $this->verify(); + $this->info('Setting up your Builder...'); + + // Building model.. + $this->createBuildingModel(); + $this->createBuildingModelObserver(); + $this->createBuilderModelFolder(); + + // Builder class.. + $this->createBuilderClass(); + + // Builder model.. + $this->createBuilderModel(); + + $this->info('Builder created successfully.'); + } + + /** + * Create Building Model. + * + * @return void + */ + protected function createBuildingModel() + { + list($path, $stub) = $this->getPath('building_model'); + + $name = Str::studly($this->argument('name')); + $content = File::get($stub); + + File::put($path, strtr($content, [ + '{name}' => $name, + ])); + } + + /** + * Create Building Model Observer. + * + * @return void + */ + protected function createBuildingModelObserver() + { + list($path, $stub) = $this->getPath('building_model_observer'); + + $name = Str::studly($this->argument('name')); + $handle = str_handle($name); + $content = File::get($stub); + + File::put($path, strtr($content, [ + '{name}' => $name, + '{handle}' => $handle + ])); + } + + /** + * Ensure building model's folder path exists. + * + * @return void + */ + protected function createBuilderModelFolder() + { + $name = Str::of($this->argument('name'))->studly()->plural()->__toString(); + $path = fusion_path("src/Models/{$name}"); + + File::ensureDirectoryExists($path, 0755, true); + + if (!File::exists("{$path}/.gitignore")) { + File::put("{$path}/.gitignore", "*\n!.gitignore"); + } + } + + /** + * Create Builder Class. + * + * @return void + */ + protected function createBuilderClass() + { + list($path, $stub) = $this->getPath('builder_class'); + + $name = Str::studly($this->argument('name')); + $handle = str_handle($name); + $content = File::get($stub); + + File::put($path, strtr($content, [ + '{name}' => $name, + '{handle}' => $handle + ])); + } + + /** + * Create Builder Model stub file. + * + * @return void + */ + protected function createBuilderModel() + { + list($path, $stub) = $this->getPath('builder_model'); + + $name = Str::of($this->argument('name'))->studly()->plural()->__toString(); + $content = File::get($stub); + + File::put($path, strtr($content, [ + '{name}' => $name, + ])); + } + + /** + * Helper. + * + * @throws Exception + * @return void + */ + private function verify() + { + foreach ($this->getPaths() as $key => $value) { + if (File::exists($value[0])) { + throw new \Exception($value[0] . ' already exists!'); + } + } + } + + /** + * Helper. + * + * @param string $key + * @access private + * @return array + */ + private function getPath($key) + { + return $this->getPaths()[$key]; + } + + /** + * Helper. + * + * @access private + * @return array + */ + private function getPaths() + { + $modelName = Str::studly($this->argument('name')); + $stubName = Str::camel($this->argument('name')); + + return [ + 'building_model' => [ + fusion_path("/src/Models/{$modelName}.php"), + fusion_path('stubs/builders/console/buildingModel.stub'), + ], + 'building_model_observer' => [ + fusion_path("/src/Observers/{$modelName}Observer.php"), + fusion_path('stubs/builders/console/buildingModelObserver.stub'), + ], + 'builder_class' => [ + fusion_path("/src/Services/Builders/{$modelName}"), + fusion_path('stubs/builders/console/builderClass.stub'), + ], + 'builder_model' => [ + $path = fusion_path("/stubs/builders/{$stubName}.stub"), + fusion_path('stubs/builders/console/builderModel.stub'), + ], + ]; + } +} \ No newline at end of file diff --git a/stubs/builders/console/builderClass.stub b/stubs/builders/console/builderClass.stub new file mode 100644 index 000000000..c7da866ec --- /dev/null +++ b/stubs/builders/console/builderClass.stub @@ -0,0 +1,28 @@ +source = Model::where('handle', $handle)->firstOrFail(); + } + + /** + * Builder table prefix. + * + * @var string + */ + public static function prefix() + { + return '{handle}'; + } +} diff --git a/stubs/builders/console/builderModel.stub b/stubs/builders/console/builderModel.stub new file mode 100644 index 000000000..10f0b298f --- /dev/null +++ b/stubs/builders/console/builderModel.stub @@ -0,0 +1,32 @@ +getBuilderTable(), function (Blueprint $table) { + $table->id(); + $table->unsignedBigInteger('{handle}_id'); + + // ..add additional columns here.. + + $table->timestamps(); + }); + } + + /** + * Handle the "updating" event. + * + * @param \Illuminate\Database\Eloquent\Model $model + * + * @return void + */ + public function updating(Model $model) + { + $old = {name}::find($model->id); + + if ($old->getBuilderTable() !== $model->getBuilderTable()) { + Schema::rename($old->getBuilderTable(), $model->getBuilderTable()); + } + } + + /** + * Handle the "deleted" event. + * + * @param \Illuminate\Database\Eloquent\Model $model + * + * @return void + */ + public function deleted(Model $model) + { + Schema::dropIfExists($model->getBuilderTable()); + } +}