Skip to content

Commit dcc5dfc

Browse files
authored
Support Models folder (#13)
* Support Models folder
1 parent 9cb0ce9 commit dcc5dfc

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

src/Console/Commands/ScaffoldAuthenticationControllers.php

+15-4
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ class ScaffoldAuthenticationControllers extends Command
3939
*/
4040
protected $namespace = null;
4141

42+
/**
43+
* Models namespace
44+
*
45+
* @var string
46+
*/
47+
protected $modelNamespace = null;
48+
4249
/**
4350
* @var Illuminate\Filesystem\Filesystem
4451
*/
@@ -54,6 +61,8 @@ public function __construct()
5461
parent::__construct();
5562

5663
$this->namespace = self::DEFAULT_NAMESPACE;
64+
65+
$this->modelNamespace = self::DEFAULT_NAMESPACE;
5766

5867
$this->filesystem = new Filesystem;
5968
}
@@ -88,6 +97,8 @@ protected function identifyApplicationNamespace()
8897
if ($this->namespace !== self::DEFAULT_NAMESPACE) {
8998
$this->comment("Using [$this->namespace] as application namespace.");
9099
}
100+
101+
$this->modelNamespace = is_dir(app_path('Models')) ? $this->namespace.'\\Models' : $this->namespace;
91102
} catch (RuntimeException $ex) {
92103
$this->warn("Unable to identity the application namespace, assuming [$this->namespace].");
93104
}
@@ -126,12 +137,12 @@ protected function scaffoldModels()
126137
mkdir($directory, 0755, true);
127138
}
128139

129-
// TODO: make it Models folder aware
140+
$useModelsDirectory = is_dir(app_path('Models'));
130141

131142
collect($this->filesystem->allFiles(__DIR__.'/../../../stubs/Identities/Models'))
132-
->each(function (SplFileInfo $file) {
143+
->each(function (SplFileInfo $file) use ($useModelsDirectory) {
133144
$modelName = Str::replaceLast('.stub', '.php', $file->getFilename());
134-
$model = app_path($modelName);
145+
$model = $useModelsDirectory ? app_path('Models/'.$modelName) : app_path($modelName);
135146

136147
if (file_exists($model) && ! $this->option('force')) {
137148
if ($this->confirm("The [$modelName] file already exists. Do you want to replace it?")) {
@@ -188,7 +199,7 @@ protected function compileControllerStub($stub)
188199
protected function compileModelStub($stub)
189200
{
190201
$originalNamespaceDeclaration = str_replace('\\;', ';', "namespace ".self::DEFAULT_NAMESPACE.';');
191-
$newNamespaceDeclaration = str_replace('\\;', ';', "namespace $this->namespace;");
202+
$newNamespaceDeclaration = str_replace('\\;', ';', "namespace $this->modelNamespace;");
192203

193204
return str_replace(
194205
$originalNamespaceDeclaration,

tests/Unit/ScaffoldAuthenticationControllersTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public function test_scaffold()
2929

3030
$files = [
3131
base_path('database/migrations/2020_08_09_115707_create_identities_table.php'),
32-
app_path('Identity.php'),
32+
app_path('Models/Identity.php'),
3333
app_path('Http/Controllers/Identities/Auth/ConnectController.php'),
3434
app_path('Http/Controllers/Identities/Auth/LoginController.php'),
3535
app_path('Http/Controllers/Identities/Auth/RegisterController.php'),

0 commit comments

Comments
 (0)