Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/Console/Commands/ScaffoldAuthenticationControllers.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ class ScaffoldAuthenticationControllers extends Command
*/
protected $namespace = null;

/**
* Models namespace
*
* @var string
*/
protected $modelNamespace = null;

/**
* @var Illuminate\Filesystem\Filesystem
*/
Expand All @@ -54,6 +61,8 @@ public function __construct()
parent::__construct();

$this->namespace = self::DEFAULT_NAMESPACE;

$this->modelNamespace = self::DEFAULT_NAMESPACE;

$this->filesystem = new Filesystem;
}
Expand Down Expand Up @@ -88,6 +97,8 @@ 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].");
}
Expand Down Expand Up @@ -126,12 +137,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?")) {
Expand Down Expand Up @@ -188,7 +199,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,
Expand Down
2 changes: 1 addition & 1 deletion tests/Unit/ScaffoldAuthenticationControllersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down