Seeder.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Console\View\Components\TwoColumnDetail;
  5. use Illuminate\Contracts\Container\Container;
  6. use Illuminate\Database\Console\Seeds\WithoutModelEvents;
  7. use Illuminate\Support\Arr;
  8. use InvalidArgumentException;
  9. abstract class Seeder
  10. {
  11. /**
  12. * The container instance.
  13. *
  14. * @var \Illuminate\Contracts\Container\Container
  15. */
  16. protected $container;
  17. /**
  18. * The console command instance.
  19. *
  20. * @var \Illuminate\Console\Command
  21. */
  22. protected $command;
  23. /**
  24. * Seeders that have been called at least one time.
  25. *
  26. * @var array
  27. */
  28. protected static $called = [];
  29. /**
  30. * Run the given seeder class.
  31. *
  32. * @param array|string $class
  33. * @param bool $silent
  34. * @param array $parameters
  35. * @return $this
  36. */
  37. public function call($class, $silent = false, array $parameters = [])
  38. {
  39. $classes = Arr::wrap($class);
  40. foreach ($classes as $class) {
  41. $seeder = $this->resolve($class);
  42. $name = get_class($seeder);
  43. if ($silent === false && isset($this->command)) {
  44. with(new TwoColumnDetail($this->command->getOutput()))->render(
  45. $name,
  46. '<fg=yellow;options=bold>RUNNING</>'
  47. );
  48. }
  49. $startTime = microtime(true);
  50. $seeder->__invoke($parameters);
  51. if ($silent === false && isset($this->command)) {
  52. $runTime = number_format((microtime(true) - $startTime) * 1000);
  53. with(new TwoColumnDetail($this->command->getOutput()))->render(
  54. $name,
  55. "<fg=gray>$runTime ms</> <fg=green;options=bold>DONE</>"
  56. );
  57. $this->command->getOutput()->writeln('');
  58. }
  59. static::$called[] = $class;
  60. }
  61. return $this;
  62. }
  63. /**
  64. * Run the given seeder class.
  65. *
  66. * @param array|string $class
  67. * @param array $parameters
  68. * @return void
  69. */
  70. public function callWith($class, array $parameters = [])
  71. {
  72. $this->call($class, false, $parameters);
  73. }
  74. /**
  75. * Silently run the given seeder class.
  76. *
  77. * @param array|string $class
  78. * @param array $parameters
  79. * @return void
  80. */
  81. public function callSilent($class, array $parameters = [])
  82. {
  83. $this->call($class, true, $parameters);
  84. }
  85. /**
  86. * Run the given seeder class once.
  87. *
  88. * @param array|string $class
  89. * @param bool $silent
  90. * @return void
  91. */
  92. public function callOnce($class, $silent = false, array $parameters = [])
  93. {
  94. if (in_array($class, static::$called)) {
  95. return;
  96. }
  97. $this->call($class, $silent, $parameters);
  98. }
  99. /**
  100. * Resolve an instance of the given seeder class.
  101. *
  102. * @param string $class
  103. * @return \Illuminate\Database\Seeder
  104. */
  105. protected function resolve($class)
  106. {
  107. if (isset($this->container)) {
  108. $instance = $this->container->make($class);
  109. $instance->setContainer($this->container);
  110. } else {
  111. $instance = new $class;
  112. }
  113. if (isset($this->command)) {
  114. $instance->setCommand($this->command);
  115. }
  116. return $instance;
  117. }
  118. /**
  119. * Set the IoC container instance.
  120. *
  121. * @param \Illuminate\Contracts\Container\Container $container
  122. * @return $this
  123. */
  124. public function setContainer(Container $container)
  125. {
  126. $this->container = $container;
  127. return $this;
  128. }
  129. /**
  130. * Set the console command instance.
  131. *
  132. * @param \Illuminate\Console\Command $command
  133. * @return $this
  134. */
  135. public function setCommand(Command $command)
  136. {
  137. $this->command = $command;
  138. return $this;
  139. }
  140. /**
  141. * Run the database seeds.
  142. *
  143. * @param array $parameters
  144. * @return mixed
  145. *
  146. * @throws \InvalidArgumentException
  147. */
  148. public function __invoke(array $parameters = [])
  149. {
  150. if (! method_exists($this, 'run')) {
  151. throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
  152. }
  153. $callback = fn () => isset($this->container)
  154. ? $this->container->call([$this, 'run'], $parameters)
  155. : $this->run(...$parameters);
  156. $uses = array_flip(class_uses_recursive(static::class));
  157. if (isset($uses[WithoutModelEvents::class])) {
  158. $callback = $this->withoutModelEvents($callback);
  159. }
  160. return $callback();
  161. }
  162. }