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
MakeRequestCommand.php
170 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Scaffolds a form request class with validation rule placeholders. |
| 5 | * Places the file under app Requests with the resolved namespace. |
| 6 | * Supports the request validation pipeline used by controllers and DTOs. |
| 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 MakeRequestCommand 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 request |
| 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('Http/Requests'); |
| 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 | * Check if the command passed the validation |
| 77 | * |
| 78 | * @param mixed $args The positional arguments. |
| 79 | * @param mixed $assoc The associative arguments. |
| 80 | * |
| 81 | * @return bool |
| 82 | * |
| 83 | * @since 1.0.0 |
| 84 | */ |
| 85 | protected function passed($args, $assoc) |
| 86 | { |
| 87 | return !empty($args[0]); |
| 88 | } |
| 89 | /** |
| 90 | * Create a new model file |
| 91 | * |
| 92 | * @return void |
| 93 | * |
| 94 | * @since 1.0.0 |
| 95 | */ |
| 96 | protected function create() |
| 97 | { |
| 98 | $data = $this->data(); |
| 99 | $request_class = $data['request_class']; |
| 100 | $namespace = $data['namespace']; |
| 101 | $output_file = $data['output_file']; |
| 102 | $content = $this->populate_stub($data); |
| 103 | if (File::exists($output_file)) { |
| 104 | \WP_CLI::error('Request file already exists.'); |
| 105 | } |
| 106 | File::put($output_file, $content); |
| 107 | \WP_CLI::success(\sprintf('Request [%s] created successfully.', $namespace . "\\" . $request_class)); |
| 108 | } |
| 109 | /** |
| 110 | * Make the data for the request |
| 111 | * |
| 112 | * @return array |
| 113 | * |
| 114 | * @since 1.0.0 |
| 115 | */ |
| 116 | protected function data() |
| 117 | { |
| 118 | $request_class = Str::pascal($this->args[0]); |
| 119 | $data = ['stub' => $this->get_stub(), 'namespace' => app()->qualify_app_namespace('Http/Requests'), 'request_class' => $request_class, 'output_file' => \sprintf('%s/%s.php', $this->output_dir, $request_class)]; |
| 120 | $folder = $this->assoc['folder'] ?? null; |
| 121 | if ($folder) { |
| 122 | $data['namespace'] = \sprintf('%s\\%s', $data['namespace'], Str::pascal($folder)); |
| 123 | $data['output_file'] = \sprintf('%s/%s/%s.php', $this->output_dir, $folder, $request_class); |
| 124 | } |
| 125 | return $data; |
| 126 | } |
| 127 | /** |
| 128 | * Get the stub content |
| 129 | * |
| 130 | * @return string |
| 131 | * |
| 132 | * @since 1.0.0 |
| 133 | */ |
| 134 | protected function get_stub() |
| 135 | { |
| 136 | $stub_path = $this->stub_path() . '/request.stub'; |
| 137 | if (File::missing($stub_path)) { |
| 138 | \WP_CLI::error('Request stub not found: ' . $stub_path); |
| 139 | } |
| 140 | return File::get($stub_path); |
| 141 | } |
| 142 | /** |
| 143 | * Populate the stub content |
| 144 | * |
| 145 | * @param mixed $data The data payload. |
| 146 | * |
| 147 | * @return string |
| 148 | * |
| 149 | * @since 1.0.0 |
| 150 | */ |
| 151 | protected function populate_stub($data) |
| 152 | { |
| 153 | $stub = $data['stub']; |
| 154 | $request_class = $data['request_class']; |
| 155 | $namespace = $data['namespace']; |
| 156 | return Str::replace(['{{class_name}}', '{{namespace}}'], [$request_class, $namespace], $stub); |
| 157 | } |
| 158 | /** |
| 159 | * Prepare the command's synopsis and other metadata |
| 160 | * |
| 161 | * @return void |
| 162 | * |
| 163 | * @since 1.0.0 |
| 164 | */ |
| 165 | protected function prepare() |
| 166 | { |
| 167 | $this->summary('Create a new request class')->description("## EXAMPLES \n\n wp kirki make:request UserRequest")->synopsis(Synopsis::type('positional')->name('name')->description('The request name'))->synopsis(Synopsis::type('assoc')->name('folder')->description('The request folder')->optional()); |
| 168 | } |
| 169 | } |
| 170 |