Concerns
1 week ago
Connection
1 week ago
Constants
1 month ago
Contracts
1 week ago
Migrations
1 month ago
Query
1 week ago
Schema
1 week ago
Seeder.php
1 week ago
Seeder.php
99 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Base class for database seeders with call tracking to prevent duplicate execution. |
| 5 | * Resolves seeder classes from the container and runs them within optional transactions. |
| 6 | * Coordinates ordered seeding through the static call API. |
| 7 | * |
| 8 | * @package Framework |
| 9 | * @subpackage Database |
| 10 | * @since 1.0.0 |
| 11 | */ |
| 12 | namespace Kirki\Framework\Database; |
| 13 | |
| 14 | \defined('ABSPATH') || exit; |
| 15 | use Kirki\Framework\Supports\Arr; |
| 16 | use Throwable; |
| 17 | use function Kirki\Framework\app; |
| 18 | class Seeder |
| 19 | { |
| 20 | /** |
| 21 | * Store the called seeder classes |
| 22 | * |
| 23 | * @var string[] |
| 24 | * |
| 25 | * @since 1.0.0 |
| 26 | */ |
| 27 | protected static $called = []; |
| 28 | /** |
| 29 | * Track the already resolved seeders so that it doesn't run again |
| 30 | * |
| 31 | * @var string[] |
| 32 | * |
| 33 | * @since 1.0.0 |
| 34 | */ |
| 35 | protected static $resolved = []; |
| 36 | /** |
| 37 | * Call the seeders |
| 38 | * |
| 39 | * @param mixed $class The class. |
| 40 | * |
| 41 | * @return Seeder |
| 42 | * |
| 43 | * @since 1.0.0 |
| 44 | */ |
| 45 | public function call($class) |
| 46 | { |
| 47 | $classes = Arr::wrap($class); |
| 48 | foreach ($classes as $class) { |
| 49 | if (!\in_array($class, static::$called, \true) && empty(static::$resolved[$class])) { |
| 50 | static::$called[] = $class; |
| 51 | } |
| 52 | } |
| 53 | return $this; |
| 54 | } |
| 55 | /** |
| 56 | * Resolve the seeder |
| 57 | * |
| 58 | * @param mixed $class The class. |
| 59 | * |
| 60 | * @return Seeder |
| 61 | * |
| 62 | * @since 1.0.0 |
| 63 | */ |
| 64 | protected function resolve($class) |
| 65 | { |
| 66 | return app()->make($class); |
| 67 | } |
| 68 | /** |
| 69 | * Run the seeder |
| 70 | * |
| 71 | * @return void |
| 72 | * |
| 73 | * @since 1.0.0 |
| 74 | */ |
| 75 | public function run() |
| 76 | { |
| 77 | // |
| 78 | } |
| 79 | /** |
| 80 | * Run the seeders |
| 81 | * |
| 82 | * @return void |
| 83 | * |
| 84 | * @since 1.0.0 |
| 85 | */ |
| 86 | public function __invoke() |
| 87 | { |
| 88 | try { |
| 89 | while (!empty(static::$called)) { |
| 90 | $seeder = \array_shift(static::$called); |
| 91 | $this->resolve($seeder)->run(); |
| 92 | static::$resolved[$seeder] = \true; |
| 93 | } |
| 94 | } catch (Throwable $exception) { |
| 95 | throw $exception; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 |