From e53a55dbe56dd80c694bc78f056d8883c669f794 Mon Sep 17 00:00:00 2001 From: Shane Krolikowski Date: Fri, 18 Dec 2020 23:34:36 +0000 Subject: [PATCH 1/3] updates --- src/Console/MakeBuilderCommand.php | 167 ++++++++++++++++++ src/Providers/FusionServiceProvider.php | 1 + stubs/builders/console/builderClass.stub | 28 +++ stubs/builders/console/builderModel.stub | 32 ++++ stubs/builders/console/buildingModel.stub | 13 ++ .../console/buildingModelObserver.stub | 59 +++++++ 6 files changed, 300 insertions(+) create mode 100644 src/Console/MakeBuilderCommand.php create mode 100644 stubs/builders/console/builderClass.stub create mode 100644 stubs/builders/console/builderModel.stub create mode 100644 stubs/builders/console/buildingModel.stub create mode 100644 stubs/builders/console/buildingModelObserver.stub diff --git a/src/Console/MakeBuilderCommand.php b/src/Console/MakeBuilderCommand.php new file mode 100644 index 000000000..536fd44fd --- /dev/null +++ b/src/Console/MakeBuilderCommand.php @@ -0,0 +1,167 @@ +createBuildingModel(); + $this->createBuildingModelObserver(); + $this->createBuilderModelFolder(); + + // Builder class.. + $this->createBuilderClass(); + + // Builder model.. + $this->createBuilderModel(); + } + + /** + * 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. + * + * @param string $key + * @return array + */ + private function getPath($key) + { + $modelName = Str::studly($this->argument('name')); + $stubName = Str::camel($this->argument('name')); + + $paths = [ + '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'), + ], + ]; + + return $paths[$key]; + } + +} \ No newline at end of file diff --git a/src/Providers/FusionServiceProvider.php b/src/Providers/FusionServiceProvider.php index 2ec45aeea..981dc0810 100644 --- a/src/Providers/FusionServiceProvider.php +++ b/src/Providers/FusionServiceProvider.php @@ -59,6 +59,7 @@ public function register() \Fusion\Console\Addons\ResetCommand::class, \Fusion\Console\Addons\ListCommand::class, \Fusion\Console\MakeAddonCommand::class, + \Fusion\Console\MakeBuilderCommand::class, \Fusion\Console\MakeThemeCommand::class, \Fusion\Console\UninstallCommand::class, \Fusion\Console\InstallCommand::class, diff --git a/stubs/builders/console/builderClass.stub b/stubs/builders/console/builderClass.stub new file mode 100644 index 000000000..fd80737e5 --- /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()); + } +} From f31256fbd7e8762a1387b51f0627d37f940600b4 Mon Sep 17 00:00:00 2001 From: Shane Krolikowski Date: Fri, 18 Dec 2020 23:36:32 +0000 Subject: [PATCH 2/3] fix --- stubs/builders/console/builderClass.stub | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stubs/builders/console/builderClass.stub b/stubs/builders/console/builderClass.stub index fd80737e5..c7da866ec 100644 --- a/stubs/builders/console/builderClass.stub +++ b/stubs/builders/console/builderClass.stub @@ -23,6 +23,6 @@ class {name} extends Builder */ public static function prefix() { - return {handle}; + return '{handle}'; } } From 7043fc2d68c24f7bd48939597ab9963e876139a5 Mon Sep 17 00:00:00 2001 From: Shane Krolikowski Date: Fri, 18 Dec 2020 23:46:40 +0000 Subject: [PATCH 3/3] verify --- src/Console/MakeBuilderCommand.php | 76 +++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 23 deletions(-) diff --git a/src/Console/MakeBuilderCommand.php b/src/Console/MakeBuilderCommand.php index 536fd44fd..f09f86aa0 100644 --- a/src/Console/MakeBuilderCommand.php +++ b/src/Console/MakeBuilderCommand.php @@ -30,6 +30,10 @@ class MakeBuilderCommand extends Command */ public function handle() { + $this->info('Verifying a few things...'); + $this->verify(); + $this->info('Setting up your Builder...'); + // Building model.. $this->createBuildingModel(); $this->createBuildingModelObserver(); @@ -40,6 +44,8 @@ public function handle() // Builder model.. $this->createBuilderModel(); + + $this->info('Builder created successfully.'); } /** @@ -131,37 +137,61 @@ protected function createBuilderModel() ])); } + /** + * 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) { - $modelName = Str::studly($this->argument('name')); - $stubName = Str::camel($this->argument('name')); - - $paths = [ - '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'), - ], - ]; - - return $paths[$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