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