Seeder.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. if ($silent === false && isset($this->command)) {
  33. $this->command->getOutput()->writeln("<info>Seeding:</info> $class");
  34. }
  35. $this->resolve($class)->__invoke();
  36. }
  37. return $this;
  38. }
  39. /**
  40. * Silently seed the given connection from the given path.
  41. *
  42. * @param array|string $class
  43. * @return void
  44. */
  45. public function callSilent($class)
  46. {
  47. $this->call($class, true);
  48. }
  49. /**
  50. * Resolve an instance of the given seeder class.
  51. *
  52. * @param string $class
  53. * @return \Illuminate\Database\Seeder
  54. */
  55. protected function resolve($class)
  56. {
  57. if (isset($this->container)) {
  58. $instance = $this->container->make($class);
  59. $instance->setContainer($this->container);
  60. } else {
  61. $instance = new $class;
  62. }
  63. if (isset($this->command)) {
  64. $instance->setCommand($this->command);
  65. }
  66. return $instance;
  67. }
  68. /**
  69. * Set the IoC container instance.
  70. *
  71. * @param \Illuminate\Container\Container $container
  72. * @return $this
  73. */
  74. public function setContainer(Container $container)
  75. {
  76. $this->container = $container;
  77. return $this;
  78. }
  79. /**
  80. * Set the console command instance.
  81. *
  82. * @param \Illuminate\Console\Command $command
  83. * @return $this
  84. */
  85. public function setCommand(Command $command)
  86. {
  87. $this->command = $command;
  88. return $this;
  89. }
  90. /**
  91. * Run the database seeds.
  92. *
  93. * @return void
  94. *
  95. * @throws \InvalidArgumentException
  96. */
  97. public function __invoke()
  98. {
  99. if (! method_exists($this, 'run')) {
  100. throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
  101. }
  102. return isset($this->container)
  103. ? $this->container->call([$this, 'run'])
  104. : $this->run();
  105. }
  106. }