Seeder.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Support\Arr;
  4. use InvalidArgumentException;
  5. use Illuminate\Console\Command;
  6. use Illuminate\Container\Container;
  7. abstract class Seeder
  8. {
  9. /**
  10. * The container instance.
  11. *
  12. * @var \Illuminate\Container\Container
  13. */
  14. protected $container;
  15. /**
  16. * The console command instance.
  17. *
  18. * @var \Illuminate\Console\Command
  19. */
  20. protected $command;
  21. /**
  22. * Seed the given connection from the given path.
  23. *
  24. * @param array|string $class
  25. * @param bool $silent
  26. * @return $this
  27. */
  28. public function call($class, $silent = false)
  29. {
  30. $classes = Arr::wrap($class);
  31. foreach ($classes as $class) {
  32. $seeder = $this->resolve($class);
  33. if ($silent === false && isset($this->command)) {
  34. $this->command->getOutput()->writeln('<info>Seeding:</info> '.get_class($seeder));
  35. }
  36. $seeder->__invoke();
  37. }
  38. return $this;
  39. }
  40. /**
  41. * Silently seed the given connection from the given path.
  42. *
  43. * @param array|string $class
  44. * @return void
  45. */
  46. public function callSilent($class)
  47. {
  48. $this->call($class, true);
  49. }
  50. /**
  51. * Resolve an instance of the given seeder class.
  52. *
  53. * @param string $class
  54. * @return \Illuminate\Database\Seeder
  55. */
  56. protected function resolve($class)
  57. {
  58. if (isset($this->container)) {
  59. $instance = $this->container->make($class);
  60. $instance->setContainer($this->container);
  61. } else {
  62. $instance = new $class;
  63. }
  64. if (isset($this->command)) {
  65. $instance->setCommand($this->command);
  66. }
  67. return $instance;
  68. }
  69. /**
  70. * Set the IoC container instance.
  71. *
  72. * @param \Illuminate\Container\Container $container
  73. * @return $this
  74. */
  75. public function setContainer(Container $container)
  76. {
  77. $this->container = $container;
  78. return $this;
  79. }
  80. /**
  81. * Set the console command instance.
  82. *
  83. * @param \Illuminate\Console\Command $command
  84. * @return $this
  85. */
  86. public function setCommand(Command $command)
  87. {
  88. $this->command = $command;
  89. return $this;
  90. }
  91. /**
  92. * Run the database seeds.
  93. *
  94. * @return mixed
  95. *
  96. * @throws \InvalidArgumentException
  97. */
  98. public function __invoke()
  99. {
  100. if (! method_exists($this, 'run')) {
  101. throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
  102. }
  103. return isset($this->container)
  104. ? $this->container->call([$this, 'run'])
  105. : $this->run();
  106. }
  107. }