Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Generalized stub path resolution #37412

Closed
Closed
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
17 changes: 17 additions & 0 deletions src/Illuminate/Console/GeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
use ReflectionClass;
use Symfony\Component\Console\Input\InputArgument;

abstract class GeneratorCommand extends Command
Expand Down Expand Up @@ -118,6 +119,22 @@ public function __construct(Filesystem $files)
*/
abstract protected function getStub();

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
$reflection = new ReflectionClass(get_class($this));
$directory = dirname($reflection->getFileName());

return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: $directory.$stub;
}

/**
* Execute the console command.
*
Expand Down
13 changes: 0 additions & 13 deletions src/Illuminate/Foundation/Console/CastMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ protected function getStub()
return $this->resolveStubPath('/stubs/cast.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}

/**
* Get the default namespace for the class.
*
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/ChannelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class_basename($this->userProviderModel()),
*/
protected function getStub()
{
return __DIR__.'/stubs/channel.stub';
return $this->resolveStubPath('/stubs/channel.stub');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/ComponentMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ protected function getView()
*/
protected function getStub()
{
return __DIR__.'/stubs/view-component.stub';
return $this->resolveStubPath('/stubs/view-component.stub');
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Illuminate/Foundation/Console/ConsoleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,7 @@ protected function replaceClass($stub, $name)
*/
protected function getStub()
{
$relativePath = '/stubs/console.stub';

return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
? $customPath
: __DIR__.$relativePath;
return $this->resolveStubPath('/stubs/console.stub');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/EventMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected function alreadyExists($rawName)
*/
protected function getStub()
{
return __DIR__.'/stubs/event.stub';
return $this->resolveStubPath('/stubs/event.stub');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Foundation/Console/ExceptionMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,13 @@ protected function getStub()
{
if ($this->option('render')) {
return $this->option('report')
? __DIR__.'/stubs/exception-render-report.stub'
: __DIR__.'/stubs/exception-render.stub';
? $this->resolveStubPath('/stubs/exception-render-report.stub')
: $this->resolveStubPath('/stubs/exception-render.stub');
}

return $this->option('report')
? __DIR__.'/stubs/exception-report.stub'
: __DIR__.'/stubs/exception.stub';
? $this->resolveStubPath('/stubs/exception-report.stub')
: $this->resolveStubPath('/stubs/exception.stub');
}

/**
Expand Down
17 changes: 2 additions & 15 deletions src/Illuminate/Foundation/Console/JobMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,8 @@ class JobMakeCommand extends GeneratorCommand
protected function getStub()
{
return $this->option('sync')
? $this->resolveStubPath('/stubs/job.stub')
: $this->resolveStubPath('/stubs/job.queued.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
? $this->resolveStubPath('/stubs/job.stub')
: $this->resolveStubPath('/stubs/job.queued.stub');
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Illuminate/Foundation/Console/ListenerMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ protected function getStub()
{
if ($this->option('queued')) {
return $this->option('event')
? __DIR__.'/stubs/listener-queued.stub'
: __DIR__.'/stubs/listener-queued-duck.stub';
? $this->resolveStubPath('/stubs/listener-queued.stub')
: $this->resolveStubPath('/stubs/listener-queued-duck.stub');
}

return $this->option('event')
? __DIR__.'/stubs/listener.stub'
: __DIR__.'/stubs/listener-duck.stub';
? $this->resolveStubPath('/stubs/listener.stub')
: $this->resolveStubPath('/stubs/listener-duck.stub');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Console/MailMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected function buildClass($name)
protected function getStub()
{
return $this->option('markdown')
? __DIR__.'/stubs/markdown-mail.stub'
: __DIR__.'/stubs/mail.stub';
? $this->resolveStubPath('/stubs/markdown-mail.stub')
: $this->resolveStubPath('/stubs/mail.stub');
}

/**
Expand Down
17 changes: 2 additions & 15 deletions src/Illuminate/Foundation/Console/ModelMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,8 @@ protected function createController()
protected function getStub()
{
return $this->option('pivot')
? $this->resolveStubPath('/stubs/model.pivot.stub')
: $this->resolveStubPath('/stubs/model.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
? $this->resolveStubPath('/stubs/model.pivot.stub')
: $this->resolveStubPath('/stubs/model.stub');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Console/NotificationMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@ protected function buildClass($name)
protected function getStub()
{
return $this->option('markdown')
? __DIR__.'/stubs/markdown-notification.stub'
: __DIR__.'/stubs/notification.stub';
? $this->resolveStubPath('/stubs/markdown-notification.stub')
: $this->resolveStubPath('/stubs/notification.stub');
}

/**
Expand Down
13 changes: 0 additions & 13 deletions src/Illuminate/Foundation/Console/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,19 +101,6 @@ protected function getStub()
: $this->resolveStubPath('/stubs/observer.plain.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}

/**
* Get the default namespace for the class.
*
Expand Down
17 changes: 2 additions & 15 deletions src/Illuminate/Foundation/Console/PolicyMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,21 +151,8 @@ protected function replaceModel($stub, $model)
protected function getStub()
{
return $this->option('model')
? $this->resolveStubPath('/stubs/policy.stub')
: $this->resolveStubPath('/stubs/policy.plain.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
? $this->resolveStubPath('/stubs/policy.stub')
: $this->resolveStubPath('/stubs/policy.plain.stub');
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Foundation/Console/ProviderMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ProviderMakeCommand extends GeneratorCommand
*/
protected function getStub()
{
return __DIR__.'/stubs/provider.stub';
return $this->resolveStubPath('/stubs/provider.stub');
}

/**
Expand Down
13 changes: 0 additions & 13 deletions src/Illuminate/Foundation/Console/RequestMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,6 @@ protected function getStub()
return $this->resolveStubPath('/stubs/request.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}

/**
* Get the default namespace for the class.
*
Expand Down
17 changes: 2 additions & 15 deletions src/Illuminate/Foundation/Console/ResourceMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ public function handle()
protected function getStub()
{
return $this->collection()
? $this->resolveStubPath('/stubs/resource-collection.stub')
: $this->resolveStubPath('/stubs/resource.stub');
? $this->resolveStubPath('/stubs/resource-collection.stub')
: $this->resolveStubPath('/stubs/resource.stub');
}

/**
Expand All @@ -66,19 +66,6 @@ protected function collection()
Str::endsWith($this->argument('name'), 'Collection');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
}

/**
* Get the default namespace for the class.
*
Expand Down
8 changes: 2 additions & 6 deletions src/Illuminate/Foundation/Console/RuleMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,9 @@ class RuleMakeCommand extends GeneratorCommand
*
* @return string
*/
protected function getStub()
public function getStub()
{
$relativePath = '/stubs/rule.stub';

return file_exists($customPath = $this->laravel->basePath(trim($relativePath, '/')))
? $customPath
: __DIR__.$relativePath;
return $this->resolveStubPath('/stubs/rule.stub');
}

/**
Expand Down
17 changes: 2 additions & 15 deletions src/Illuminate/Foundation/Console/TestMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,8 @@ class TestMakeCommand extends GeneratorCommand
protected function getStub()
{
return $this->option('unit')
? $this->resolveStubPath('/stubs/test.unit.stub')
: $this->resolveStubPath('/stubs/test.stub');
}

/**
* Resolve the fully-qualified path to the stub.
*
* @param string $stub
* @return string
*/
protected function resolveStubPath($stub)
{
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
? $customPath
: __DIR__.$stub;
? $this->resolveStubPath('/stubs/test.unit.stub')
: $this->resolveStubPath('/stubs/test.stub');
}

/**
Expand Down