FactoryMakeCommand.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace Illuminate\Database\Console\Factories;
  3. use Illuminate\Console\GeneratorCommand;
  4. use Symfony\Component\Console\Input\InputOption;
  5. class FactoryMakeCommand extends GeneratorCommand
  6. {
  7. /**
  8. * The console command name.
  9. *
  10. * @var string
  11. */
  12. protected $name = 'make:factory';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Create a new model factory';
  19. /**
  20. * The type of class being generated.
  21. *
  22. * @var string
  23. */
  24. protected $type = 'Factory';
  25. /**
  26. * Get the stub file for the generator.
  27. *
  28. * @return string
  29. */
  30. protected function getStub()
  31. {
  32. return __DIR__.'/stubs/factory.stub';
  33. }
  34. /**
  35. * Build the class with the given name.
  36. *
  37. * @param string $name
  38. * @return string
  39. */
  40. protected function buildClass($name)
  41. {
  42. $namespaceModel = $this->option('model')
  43. ? $this->qualifyClass($this->option('model'))
  44. : trim($this->rootNamespace(), '\\').'\\Model';
  45. $model = class_basename($namespaceModel);
  46. return str_replace(
  47. [
  48. 'NamespacedDummyModel',
  49. 'DummyModel',
  50. ],
  51. [
  52. $namespaceModel,
  53. $model,
  54. ],
  55. parent::buildClass($name)
  56. );
  57. }
  58. /**
  59. * Get the destination class path.
  60. *
  61. * @param string $name
  62. * @return string
  63. */
  64. protected function getPath($name)
  65. {
  66. $name = str_replace(
  67. ['\\', '/'], '', $this->argument('name')
  68. );
  69. return $this->laravel->databasePath()."/factories/{$name}.php";
  70. }
  71. /**
  72. * Get the console command options.
  73. *
  74. * @return array
  75. */
  76. protected function getOptions()
  77. {
  78. return [
  79. ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
  80. ];
  81. }
  82. }