Skip to content
This repository was archived by the owner on Feb 18, 2024. It is now read-only.

Commit 20ec19f

Browse files
author
atehnix
committed
Added customizable module prefix for namespaces
1 parent d4fcd92 commit 20ec19f

20 files changed

+118
-26
lines changed

publish/config/stubs.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,16 @@
3535
'rule' => '\Rules',
3636
],
3737

38+
/*
39+
|--------------------------------------------------------------------------
40+
| The name of the module that is being developed
41+
|--------------------------------------------------------------------------
42+
| When specified, the namespaces will be prefixed with
43+
| the appropriate module name.
44+
|
45+
| For example, if the module is set to 'Blog', then the namespace
46+
| for the controllers might look like: "App\Blog\Http\Controllers"
47+
*/
48+
'module' => env('STUBS_MODULE', ''),
49+
3850
];

src/Console/ChannelMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class ChannelMakeCommand extends BaseChannelMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.channel');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.channel');
3840
}
3941
}

src/Console/ConsoleMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class ConsoleMakeCommand extends BaseConsoleMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.command');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.command');
3840
}
3941
}

src/Console/ControllerMakeCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
class ControllerMakeCommand extends BaseControllerMakeCommand
1818
{
19+
use Modulable;
20+
1921
/**
2022
* The Laravel application instance.
2123
*
@@ -61,7 +63,7 @@ protected function getStub()
6163
*/
6264
protected function getDefaultNamespace($rootNamespace)
6365
{
64-
return $rootNamespace . config('stubs.namespaces.controller');
66+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.controller');
6567
}
6668

6769
/**
@@ -77,7 +79,11 @@ protected function parseModel($model)
7779
}
7880

7981
$model = trim(str_replace('/', '\\', $model), '\\');
80-
$namespace = $this->laravel->getNamespace() . ltrim(config('stubs.namespaces.model') . '\\', '\\');
82+
83+
$namespace =
84+
trim($this->laravel->getNamespace(), '\\')
85+
. $this->getModuleNamespace()
86+
. config('stubs.namespaces.model') . '\\';
8187

8288
if (!Str::startsWith($model, $namespace)) {
8389
$model = $namespace . $model;

src/Console/EventMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class EventMakeCommand extends BaseEventMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.event');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.event');
3840
}
3941
}

src/Console/ExceptionMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class ExceptionMakeCommand extends BaseExceptionMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -42,6 +44,6 @@ protected function getStub()
4244
*/
4345
protected function getDefaultNamespace($rootNamespace)
4446
{
45-
return $rootNamespace . config('stubs.namespaces.exception');
47+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.exception');
4648
}
4749
}

src/Console/FactoryMakeCommand.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class FactoryMakeCommand extends BaseFactoryMakeCommand
1717
{
18+
use Modulable;
19+
1820
/**
1921
* The Laravel application instance.
2022
*
@@ -52,7 +54,11 @@ protected function buildClass($name)
5254
protected function parseModel($model)
5355
{
5456
$model = trim(str_replace('/', '\\', $model), '\\');
55-
$namespace = $this->laravel->getNamespace() . ltrim(config('stubs.namespaces.model') . '\\', '\\');
57+
58+
$namespace =
59+
trim($this->laravel->getNamespace(), '\\')
60+
. $this->getModuleNamespace()
61+
. config('stubs.namespaces.model') . '\\';
5662

5763
if (!Str::startsWith($model, $namespace)) {
5864
$model = $namespace . $model;

src/Console/JobMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class JobMakeCommand extends BaseJobMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -38,6 +40,6 @@ protected function getStub()
3840
*/
3941
protected function getDefaultNamespace($rootNamespace)
4042
{
41-
return $rootNamespace . config('stubs.namespaces.job');
43+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.job');
4244
}
4345
}

src/Console/ListenerMakeCommand.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class ListenerMakeCommand extends BaseListenerMakeCommand
1717
{
18+
use Modulable;
19+
1820
/**
1921
* The Laravel application instance.
2022
*
@@ -30,7 +32,7 @@ class ListenerMakeCommand extends BaseListenerMakeCommand
3032
*/
3133
protected function getDefaultNamespace($rootNamespace)
3234
{
33-
return $rootNamespace . config('stubs.namespaces.listener');
35+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.listener');
3436
}
3537

3638
/**
@@ -73,13 +75,17 @@ protected function getEvent()
7375
{
7476
$event = $this->option('event');
7577

78+
$namespace =
79+
trim($this->laravel->getNamespace(), '\\')
80+
. $this->getModuleNamespace()
81+
. config('stubs.namespaces.event') . '\\';
82+
7683
if (!Str::startsWith($event, [
77-
$this->laravel->getNamespace(),
84+
$namespace,
7885
'Illuminate',
7986
'\\',
8087
])) {
81-
$eventNamespace = ltrim(config('stubs.namespaces.event'), '\\') . '\\';
82-
$event = $this->laravel->getNamespace() . $eventNamespace . $event;
88+
$event = $namespace . $event;
8389
}
8490

8591
return $event;

src/Console/MailMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class MailMakeCommand extends BaseMailMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -38,6 +40,6 @@ protected function getStub()
3840
*/
3941
protected function getDefaultNamespace($rootNamespace)
4042
{
41-
return $rootNamespace . config('stubs.namespaces.mail');
43+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.mail');
4244
}
4345
}

