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

Commit

Permalink
Added customizable module prefix for namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
atehnix committed Mar 13, 2019
1 parent d4fcd92 commit 20ec19f
Show file tree
Hide file tree
Showing 20 changed files with 118 additions and 26 deletions.
12 changes: 12 additions & 0 deletions publish/config/stubs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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', ''),

];
4 changes: 3 additions & 1 deletion src/Console/ChannelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ChannelMakeCommand extends BaseChannelMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion src/Console/ConsoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ConsoleMakeCommand extends BaseConsoleMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
10 changes: 8 additions & 2 deletions src/Console/ControllerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

class ControllerMakeCommand extends BaseControllerMakeCommand
{
use Modulable;

/**
* The Laravel application instance.
*
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Console/EventMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class EventMakeCommand extends BaseEventMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion src/Console/ExceptionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ExceptionMakeCommand extends BaseExceptionMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand Down Expand Up @@ -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');
}
}
8 changes: 7 additions & 1 deletion src/Console/FactoryMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class FactoryMakeCommand extends BaseFactoryMakeCommand
{
use Modulable;

/**
* The Laravel application instance.
*
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Console/JobMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class JobMakeCommand extends BaseJobMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
14 changes: 10 additions & 4 deletions src/Console/ListenerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class ListenerMakeCommand extends BaseListenerMakeCommand
{
use Modulable;

/**
* The Laravel application instance.
*
Expand All @@ -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');
}

/**
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 3 additions & 1 deletion src/Console/MailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class MailMakeCommand extends BaseMailMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion src/Console/MiddlewareMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class MiddlewareMakeCommand extends BaseMiddlewareMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion src/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ModelMakeCommand extends BaseModelMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
26 changes: 26 additions & 0 deletions src/Console/Modulable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php declare(strict_types=1);
/**
* This file is part of laravel-stubs package.
*
* @author ATehnix <[email protected]>
*
* 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;
}
}
4 changes: 3 additions & 1 deletion src/Console/NotificationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class NotificationMakeCommand extends BaseNotificationMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
11 changes: 8 additions & 3 deletions src/Console/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class ObserverMakeCommand extends BaseObserverMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}

/**
Expand All @@ -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);
Expand Down
11 changes: 8 additions & 3 deletions src/Console/PolicyMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class PolicyMakeCommand extends BasePolicyMakeCommand
{
use Modulable;

/**
* The Laravel application instance.
*
Expand Down Expand Up @@ -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');
}

/**
Expand All @@ -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);
Expand Down
4 changes: 3 additions & 1 deletion src/Console/ProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class ProviderMakeCommand extends BaseProviderMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
4 changes: 3 additions & 1 deletion src/Console/RequestMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

class RequestMakeCommand extends BaseRequestMakeCommand
{
use Modulable;

/**
* Get the stub file for the generator.
*
Expand All @@ -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');
}
}
Loading

0 comments on commit 20ec19f

Please sign in to comment.