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] Add ability to create observers with custom path #34911

Merged
Merged
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
66 changes: 46 additions & 20 deletions src/Illuminate/Foundation/Console/ObserverMakeCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Illuminate\Foundation\Console;

use Illuminate\Console\GeneratorCommand;
use Illuminate\Support\Str;
use InvalidArgumentException;
use Symfony\Component\Console\Input\InputOption;

class ObserverMakeCommand extends GeneratorCommand
Expand Down Expand Up @@ -52,8 +52,21 @@ protected function buildClass($name)
protected function getStub()
{
return $this->option('model')
? __DIR__.'/stubs/observer.stub'
: __DIR__.'/stubs/observer.plain.stub';
? $this->resolveStubPath('/stubs/observer.stub')
: $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;
}

/**
Expand All @@ -65,27 +78,40 @@ protected function getStub()
*/
protected function replaceModel($stub, $model)
{
$model = str_replace('/', '\\', $model);

$namespacedModel = $this->qualifyModel($model);

if (Str::startsWith($model, '\\')) {
$stub = str_replace('NamespacedDummyModel', trim($model, '\\'), $stub);
} else {
$stub = str_replace('NamespacedDummyModel', $namespacedModel, $stub);
}
$modelClass = $this->parseModel($model);

$replace = [
'DummyFullModelClass' => $modelClass,
'{{ namespacedModel }}' => $modelClass,
'{{namespacedModel}}' => $modelClass,
'DummyModelClass' => class_basename($modelClass),
'{{ model }}' => class_basename($modelClass),
'{{model}}' => class_basename($modelClass),
'DummyModelVariable' => lcfirst(class_basename($modelClass)),
'{{ modelVariable }}' => lcfirst(class_basename($modelClass)),
'{{modelVariable}}' => lcfirst(class_basename($modelClass)),
];

$stub = str_replace(
"use {$namespacedModel};\nuse {$namespacedModel};", "use {$namespacedModel};", $stub
return str_replace(
array_keys($replace), array_values($replace), $stub
);
}

$model = class_basename(trim($model, '\\'));

$stub = str_replace('DocDummyModel', Str::snake($model, ' '), $stub);

$stub = str_replace('DummyModel', $model, $stub);
/**
* Get the fully-qualified model class name.
*
* @param string $model
* @return string
*
* @throws \InvalidArgumentException
*/
protected function parseModel($model)
{
if (preg_match('([^A-Za-z0-9_/\\\\])', $model)) {
throw new InvalidArgumentException('Model name contains invalid characters.');
}

return str_replace('dummyModel', Str::camel($model), $stub);
return $this->qualifyModel($model);
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Foundation/Console/StubPublishCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public function handle()
__DIR__.'/stubs/job.stub' => $stubsPath.'/job.stub',
__DIR__.'/stubs/model.pivot.stub' => $stubsPath.'/model.pivot.stub',
__DIR__.'/stubs/model.stub' => $stubsPath.'/model.stub',
__DIR__.'/stubs/observer.stub' => $stubsPath.'/observer.stub',
__DIR__.'/stubs/observer.plain.stub' => $stubsPath.'/observer.plain.stub',
__DIR__.'/stubs/request.stub' => $stubsPath.'/request.stub',
__DIR__.'/stubs/resource.stub' => $stubsPath.'/resource.stub',
__DIR__.'/stubs/resource-collection.stub' => $stubsPath.'/resource-collection.stub',
Expand Down
4 changes: 2 additions & 2 deletions src/Illuminate/Foundation/Console/stubs/observer.plain.stub
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php

namespace DummyNamespace;
namespace {{ namespace }};

class DummyClass
class {{ class }}
{
//
}
36 changes: 18 additions & 18 deletions src/Illuminate/Foundation/Console/stubs/observer.stub
Original file line number Diff line number Diff line change
@@ -1,62 +1,62 @@
<?php

namespace DummyNamespace;
namespace {{ namespace }};

use NamespacedDummyModel;
use {{ namespacedModel }};

class DummyClass
class {{ class }}
{
/**
* Handle the DocDummyModel "created" event.
* Handle the {{ model }} "created" event.
*
* @param \NamespacedDummyModel $dummyModel
* @param \{{ namespacedModel }} ${{ modelVariable }}
* @return void
*/
public function created(DummyModel $dummyModel)
public function created({{ model }} ${{ modelVariable }})
{
//
}

/**
* Handle the DocDummyModel "updated" event.
* Handle the {{ model }} "updated" event.
*
* @param \NamespacedDummyModel $dummyModel
* @param \{{ namespacedModel }} ${{ modelVariable }}
* @return void
*/
public function updated(DummyModel $dummyModel)
public function updated({{ model }} ${{ modelVariable }})
{
//
}

/**
* Handle the DocDummyModel "deleted" event.
* Handle the {{ model }} "deleted" event.
*
* @param \NamespacedDummyModel $dummyModel
* @param \{{ namespacedModel }} ${{ modelVariable }}
* @return void
*/
public function deleted(DummyModel $dummyModel)
public function deleted({{ model }} ${{ modelVariable }})
{
//
}

/**
* Handle the DocDummyModel "restored" event.
* Handle the {{ model }} "restored" event.
*
* @param \NamespacedDummyModel $dummyModel
* @param \{{ namespacedModel }} ${{ modelVariable }}
* @return void
*/
public function restored(DummyModel $dummyModel)
public function restored({{ model }} ${{ modelVariable }})
{
//
}

/**
* Handle the DocDummyModel "force deleted" event.
* Handle the {{ model }} "force deleted" event.
*
* @param \NamespacedDummyModel $dummyModel
* @param \{{ namespacedModel }} ${{ modelVariable }}
* @return void
*/
public function forceDeleted(DummyModel $dummyModel)
public function forceDeleted({{ model }} ${{ modelVariable }})
{
//
}
Expand Down