generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from savannabits/0.x-dev
Package Api Improvement: Collect Reusable generator modifications to a Concern
- Loading branch information
Showing
4 changed files
with
193 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Savannabits\Modular\Commands; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Illuminate\Support\Str; | ||
use Savannabits\Modular\Support\Concerns\GeneratesModularFiles; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class FactoryMakeCommand extends GeneratorCommand | ||
{ | ||
use GeneratesModularFiles; | ||
|
||
/** | ||
* The console command name. | ||
* | ||
* @var string | ||
*/ | ||
protected $name = 'modular:make-factory'; | ||
|
||
/** | ||
* The console command description. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new model factory class in a modular package'; | ||
|
||
/** | ||
* The type of class being generated. | ||
* | ||
* @var string | ||
*/ | ||
protected $type = 'Factory'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() | ||
{ | ||
return $this->resolveStubPath('/stubs/factory.stub'); | ||
} | ||
|
||
protected function buildClass($name): string | ||
{ | ||
$factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name))); | ||
|
||
$namespaceModel = $this->option('model') | ||
? $this->qualifyModel($this->option('model')) | ||
: $this->qualifyModel($this->guessModelName($name)); | ||
|
||
$model = class_basename($namespaceModel); | ||
|
||
$namespace = $this->getNamespace( | ||
$this->getModule()->makeNamespace('Database\\Factories\\') | ||
); | ||
|
||
$replace = [ | ||
'{{ factoryNamespace }}' => $namespace, | ||
'NamespacedDummyModel' => $namespaceModel, | ||
'{{ namespacedModel }}' => $namespaceModel, | ||
'{{namespacedModel}}' => $namespaceModel, | ||
'DummyModel' => $model, | ||
'{{ model }}' => $model, | ||
'{{model}}' => $model, | ||
'{{ factory }}' => $factory, | ||
'{{factory}}' => $factory, | ||
]; | ||
|
||
return str_replace( | ||
array_keys($replace), array_values($replace), parent::buildClass($name) | ||
); | ||
} | ||
|
||
/** | ||
* Get the destination class path. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
protected function getPath($name) | ||
{ | ||
$name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('Factory'); | ||
|
||
return $this->getModule()->factoriesPath(str_replace('\\', '/', $name).'.php'); | ||
} | ||
|
||
/** | ||
* Guess the model name from the Factory name or return a default model name. | ||
* | ||
* @param string $name | ||
* @return string | ||
*/ | ||
protected function guessModelName($name) | ||
{ | ||
if (str_ends_with($name, 'Factory')) { | ||
$name = substr($name, 0, -7); | ||
} | ||
|
||
$modelName = $this->qualifyModel(Str::after($name, $this->rootNamespace())); | ||
|
||
if (class_exists($modelName)) { | ||
return $modelName; | ||
} | ||
|
||
if (is_dir(app_path('Models/'))) { | ||
return $this->rootNamespace().'Models\Model'; | ||
} | ||
|
||
return $this->rootNamespace().'Model'; | ||
} | ||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace Savannabits\Modular\Support\Concerns; | ||
|
||
use Illuminate\Support\Str; | ||
use Savannabits\Modular\Facades\Modular; | ||
use Savannabits\Modular\Module; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
trait GeneratesModularFiles | ||
{ | ||
protected function getArguments(): array | ||
{ | ||
return array_merge(parent::getArguments(), [ | ||
['module', InputArgument::REQUIRED, 'The name of the module in which this should be installed'], | ||
]); | ||
} | ||
|
||
protected function resolveStubPath($stub): string | ||
{ | ||
return file_exists($customPath = $this->laravel->basePath(trim($stub, '/'))) | ||
? $customPath | ||
: Modular::packagePath(trim($stub, DIRECTORY_SEPARATOR)); | ||
} | ||
|
||
public function getModule(): Module | ||
{ | ||
return Modular::module($this->argument('module')); | ||
} | ||
|
||
protected function getDefaultNamespace($rootNamespace): string | ||
{ | ||
return trim($rootNamespace, '\\').'\\'.trim(Str::replace(DIRECTORY_SEPARATOR, '\\', $this->getRelativeNamespace()), '\\'); | ||
} | ||
|
||
protected function getRelativeNamespace(): string | ||
{ | ||
return ''; | ||
} | ||
|
||
protected function rootNamespace(): string | ||
{ | ||
return $this->getModule()->getRootNamespace(); | ||
} | ||
|
||
protected function getPath($name): string | ||
{ | ||
$name = Str::replaceFirst($this->rootNamespace(), '', $name); | ||
|
||
return $this->getModule()->srcPath(str_replace('\\', '/', $name).'.php'); | ||
} | ||
} |