diff --git a/publish/config/stubs.php b/publish/config/stubs.php index 3767530..66c00db 100644 --- a/publish/config/stubs.php +++ b/publish/config/stubs.php @@ -35,4 +35,16 @@ 'rule' => '\Rules', ], + /* + |-------------------------------------------------------------------------- + | The name of the module that is being developed + |-------------------------------------------------------------------------- + | When specified, the namespaces will be prefixed with + | the appropriate module name. + | + | For example, if the module is set to 'Blog', then the namespace + | for the controllers might look like: "App\Blog\Http\Controllers" + */ + 'module' => env('STUBS_MODULE', ''), + ]; diff --git a/src/Console/ChannelMakeCommand.php b/src/Console/ChannelMakeCommand.php index 3316a1b..693b0e6 100644 --- a/src/Console/ChannelMakeCommand.php +++ b/src/Console/ChannelMakeCommand.php @@ -14,6 +14,8 @@ class ChannelMakeCommand extends BaseChannelMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.channel'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.channel'); } } diff --git a/src/Console/ConsoleMakeCommand.php b/src/Console/ConsoleMakeCommand.php index 03bf96d..2affde1 100644 --- a/src/Console/ConsoleMakeCommand.php +++ b/src/Console/ConsoleMakeCommand.php @@ -14,6 +14,8 @@ class ConsoleMakeCommand extends BaseConsoleMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.command'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.command'); } } diff --git a/src/Console/ControllerMakeCommand.php b/src/Console/ControllerMakeCommand.php index b084c59..afd2ec3 100644 --- a/src/Console/ControllerMakeCommand.php +++ b/src/Console/ControllerMakeCommand.php @@ -16,6 +16,8 @@ class ControllerMakeCommand extends BaseControllerMakeCommand { + use Modulable; + /** * The Laravel application instance. * @@ -61,7 +63,7 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.controller'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.controller'); } /** @@ -77,7 +79,11 @@ protected function parseModel($model) } $model = trim(str_replace('/', '\\', $model), '\\'); - $namespace = $this->laravel->getNamespace() . ltrim(config('stubs.namespaces.model') . '\\', '\\'); + + $namespace = + trim($this->laravel->getNamespace(), '\\') + . $this->getModuleNamespace() + . config('stubs.namespaces.model') . '\\'; if (!Str::startsWith($model, $namespace)) { $model = $namespace . $model; diff --git a/src/Console/EventMakeCommand.php b/src/Console/EventMakeCommand.php index ce39b90..0f3c735 100644 --- a/src/Console/EventMakeCommand.php +++ b/src/Console/EventMakeCommand.php @@ -14,6 +14,8 @@ class EventMakeCommand extends BaseEventMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.event'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.event'); } } diff --git a/src/Console/ExceptionMakeCommand.php b/src/Console/ExceptionMakeCommand.php index 54d4f25..81c25ab 100644 --- a/src/Console/ExceptionMakeCommand.php +++ b/src/Console/ExceptionMakeCommand.php @@ -14,6 +14,8 @@ class ExceptionMakeCommand extends BaseExceptionMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -42,6 +44,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.exception'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.exception'); } } diff --git a/src/Console/FactoryMakeCommand.php b/src/Console/FactoryMakeCommand.php index 16d4ec4..9881272 100644 --- a/src/Console/FactoryMakeCommand.php +++ b/src/Console/FactoryMakeCommand.php @@ -15,6 +15,8 @@ class FactoryMakeCommand extends BaseFactoryMakeCommand { + use Modulable; + /** * The Laravel application instance. * @@ -52,7 +54,11 @@ protected function buildClass($name) protected function parseModel($model) { $model = trim(str_replace('/', '\\', $model), '\\'); - $namespace = $this->laravel->getNamespace() . ltrim(config('stubs.namespaces.model') . '\\', '\\'); + + $namespace = + trim($this->laravel->getNamespace(), '\\') + . $this->getModuleNamespace() + . config('stubs.namespaces.model') . '\\'; if (!Str::startsWith($model, $namespace)) { $model = $namespace . $model; diff --git a/src/Console/JobMakeCommand.php b/src/Console/JobMakeCommand.php index 8f81cf8..14b9cd7 100644 --- a/src/Console/JobMakeCommand.php +++ b/src/Console/JobMakeCommand.php @@ -14,6 +14,8 @@ class JobMakeCommand extends BaseJobMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -38,6 +40,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.job'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.job'); } } diff --git a/src/Console/ListenerMakeCommand.php b/src/Console/ListenerMakeCommand.php index a9e8413..aec535c 100644 --- a/src/Console/ListenerMakeCommand.php +++ b/src/Console/ListenerMakeCommand.php @@ -15,6 +15,8 @@ class ListenerMakeCommand extends BaseListenerMakeCommand { + use Modulable; + /** * The Laravel application instance. * @@ -30,7 +32,7 @@ class ListenerMakeCommand extends BaseListenerMakeCommand */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.listener'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.listener'); } /** @@ -73,13 +75,17 @@ protected function getEvent() { $event = $this->option('event'); + $namespace = + trim($this->laravel->getNamespace(), '\\') + . $this->getModuleNamespace() + . config('stubs.namespaces.event') . '\\'; + if (!Str::startsWith($event, [ - $this->laravel->getNamespace(), + $namespace, 'Illuminate', '\\', ])) { - $eventNamespace = ltrim(config('stubs.namespaces.event'), '\\') . '\\'; - $event = $this->laravel->getNamespace() . $eventNamespace . $event; + $event = $namespace . $event; } return $event; diff --git a/src/Console/MailMakeCommand.php b/src/Console/MailMakeCommand.php index 0b92497..e668ea9 100644 --- a/src/Console/MailMakeCommand.php +++ b/src/Console/MailMakeCommand.php @@ -14,6 +14,8 @@ class MailMakeCommand extends BaseMailMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -38,6 +40,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.mail'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.mail'); } } diff --git a/src/Console/MiddlewareMakeCommand.php b/src/Console/MiddlewareMakeCommand.php index 9c15f2c..20d674c 100644 --- a/src/Console/MiddlewareMakeCommand.php +++ b/src/Console/MiddlewareMakeCommand.php @@ -14,6 +14,8 @@ class MiddlewareMakeCommand extends BaseMiddlewareMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.middleware'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.middleware'); } } diff --git a/src/Console/ModelMakeCommand.php b/src/Console/ModelMakeCommand.php index d6bd5ac..5975c80 100644 --- a/src/Console/ModelMakeCommand.php +++ b/src/Console/ModelMakeCommand.php @@ -14,6 +14,8 @@ class ModelMakeCommand extends BaseModelMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -38,6 +40,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.model'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.model'); } } diff --git a/src/Console/Modulable.php b/src/Console/Modulable.php new file mode 100644 index 0000000..5334f52 --- /dev/null +++ b/src/Console/Modulable.php @@ -0,0 +1,26 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace ATehnix\LaravelStubs\Console; + +trait Modulable +{ + /** + * The namespace prefix of the module that is being developed + * + * @return string|null + */ + protected function getModuleNamespace() + { + $module = trim(config('stubs.module'), '\\'); + + return $module ? '\\' . $module : null; + } +} diff --git a/src/Console/NotificationMakeCommand.php b/src/Console/NotificationMakeCommand.php index ed8c66a..30b320f 100644 --- a/src/Console/NotificationMakeCommand.php +++ b/src/Console/NotificationMakeCommand.php @@ -14,6 +14,8 @@ class NotificationMakeCommand extends BaseNotificationMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -38,6 +40,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.notification'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.notification'); } } diff --git a/src/Console/ObserverMakeCommand.php b/src/Console/ObserverMakeCommand.php index 0089b5e..c73d3fd 100644 --- a/src/Console/ObserverMakeCommand.php +++ b/src/Console/ObserverMakeCommand.php @@ -15,6 +15,8 @@ class ObserverMakeCommand extends BaseObserverMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -39,7 +41,7 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.observer'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.observer'); } /** @@ -53,8 +55,11 @@ protected function replaceModel($stub, $model) { $model = str_replace('/', '\\', $model); - $modelsNamespace = ltrim(config('stubs.namespaces.model') . '\\', '\\'); - $namespaceModel = $this->laravel->getNamespace() . $modelsNamespace . $model; + $namespaceModel = + trim($this->laravel->getNamespace(), '\\') + . $this->getModuleNamespace() + . config('stubs.namespaces.model') . '\\' + . $model; if (Str::startsWith($model, '\\')) { $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); diff --git a/src/Console/PolicyMakeCommand.php b/src/Console/PolicyMakeCommand.php index 083a647..f6ebafc 100644 --- a/src/Console/PolicyMakeCommand.php +++ b/src/Console/PolicyMakeCommand.php @@ -15,6 +15,8 @@ class PolicyMakeCommand extends BasePolicyMakeCommand { + use Modulable; + /** * The Laravel application instance. * @@ -46,7 +48,7 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.policy'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.policy'); } /** @@ -60,8 +62,11 @@ protected function replaceModel($stub, $model) { $model = str_replace('/', '\\', $model); - $modelsNamespace = ltrim(config('stubs.namespaces.model') . '\\', '\\'); - $namespaceModel = $this->laravel->getNamespace() . $modelsNamespace . $model; + $namespaceModel = + trim($this->laravel->getNamespace(), '\\') + . $this->getModuleNamespace() + . config('stubs.namespaces.model') . '\\' + . $model; if (Str::startsWith($model, '\\')) { $stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub); diff --git a/src/Console/ProviderMakeCommand.php b/src/Console/ProviderMakeCommand.php index 91d7e65..152ae03 100644 --- a/src/Console/ProviderMakeCommand.php +++ b/src/Console/ProviderMakeCommand.php @@ -14,6 +14,8 @@ class ProviderMakeCommand extends BaseProviderMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.provider'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.provider'); } } diff --git a/src/Console/RequestMakeCommand.php b/src/Console/RequestMakeCommand.php index afdb4e1..4cde530 100644 --- a/src/Console/RequestMakeCommand.php +++ b/src/Console/RequestMakeCommand.php @@ -14,6 +14,8 @@ class RequestMakeCommand extends BaseRequestMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.request'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.request'); } } diff --git a/src/Console/ResourceMakeCommand.php b/src/Console/ResourceMakeCommand.php index e58d596..f105312 100644 --- a/src/Console/ResourceMakeCommand.php +++ b/src/Console/ResourceMakeCommand.php @@ -14,6 +14,8 @@ class ResourceMakeCommand extends BaseResourceMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -38,6 +40,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.resource'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.resource'); } } diff --git a/src/Console/RuleMakeCommand.php b/src/Console/RuleMakeCommand.php index 36c1cf0..c3313ec 100644 --- a/src/Console/RuleMakeCommand.php +++ b/src/Console/RuleMakeCommand.php @@ -14,6 +14,8 @@ class RuleMakeCommand extends BaseRuleMakeCommand { + use Modulable; + /** * Get the stub file for the generator. * @@ -34,6 +36,6 @@ protected function getStub() */ protected function getDefaultNamespace($rootNamespace) { - return $rootNamespace . config('stubs.namespaces.rule'); + return $rootNamespace . $this->getModuleNamespace() . config('stubs.namespaces.rule'); } }