FactoryMakeCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace Illuminate\Database\Console\Factories;
  3. use Illuminate\Console\GeneratorCommand;
  4. use Illuminate\Support\Str;
  5. use Symfony\Component\Console\Attribute\AsCommand;
  6. use Symfony\Component\Console\Input\InputOption;
  7. #[AsCommand(name: 'make:factory')]
  8. class FactoryMakeCommand extends GeneratorCommand
  9. {
  10. /**
  11. * The console command name.
  12. *
  13. * @var string
  14. */
  15. protected $name = 'make:factory';
  16. /**
  17. * The console command description.
  18. *
  19. * @var string
  20. */
  21. protected $description = 'Create a new model factory';
  22. /**
  23. * The type of class being generated.
  24. *
  25. * @var string
  26. */
  27. protected $type = 'Factory';
  28. /**
  29. * Get the stub file for the generator.
  30. *
  31. * @return string
  32. */
  33. protected function getStub()
  34. {
  35. return $this->resolveStubPath('/stubs/factory.stub');
  36. }
  37. /**
  38. * Resolve the fully-qualified path to the stub.
  39. *
  40. * @param string $stub
  41. * @return string
  42. */
  43. protected function resolveStubPath($stub)
  44. {
  45. return file_exists($customPath = $this->laravel->basePath(trim($stub, '/')))
  46. ? $customPath
  47. : __DIR__.$stub;
  48. }
  49. /**
  50. * Build the class with the given name.
  51. *
  52. * @param string $name
  53. * @return string
  54. */
  55. protected function buildClass($name)
  56. {
  57. $factory = class_basename(Str::ucfirst(str_replace('Factory', '', $name)));
  58. $namespaceModel = $this->option('model')
  59. ? $this->qualifyModel($this->option('model'))
  60. : $this->qualifyModel($this->guessModelName($name));
  61. $model = class_basename($namespaceModel);
  62. $namespace = $this->getNamespace(
  63. Str::replaceFirst($this->rootNamespace(), 'Database\\Factories\\', $this->qualifyClass($this->getNameInput()))
  64. );
  65. $replace = [
  66. '{{ factoryNamespace }}' => $namespace,
  67. 'NamespacedDummyModel' => $namespaceModel,
  68. '{{ namespacedModel }}' => $namespaceModel,
  69. '{{namespacedModel}}' => $namespaceModel,
  70. 'DummyModel' => $model,
  71. '{{ model }}' => $model,
  72. '{{model}}' => $model,
  73. '{{ factory }}' => $factory,
  74. '{{factory}}' => $factory,
  75. ];
  76. return str_replace(
  77. array_keys($replace), array_values($replace), parent::buildClass($name)
  78. );
  79. }
  80. /**
  81. * Get the destination class path.
  82. *
  83. * @param string $name
  84. * @return string
  85. */
  86. protected function getPath($name)
  87. {
  88. $name = (string) Str::of($name)->replaceFirst($this->rootNamespace(), '')->finish('Factory');
  89. return $this->laravel->databasePath().'/factories/'.str_replace('\\', '/', $name).'.php';
  90. }
  91. /**
  92. * Guess the model name from the Factory name or return a default model name.
  93. *
  94. * @param string $name
  95. * @return string
  96. */
  97. protected function guessModelName($name)
  98. {
  99. if (str_ends_with($name, 'Factory')) {
  100. $name = substr($name, 0, -7);
  101. }
  102. $modelName = $this->qualifyModel(Str::after($name, $this->rootNamespace()));
  103. if (class_exists($modelName)) {
  104. return $modelName;
  105. }
  106. if (is_dir(app_path('Models/'))) {
  107. return $this->rootNamespace().'Models\Model';
  108. }
  109. return $this->rootNamespace().'Model';
  110. }
  111. /**
  112. * Get the console command options.
  113. *
  114. * @return array
  115. */
  116. protected function getOptions()
  117. {
  118. return [
  119. ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
  120. ];
  121. }
  122. }