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
MakeSeederCommand.php
197 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Creates a database seeder class in the seeders directory from a stub template. |
| 5 | * Names the class and file according to the supplied seeder name. |
| 6 | * Works with SeedCommand to populate tables during development. |
| 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\Supports\Facades\File; |
| 18 | use Kirki\Framework\Supports\Str; |
| 19 | use function Kirki\Framework\app; |
| 20 | use function Kirki\Framework\database_path; |
| 21 | class MakeSeederCommand extends CommandBase |
| 22 | { |
| 23 | /** |
| 24 | * The arguments |
| 25 | * |
| 26 | * @var array |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected $args; |
| 31 | /** |
| 32 | * The arguments |
| 33 | * |
| 34 | * @var array |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | protected $assoc; |
| 39 | /** |
| 40 | * The base path for the models |
| 41 | * |
| 42 | * @var string |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | */ |
| 46 | protected $output_dir; |
| 47 | /** |
| 48 | * Initialize the command |
| 49 | * |
| 50 | * @return void |
| 51 | * |
| 52 | * @since 1.0.0 |
| 53 | */ |
| 54 | public function __construct() |
| 55 | { |
| 56 | parent::__construct(); |
| 57 | $this->output_dir = database_path('seeders'); |
| 58 | } |
| 59 | /** |
| 60 | * Run the command |
| 61 | * |
| 62 | * @param mixed $args The positional arguments. |
| 63 | * @param mixed $assoc The associative arguments. |
| 64 | * |
| 65 | * @return void |
| 66 | * |
| 67 | * @since 1.0.0 |
| 68 | */ |
| 69 | public function run($args, $assoc) |
| 70 | { |
| 71 | $this->args = $args; |
| 72 | $this->assoc = $assoc; |
| 73 | if (File::missing($this->output_dir)) { |
| 74 | File::make_dir($this->output_dir); |
| 75 | } |
| 76 | if ($this->is_database_seeder_missing()) { |
| 77 | $this->create_database_seeder(); |
| 78 | } |
| 79 | $this->create(); |
| 80 | } |
| 81 | /** |
| 82 | * Determine whether the database seeder missing. |
| 83 | * |
| 84 | * @return bool |
| 85 | * |
| 86 | * @since 1.0.0 |
| 87 | */ |
| 88 | protected function is_database_seeder_missing() |
| 89 | { |
| 90 | return File::missing(database_path('seeders/DatabaseSeeder.php')); |
| 91 | } |
| 92 | /** |
| 93 | * Create database seeder. |
| 94 | * |
| 95 | * @return void |
| 96 | * |
| 97 | * @since 1.0.0 |
| 98 | */ |
| 99 | protected function create_database_seeder() |
| 100 | { |
| 101 | $data = ['seeder' => 'DatabaseSeeder', 'namespace' => app()->get_seeders_namespace(), 'output_file' => \sprintf('%s/%s.php', $this->output_dir, 'DatabaseSeeder'), 'stub' => $this->get_stub()]; |
| 102 | $output_file = $data['output_file']; |
| 103 | $namespace = $data['namespace']; |
| 104 | $seeder = $data['seeder']; |
| 105 | $content = $this->populate_stub($data); |
| 106 | if (File::exists($output_file)) { |
| 107 | \WP_CLI::error('Seeder file already exists.'); |
| 108 | } |
| 109 | File::put($output_file, $content); |
| 110 | \WP_CLI::success(\sprintf('Seeder [%s] created successfully.', $namespace . '\\' . $seeder)); |
| 111 | } |
| 112 | /** |
| 113 | * Get data for seeder file |
| 114 | * |
| 115 | * @return array |
| 116 | * |
| 117 | * @since 1.0.0 |
| 118 | */ |
| 119 | protected function data() |
| 120 | { |
| 121 | return ['seeder' => Str::pascal($this->args[0]), 'namespace' => app()->get_seeders_namespace(), 'output_file' => \sprintf('%s/%s.php', $this->output_dir, Str::pascal($this->args[0])), 'stub' => $this->get_stub()]; |
| 122 | } |
| 123 | /** |
| 124 | * Check if the command passed the validation |
| 125 | * |
| 126 | * @param mixed $args The positional arguments. |
| 127 | * @param mixed $assoc The associative arguments. |
| 128 | * |
| 129 | * @return bool |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | protected function passed($args, $assoc) |
| 134 | { |
| 135 | return !empty($args[0]); |
| 136 | } |
| 137 | /** |
| 138 | * Create a new model file |
| 139 | * |
| 140 | * @return void |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | */ |
| 144 | protected function create() |
| 145 | { |
| 146 | $data = $this->data(); |
| 147 | $output_file = $data['output_file']; |
| 148 | $namespace = $data['namespace']; |
| 149 | $seeder = $data['seeder']; |
| 150 | $content = $this->populate_stub($data); |
| 151 | if (File::exists($output_file)) { |
| 152 | \WP_CLI::error('Seeder file already exists.'); |
| 153 | } |
| 154 | File::put($output_file, $content); |
| 155 | \WP_CLI::success(\sprintf('Seeder [%s] created successfully.', $namespace . '\\' . $seeder)); |
| 156 | } |
| 157 | /** |
| 158 | * Get the stub content |
| 159 | * |
| 160 | * @return string |
| 161 | * |
| 162 | * @since 1.0.0 |
| 163 | */ |
| 164 | protected function get_stub() |
| 165 | { |
| 166 | $stub_path = $this->stub_path() . '/seeder.stub'; |
| 167 | if (File::missing($stub_path)) { |
| 168 | \WP_CLI::error('Seeder stub not found: ' . $stub_path); |
| 169 | } |
| 170 | return File::get($stub_path); |
| 171 | } |
| 172 | /** |
| 173 | * Populate the stub content |
| 174 | * |
| 175 | * @param mixed $data The data payload. |
| 176 | * |
| 177 | * @return string |
| 178 | * |
| 179 | * @since 1.0.0 |
| 180 | */ |
| 181 | protected function populate_stub($data) |
| 182 | { |
| 183 | return Str::replace(['{{class_name}}', '{{namespace}}'], [$data['seeder'], $data['namespace']], $data['stub']); |
| 184 | } |
| 185 | /** |
| 186 | * Prepare the command's synopsis and other metadata |
| 187 | * |
| 188 | * @return void |
| 189 | * |
| 190 | * @since 1.0.0 |
| 191 | */ |
| 192 | protected function prepare() |
| 193 | { |
| 194 | $this->summary('Create a new model class')->description("## EXAMPLES \n\n wp kirki make:seeder DatabaseSeeder")->synopsis(Synopsis::type('positional')->name('seeder')->description('The seeder name')); |
| 195 | } |
| 196 | } |
| 197 |