From 545186a88d3deed5589a55a8408560f62cd9851e Mon Sep 17 00:00:00 2001 From: Alessio Date: Sat, 19 Feb 2022 11:32:10 +0100 Subject: [PATCH 1/2] Support Models folder --- .../ScaffoldAuthenticationControllers.php | 20 +++++++++++++++---- .../ScaffoldAuthenticationControllersTest.php | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-) diff --git a/src/Console/Commands/ScaffoldAuthenticationControllers.php b/src/Console/Commands/ScaffoldAuthenticationControllers.php index 8ac6321..07b6089 100644 --- a/src/Console/Commands/ScaffoldAuthenticationControllers.php +++ b/src/Console/Commands/ScaffoldAuthenticationControllers.php @@ -39,6 +39,13 @@ class ScaffoldAuthenticationControllers extends Command */ protected $namespace = null; + /** + * Models namespace + * + * @var string + */ + protected $modelNamespace = null; + /** * @var Illuminate\Filesystem\Filesystem */ @@ -54,6 +61,8 @@ public function __construct() parent::__construct(); $this->namespace = self::DEFAULT_NAMESPACE; + + $this->modelNamespace = self::DEFAULT_NAMESPACE; $this->filesystem = new Filesystem; } @@ -88,6 +97,9 @@ protected function identifyApplicationNamespace() if ($this->namespace !== self::DEFAULT_NAMESPACE) { $this->comment("Using [$this->namespace] as application namespace."); } + + $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\\Models' : $this->namespace; + } catch (RuntimeException $ex) { $this->warn("Unable to identity the application namespace, assuming [$this->namespace]."); } @@ -126,12 +138,12 @@ protected function scaffoldModels() mkdir($directory, 0755, true); } - // TODO: make it Models folder aware + $useModelsDirectory = is_dir(app_path('Models')); collect($this->filesystem->allFiles(__DIR__.'/../../../stubs/Identities/Models')) - ->each(function (SplFileInfo $file) { + ->each(function (SplFileInfo $file) use ($useModelsDirectory) { $modelName = Str::replaceLast('.stub', '.php', $file->getFilename()); - $model = app_path($modelName); + $model = $useModelsDirectory ? app_path('Models/'.$modelName) : app_path($modelName); if (file_exists($model) && ! $this->option('force')) { if ($this->confirm("The [$modelName] file already exists. Do you want to replace it?")) { @@ -188,7 +200,7 @@ protected function compileControllerStub($stub) protected function compileModelStub($stub) { $originalNamespaceDeclaration = str_replace('\\;', ';', "namespace ".self::DEFAULT_NAMESPACE.';'); - $newNamespaceDeclaration = str_replace('\\;', ';', "namespace $this->namespace;"); + $newNamespaceDeclaration = str_replace('\\;', ';', "namespace $this->modelNamespace;"); return str_replace( $originalNamespaceDeclaration, diff --git a/tests/Unit/ScaffoldAuthenticationControllersTest.php b/tests/Unit/ScaffoldAuthenticationControllersTest.php index a8ada24..3ec0818 100644 --- a/tests/Unit/ScaffoldAuthenticationControllersTest.php +++ b/tests/Unit/ScaffoldAuthenticationControllersTest.php @@ -29,7 +29,7 @@ public function test_scaffold() $files = [ base_path('database/migrations/2020_08_09_115707_create_identities_table.php'), - app_path('Identity.php'), + app_path('Models/Identity.php'), app_path('Http/Controllers/Identities/Auth/ConnectController.php'), app_path('Http/Controllers/Identities/Auth/LoginController.php'), app_path('Http/Controllers/Identities/Auth/RegisterController.php'), From a5b7b109b60861e85145fa5f737292edd5d26fc1 Mon Sep 17 00:00:00 2001 From: avvertix Date: Sat, 19 Feb 2022 10:33:30 +0000 Subject: [PATCH 2/2] Fix styling --- src/Console/Commands/ScaffoldAuthenticationControllers.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Console/Commands/ScaffoldAuthenticationControllers.php b/src/Console/Commands/ScaffoldAuthenticationControllers.php index 07b6089..96e1ab6 100644 --- a/src/Console/Commands/ScaffoldAuthenticationControllers.php +++ b/src/Console/Commands/ScaffoldAuthenticationControllers.php @@ -99,7 +99,6 @@ protected function identifyApplicationNamespace() } $this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\\Models' : $this->namespace; - } catch (RuntimeException $ex) { $this->warn("Unable to identity the application namespace, assuming [$this->namespace]."); }