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
MakeMigrationCommand.php
190 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Creates a timestamped migration file in the database migrations folder. |
| 5 | * Uses a stub template and converts the supplied name to snake_case for the filename. |
| 6 | * Integrates with the migrator by placing files where MigrationRepository discovers them. |
| 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 MakeMigrationCommand extends CommandBase |
| 22 | { |
| 23 | /** |
| 24 | * Command's positional arguments |
| 25 | * |
| 26 | * @var array |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected $args; |
| 31 | /** |
| 32 | * Command's associative arguments |
| 33 | * |
| 34 | * @var array |
| 35 | * |
| 36 | * @since 1.0.0 |
| 37 | */ |
| 38 | protected $assoc; |
| 39 | /** |
| 40 | * Migration base path |
| 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('migrations'); |
| 58 | } |
| 59 | /** |
| 60 | * Prepare the command's synopsis and other metadata |
| 61 | * |
| 62 | * @return void |
| 63 | * |
| 64 | * @since 1.0.0 |
| 65 | */ |
| 66 | protected function prepare() |
| 67 | { |
| 68 | $this->summary('Create a new migration file')->description("## EXAMPLES \n\n wp kirki make:migration create_users_table")->synopsis(Synopsis::type('positional')->name('name')->description('The migration name. The name should starts with `create_` and ends with `table`'))->synopsis(Synopsis::type('assoc')->name('prefix')->description('Table prefix (if any)')->optional()); |
| 69 | } |
| 70 | /** |
| 71 | * Run the command |
| 72 | * |
| 73 | * @param mixed $args The positional arguments. |
| 74 | * @param mixed $assoc The associative arguments. |
| 75 | * |
| 76 | * @return void |
| 77 | * |
| 78 | * @since 1.0.0 |
| 79 | */ |
| 80 | public function run($args, $assoc) |
| 81 | { |
| 82 | $this->args = $args; |
| 83 | $this->assoc = $assoc; |
| 84 | $this->create(); |
| 85 | } |
| 86 | /** |
| 87 | * Check if the command passed the validation |
| 88 | * |
| 89 | * @param mixed $args The positional arguments. |
| 90 | * @param mixed $assoc The associative arguments. |
| 91 | * |
| 92 | * @return bool |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | */ |
| 96 | protected function passed($args, $assoc) |
| 97 | { |
| 98 | return !empty($args[0]); |
| 99 | } |
| 100 | /** |
| 101 | * Get data for migration file |
| 102 | * |
| 103 | * @return array |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | protected function data() |
| 108 | { |
| 109 | return ['stub' => $this->get_stub(), 'table' => $this->table($this->args[0]), 'migration_class' => $this->migration_class($this->args[0]), 'output_file' => \sprintf('%s/%s.php', $this->output_dir, $this->migration_class($this->args[0]))]; |
| 110 | } |
| 111 | /** |
| 112 | * Get migration stub content |
| 113 | * |
| 114 | * @return string |
| 115 | * |
| 116 | * @since 1.0.0 |
| 117 | */ |
| 118 | protected function get_stub() |
| 119 | { |
| 120 | $stub_path = $this->stub_path() . '/migration.stub'; |
| 121 | if (File::missing($stub_path)) { |
| 122 | \WP_CLI::error('Migration stub not found: ' . $stub_path); |
| 123 | } |
| 124 | return File::get($stub_path); |
| 125 | } |
| 126 | /** |
| 127 | * Create migration file |
| 128 | * |
| 129 | * @return void |
| 130 | * |
| 131 | * @since 1.0.0 |
| 132 | */ |
| 133 | protected function create() |
| 134 | { |
| 135 | $data = $this->data(); |
| 136 | $migration_class = $data['migration_class']; |
| 137 | $output_file = $data['output_file']; |
| 138 | if (File::exists($output_file)) { |
| 139 | \WP_CLI::error('Migration file already exists.'); |
| 140 | } |
| 141 | $content = $this->populate_stub($data); |
| 142 | File::put($output_file, $content); |
| 143 | \WP_CLI::success(\sprintf('Migration [%s] created.', $migration_class)); |
| 144 | } |
| 145 | /** |
| 146 | * Get table name from filename |
| 147 | * |
| 148 | * @param mixed $filename The filename. |
| 149 | * |
| 150 | * @return string |
| 151 | * |
| 152 | * @since 1.0.0 |
| 153 | */ |
| 154 | protected function table($filename) |
| 155 | { |
| 156 | $table = Str::replace(['create_', '_table'], '', $filename); |
| 157 | $prefix = $this->assoc['prefix'] ?? app()->prefix(); |
| 158 | if ($prefix) { |
| 159 | return \sprintf('%s_%s', Str::snake(Str::trim($prefix, '_')), $table); |
| 160 | } |
| 161 | return $table; |
| 162 | } |
| 163 | /** |
| 164 | * Get migration class name from filename |
| 165 | * |
| 166 | * @param mixed $filename The filename. |
| 167 | * |
| 168 | * @return string |
| 169 | * |
| 170 | * @since 1.0.0 |
| 171 | */ |
| 172 | protected function migration_class($filename) |
| 173 | { |
| 174 | return Str::pascal($filename); |
| 175 | } |
| 176 | /** |
| 177 | * Populate stub content |
| 178 | * |
| 179 | * @param mixed $data The data payload. |
| 180 | * |
| 181 | * @return string |
| 182 | * |
| 183 | * @since 1.0.0 |
| 184 | */ |
| 185 | protected function populate_stub($data) |
| 186 | { |
| 187 | return Str::replace(['{{class_name}}', '{{table}}', '{{migrations_namespace}}'], [$data['migration_class'], $data['table'], app()->get_migrations_namespace()], $data['stub']); |
| 188 | } |
| 189 | } |
| 190 |