Concerns
3 weeks ago
Connection
3 weeks ago
Constants
3 weeks ago
Contracts
3 weeks ago
Migrations
3 weeks ago
Query
1 week ago
Schema
3 weeks ago
Seeder.php
3 weeks ago
Seeder.php
101 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 Kirki\Framework\Supports\Facades\DB; |
| 17 | use Kirki\Framework\Supports\Facades\Log; |
| 18 | use Throwable; |
| 19 | use function Kirki\Framework\app; |
| 20 | class Seeder |
| 21 | { |
| 22 | /** |
| 23 | * Store the called seeder classes |
| 24 | * |
| 25 | * @var string[] |
| 26 | * |
| 27 | * @since 1.0.0 |
| 28 | */ |
| 29 | protected static $called = []; |
| 30 | /** |
| 31 | * Track the already resolved seeders so that it doesn't run again |
| 32 | * |
| 33 | * @var string[] |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | */ |
| 37 | protected static $resolved = []; |
| 38 | /** |
| 39 | * Call the seeders |
| 40 | * |
| 41 | * @param mixed $class The class. |
| 42 | * |
| 43 | * @return Seeder |
| 44 | * |
| 45 | * @since 1.0.0 |
| 46 | */ |
| 47 | public function call($class) |
| 48 | { |
| 49 | $classes = Arr::wrap($class); |
| 50 | foreach ($classes as $class) { |
| 51 | if (!\in_array($class, static::$called, \true) && empty(static::$resolved[$class])) { |
| 52 | static::$called[] = $class; |
| 53 | } |
| 54 | } |
| 55 | return $this; |
| 56 | } |
| 57 | /** |
| 58 | * Resolve the seeder |
| 59 | * |
| 60 | * @param mixed $class The class. |
| 61 | * |
| 62 | * @return Seeder |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | protected function resolve($class) |
| 67 | { |
| 68 | return app()->make($class); |
| 69 | } |
| 70 | /** |
| 71 | * Run the seeder |
| 72 | * |
| 73 | * @return void |
| 74 | * |
| 75 | * @since 1.0.0 |
| 76 | */ |
| 77 | public function run() |
| 78 | { |
| 79 | // |
| 80 | } |
| 81 | /** |
| 82 | * Run the seeders |
| 83 | * |
| 84 | * @return void |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | public function __invoke() |
| 89 | { |
| 90 | try { |
| 91 | while (!empty(static::$called)) { |
| 92 | $seeder = \array_shift(static::$called); |
| 93 | $this->resolve($seeder)->run(); |
| 94 | static::$resolved[$seeder] = \true; |
| 95 | } |
| 96 | } catch (Throwable $exception) { |
| 97 | throw $exception; |
| 98 | } |
| 99 | } |
| 100 | } |
| 101 |