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
MakeModelCommand.php
161 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Scaffolds an Eloquent-style model class extending the framework Model base. |
| 5 | * Outputs to the app Models path with fillable and table conventions from the stub. |
| 6 | * Reduces repetitive model setup for new database entities. |
| 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\app_path; |
| 21 | class MakeModelCommand extends CommandBase |
| 22 | { |
| 23 | /** |
| 24 | * The arguments for the command |
| 25 | * |
| 26 | * @var array |
| 27 | * |
| 28 | * @since 1.0.0 |
| 29 | */ |
| 30 | protected $args; |
| 31 | /** |
| 32 | * The options for the command |
| 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 = app_path('Models'); |
| 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 | $this->create(); |
| 74 | } |
| 75 | /** |
| 76 | * Data. |
| 77 | * |
| 78 | * @return void |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | */ |
| 82 | protected function data() |
| 83 | { |
| 84 | return ['model' => Str::pascal($this->args[0]), 'namespace' => app()->qualify_app_namespace('Models'), 'output_file' => \sprintf('%s/%s.php', $this->output_dir, Str::pascal($this->args[0])), 'stub' => $this->get_stub()]; |
| 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 | * Create a new model file |
| 102 | * |
| 103 | * @return void |
| 104 | * |
| 105 | * @since 1.0.0 |
| 106 | */ |
| 107 | protected function create() |
| 108 | { |
| 109 | $data = $this->data(); |
| 110 | $stub = $data['stub']; |
| 111 | $model = $data['model']; |
| 112 | $output_file = $data['output_file']; |
| 113 | $content = $this->populate_stub($model, $stub); |
| 114 | if (\file_exists($output_file)) { |
| 115 | \WP_CLI::error('Model file already exists.'); |
| 116 | } |
| 117 | \file_put_contents($output_file, $content); |
| 118 | \WP_CLI::success(\sprintf('Model App\\Models\\[%s] created successfully.', $model)); |
| 119 | } |
| 120 | /** |
| 121 | * Get the stub content |
| 122 | * |
| 123 | * @return string |
| 124 | * |
| 125 | * @since 1.0.0 |
| 126 | */ |
| 127 | protected function get_stub() |
| 128 | { |
| 129 | $stub_path = $this->stub_path() . '/model.stub'; |
| 130 | if (File::missing($stub_path)) { |
| 131 | \WP_CLI::error('Model stub not found: ' . $stub_path); |
| 132 | } |
| 133 | return File::get($stub_path); |
| 134 | } |
| 135 | /** |
| 136 | * Populate the stub content |
| 137 | * |
| 138 | * @param mixed $model The model instance. |
| 139 | * @param mixed $stub The stub. |
| 140 | * |
| 141 | * @return string |
| 142 | * |
| 143 | * @since 1.0.0 |
| 144 | */ |
| 145 | protected function populate_stub($model, $stub) |
| 146 | { |
| 147 | return Str::replace(['{{class_name}}', '{{namespace}}'], [$model, app()->qualify_app_namespace('Models')], $stub); |
| 148 | } |
| 149 | /** |
| 150 | * Prepare the command's synopsis and other metadata |
| 151 | * |
| 152 | * @return void |
| 153 | * |
| 154 | * @since 1.0.0 |
| 155 | */ |
| 156 | protected function prepare() |
| 157 | { |
| 158 | $this->summary('Create a new model class')->description("## EXAMPLES \n\n wp kirki make:model User")->synopsis(Synopsis::type('positional')->name('model')->description('The model name')); |
| 159 | } |
| 160 | } |
| 161 |