FactoryMakeCommand.php 1.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. $model = $this->option('model')
  43. ? $this->qualifyClass($this->option('model'))
  44. : 'Model';
  45. return str_replace(
  46. 'DummyModel', $model, parent::buildClass($name)
  47. );
  48. }
  49. /**
  50. * Get the destination class path.
  51. *
  52. * @param string $name
  53. * @return string
  54. */
  55. protected function getPath($name)
  56. {
  57. $name = str_replace(
  58. ['\\', '/'], '', $this->argument('name')
  59. );
  60. return $this->laravel->databasePath()."/factories/{$name}.php";
  61. }
  62. /**
  63. * Get the console command options.
  64. *
  65. * @return array
  66. */
  67. protected function getOptions()
  68. {
  69. return [
  70. ['model', 'm', InputOption::VALUE_OPTIONAL, 'The name of the model'],
  71. ];
  72. }
  73. }