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