Seeder.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Console\Command;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Support\Arr;
  6. use InvalidArgumentException;
  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. * Run the given seeder class.
  23. *
  24. * @param array|string $class
  25. * @param bool $silent
  26. * @param array $parameters
  27. * @return $this
  28. */
  29. public function call($class, $silent = false, array $parameters = [])
  30. {
  31. $classes = Arr::wrap($class);
  32. foreach ($classes as $class) {
  33. $seeder = $this->resolve($class);
  34. $name = get_class($seeder);
  35. if ($silent === false && isset($this->command)) {
  36. $this->command->getOutput()->writeln("<comment>Seeding:</comment> {$name}");
  37. }
  38. $startTime = microtime(true);
  39. $seeder->__invoke($parameters);
  40. $runTime = number_format((microtime(true) - $startTime) * 1000, 2);
  41. if ($silent === false && isset($this->command)) {
  42. $this->command->getOutput()->writeln("<info>Seeded:</info> {$name} ({$runTime}ms)");
  43. }
  44. }
  45. return $this;
  46. }
  47. /**
  48. * Run the given seeder class.
  49. *
  50. * @param array|string $class
  51. * @param array $parameters
  52. * @return void
  53. */
  54. public function callWith($class, array $parameters = [])
  55. {
  56. $this->call($class, false, $parameters);
  57. }
  58. /**
  59. * Silently run the given seeder class.
  60. *
  61. * @param array|string $class
  62. * @param array $parameters
  63. * @return void
  64. */
  65. public function callSilent($class, array $parameters = [])
  66. {
  67. $this->call($class, true, $parameters);
  68. }
  69. /**
  70. * Resolve an instance of the given seeder class.
  71. *
  72. * @param string $class
  73. * @return \Illuminate\Database\Seeder
  74. */
  75. protected function resolve($class)
  76. {
  77. if (isset($this->container)) {
  78. $instance = $this->container->make($class);
  79. $instance->setContainer($this->container);
  80. } else {
  81. $instance = new $class;
  82. }
  83. if (isset($this->command)) {
  84. $instance->setCommand($this->command);
  85. }
  86. return $instance;
  87. }
  88. /**
  89. * Set the IoC container instance.
  90. *
  91. * @param \Illuminate\Container\Container $container
  92. * @return $this
  93. */
  94. public function setContainer(Container $container)
  95. {
  96. $this->container = $container;
  97. return $this;
  98. }
  99. /**
  100. * Set the console command instance.
  101. *
  102. * @param \Illuminate\Console\Command $command
  103. * @return $this
  104. */
  105. public function setCommand(Command $command)
  106. {
  107. $this->command = $command;
  108. return $this;
  109. }
  110. /**
  111. * Run the database seeds.
  112. *
  113. * @param array $parameters
  114. * @return mixed
  115. *
  116. * @throws \InvalidArgumentException
  117. */
  118. public function __invoke(array $parameters = [])
  119. {
  120. if (! method_exists($this, 'run')) {
  121. throw new InvalidArgumentException('Method [run] missing from '.get_class($this));
  122. }
  123. return isset($this->container)
  124. ? $this->container->call([$this, 'run'], $parameters)
  125. : $this->run(...$parameters);
  126. }
  127. }