src/Console/MiddlewareMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class MiddlewareMakeCommand extends BaseMiddlewareMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.middleware');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.middleware');
3840
}
3941
}

src/Console/ModelMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class ModelMakeCommand extends BaseModelMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -38,6 +40,6 @@ protected function getStub()
3840
*/
3941
protected function getDefaultNamespace($rootNamespace)
4042
{
41-
return $rootNamespace . config('stubs.namespaces.model');
43+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.model');
4244
}
4345
}

src/Console/Modulable.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* This file is part of laravel-stubs package.
4+
*
5+
* @author ATehnix <[email protected]>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace ATehnix\LaravelStubs\Console;
12+
13+
trait Modulable
14+
{
15+
/**
16+
* The namespace prefix of the module that is being developed
17+
*
18+
* @return string|null
19+
*/
20+
protected function getModuleNamespace()
21+
{
22+
$module = trim(config('stubs.module'), '\\');
23+
24+
return $module ? '\\' . $module : null;
25+
}
26+
}

src/Console/NotificationMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class NotificationMakeCommand extends BaseNotificationMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -38,6 +40,6 @@ protected function getStub()
3840
*/
3941
protected function getDefaultNamespace($rootNamespace)
4042
{
41-
return $rootNamespace . config('stubs.namespaces.notification');
43+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.notification');
4244
}
4345
}

src/Console/ObserverMakeCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class ObserverMakeCommand extends BaseObserverMakeCommand
1717
{
18+
use Modulable;
19+
1820
/**
1921
* Get the stub file for the generator.
2022
*
@@ -39,7 +41,7 @@ protected function getStub()
3941
*/
4042
protected function getDefaultNamespace($rootNamespace)
4143
{
42-
return $rootNamespace . config('stubs.namespaces.observer');
44+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.observer');
4345
}
4446

4547
/**
@@ -53,8 +55,11 @@ protected function replaceModel($stub, $model)
5355
{
5456
$model = str_replace('/', '\\', $model);
5557

56-
$modelsNamespace = ltrim(config('stubs.namespaces.model') . '\\', '\\');
57-
$namespaceModel = $this->laravel->getNamespace() . $modelsNamespace . $model;
58+
$namespaceModel =
59+
trim($this->laravel->getNamespace(), '\\')
60+
. $this->getModuleNamespace()
61+
. config('stubs.namespaces.model') . '\\'
62+
. $model;
5863

5964
if (Str::startsWith($model, '\\')) {
6065
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);

src/Console/PolicyMakeCommand.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
class PolicyMakeCommand extends BasePolicyMakeCommand
1717
{
18+
use Modulable;
19+
1820
/**
1921
* The Laravel application instance.
2022
*
@@ -46,7 +48,7 @@ protected function getStub()
4648
*/
4749
protected function getDefaultNamespace($rootNamespace)
4850
{
49-
return $rootNamespace . config('stubs.namespaces.policy');
51+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.policy');
5052
}
5153

5254
/**
@@ -60,8 +62,11 @@ protected function replaceModel($stub, $model)
6062
{
6163
$model = str_replace('/', '\\', $model);
6264

63-
$modelsNamespace = ltrim(config('stubs.namespaces.model') . '\\', '\\');
64-
$namespaceModel = $this->laravel->getNamespace() . $modelsNamespace . $model;
65+
$namespaceModel =
66+
trim($this->laravel->getNamespace(), '\\')
67+
. $this->getModuleNamespace()
68+
. config('stubs.namespaces.model') . '\\'
69+
. $model;
6570

6671
if (Str::startsWith($model, '\\')) {
6772
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);

src/Console/ProviderMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class ProviderMakeCommand extends BaseProviderMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.provider');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.provider');
3840
}
3941
}

src/Console/RequestMakeCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
class RequestMakeCommand extends BaseRequestMakeCommand
1616
{
17+
use Modulable;
18+
1719
/**
1820
* Get the stub file for the generator.
1921
*
@@ -34,6 +36,6 @@ protected function getStub()
3436
*/
3537
protected function getDefaultNamespace($rootNamespace)
3638
{
37-
return $rootNamespace . config('stubs.namespaces.request');
39+
return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.request');
3840
}
3941
}

0 commit comments

Comments
 (0)