PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.0.13
Kirki – Freeform Page Builder, Website Builder & Customizer v6.0.13
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Console / Commands / SeedCommand.php
kirki / libraries / framework / Console / Commands Last commit date
FreshCommand.php 3 weeks ago MakeClassCommand.php 3 weeks ago MakeControllerCommand.php 3 weeks ago MakeMigrationCommand.php 3 weeks ago MakeModelCommand.php 3 weeks ago MakeProviderCommand.php 3 weeks ago MakeRequestCommand.php 3 weeks ago MakeSeederCommand.php 3 weeks ago MigrateCommand.php 3 weeks ago SeedCommand.php 3 weeks ago
SeedCommand.php
200 lines
1 <?php
2
3 /**
4 * Executes database seeders, optionally targeting a specific class or the default DatabaseSeeder.
5 * Wraps seeding in transaction and error handling with logging on failure.
6 * Supports fresh database population after migrations.
7 *
8 * @package Framework
9 * @subpackage Console\Commands
10 * @since 1.0.0
11 */
12 namespace Kirki\Framework\Console\Commands;
13
14 \defined('ABSPATH') || exit;
15 use Kirki\Framework\Console\CommandBase;
16 use Kirki\Framework\Console\Synopsis;
17 use Kirki\Framework\Database\Seeder;
18 use Kirki\Framework\Database\Seeders\DatabaseSeeder;
19 use Kirki\Framework\Supports\Facades\DB;
20 use Kirki\Framework\Supports\Facades\Log;
21 use Kirki\Framework\Supports\Facades\Schema;
22 use Kirki\Framework\Supports\Str;
23 use Exception;
24 use Throwable;
25 use function Kirki\Framework\app;
26 use function Kirki\Framework\database_path;
27 class SeedCommand extends CommandBase
28 {
29 /**
30 * The sequential arguments
31 *
32 * @var array
33 *
34 * @since 1.0.0
35 */
36 protected $args;
37 /**
38 * The assoc arguments
39 *
40 * @var array
41 *
42 * @since 1.0.0
43 */
44 protected $assoc;
45 /**
46 * Run the command
47 *
48 * @param mixed $args The positional arguments.
49 * @param mixed $assoc The associative arguments.
50 *
51 * @return void
52 *
53 * @since 1.0.0
54 */
55 public function run($args, $assoc)
56 {
57 $this->args = $args;
58 $this->assoc = $assoc;
59 $this->seed();
60 }
61 /**
62 * Discover the seeders
63 *
64 * @return array
65 *
66 * @since 1.0.0
67 */
68 protected function discover()
69 {
70 $seeders = [];
71 $seeders_files = \glob(database_path('seeders/*.php'));
72 if (empty($seeders_files)) {
73 return $seeders;
74 }
75 foreach ($seeders_files as $file) {
76 $classname = $this->classname($this->filename($file));
77 if ($this->exists($classname)) {
78 $seeders[] = $classname;
79 }
80 }
81 return $seeders;
82 }
83 /**
84 * Get the seeders
85 *
86 * @return array
87 *
88 * @since 1.0.0
89 */
90 protected function seeders()
91 {
92 if (!empty($this->assoc['class'])) {
93 return $this->seeder_classes($this->assoc['class']);
94 }
95 return $this->discover();
96 }
97 /**
98 * Get the seeder classes
99 *
100 * @param mixed $class The class.
101 *
102 * @return array
103 *
104 * @since 1.0.0
105 */
106 protected function seeder_classes($class)
107 {
108 $classes = Str::split(',', $class);
109 foreach ($classes as $class) {
110 $classname = $this->classname($class);
111 if ($this->exists($classname)) {
112 $seeders[] = $classname;
113 }
114 }
115 return $seeders;
116 }
117 /**
118 * Get the filename of the seeder
119 *
120 * @param mixed $path The path.
121 *
122 * @return string
123 *
124 * @since 1.0.0
125 */
126 protected function filename($path)
127 {
128 return \basename($path, '.php');
129 }
130 /**
131 * Get the classname of the seeder
132 *
133 * @param mixed $filename The filename.
134 *
135 * @return string
136 *
137 * @since 1.0.0
138 */
139 protected function classname($filename)
140 {
141 $namespace = app()->get_seeders_namespace() . '\\';
142 return $namespace . Str::pascal($filename);
143 }
144 /**
145 * Check if the seeder exists
146 *
147 * @param mixed $class The class.
148 *
149 * @return bool
150 *
151 * @since 1.0.0
152 */
153 protected function exists($class)
154 {
155 return \class_exists($class);
156 }
157 /**
158 * Seed the database
159 *
160 * @return void
161 *
162 * @since 1.0.0
163 */
164 protected function seed()
165 {
166 $seeders = $this->seeders();
167 DB::begin_transaction();
168 try {
169 Schema::disabled_checking_foreign_key_constraints();
170 if (!\class_exists(DatabaseSeeder::class)) {
171 $instance = app()->make(Seeder::class);
172 $instance->call($seeders);
173 $instance();
174 } else {
175 $instance = app()->make(DatabaseSeeder::class);
176 $instance->run();
177 $instance();
178 }
179 } catch (Exception $exception) {
180 DB::rollback();
181 Log::error($exception->getMessage());
182 } finally {
183 Schema::enabled_checking_foreign_key_constraints();
184 }
185 DB::commit();
186 \WP_CLI::success('Seeder run successfully');
187 }
188 /**
189 * Prepare the command synopsis and metadata
190 *
191 * @return void
192 *
193 * @since 1.0.0
194 */
195 protected function prepare()
196 {
197 $this->summary('Seed the database')->description("## EXAMPLES \n\n wp kirki db:seed")->synopsis(Synopsis::type('assoc')->name('class')->description('The seeder class name')->optional());
198 }
199 }
200