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
MakeClassCommand.php
192 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Scaffolding generator that creates a new PHP class file from a stub in the application path. |
| 5 | * Accepts a class name and optional subdirectory, resolving namespaces from composer PSR-4 mappings. |
| 6 | * Speeds up boilerplate creation for arbitrary application classes. |
| 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 MakeClassCommand 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 = app_path(); |
| 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 | * Get data for seeder file |
| 77 | * |
| 78 | * @return array |
| 79 | * |
| 80 | * @since 1.0.0 |
| 81 | */ |
| 82 | protected function data() |
| 83 | { |
| 84 | return ['seeder' => Str::pascal($this->args[0]), 'namespace' => $this->namespace(), '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 | $output_file = $data['output_file']; |
| 111 | $namespace = $data['namespace']; |
| 112 | $seeder = $data['seeder']; |
| 113 | $content = $this->populate_stub($data); |
| 114 | if (File::missing($output_file)) { |
| 115 | File::make_dir($output_file); |
| 116 | } |
| 117 | if (File::exists($output_file)) { |
| 118 | $this->cli_error('Seeder file already exists.'); |
| 119 | } |
| 120 | File::put($output_file, $content); |
| 121 | $this->cli_success(\sprintf('Seeder [%s] created successfully.', $namespace . '\\' . $seeder)); |
| 122 | } |
| 123 | /** |
| 124 | * Get the output directory |
| 125 | * |
| 126 | * @return string |
| 127 | * |
| 128 | * @since 1.0.0 |
| 129 | */ |
| 130 | protected function output_dir() |
| 131 | { |
| 132 | return $this->assoc['folder'] ? $this->output_dir . '/' . $this->assoc['folder'] : $this->output_dir; |
| 133 | } |
| 134 | /** |
| 135 | * Get the namespace for the class |
| 136 | * |
| 137 | * @return string |
| 138 | * |
| 139 | * @since 1.0.0 |
| 140 | */ |
| 141 | protected function namespace() |
| 142 | { |
| 143 | if (!empty($this->assoc['folder'])) { |
| 144 | $folder = Str::split('/', $this->assoc['folder']); |
| 145 | $folder = \array_map(function ($segment) { |
| 146 | return Str::pascal($segment); |
| 147 | }, $folder); |
| 148 | return app()->qualify_app_namespace(\implode('/', $folder)); |
| 149 | } |
| 150 | return app()->qualify_app_namespace(); |
| 151 | } |
| 152 | /** |
| 153 | * Get the stub content |
| 154 | * |
| 155 | * @return string |
| 156 | * |
| 157 | * @since 1.0.0 |
| 158 | */ |
| 159 | protected function get_stub() |
| 160 | { |
| 161 | $stub_path = $this->stub_path() . '/class.stub'; |
| 162 | if (File::missing($stub_path)) { |
| 163 | $this->cli_error('Class stub not found: ' . $stub_path); |
| 164 | } |
| 165 | return File::get($stub_path); |
| 166 | } |
| 167 | /** |
| 168 | * Populate the stub content |
| 169 | * |
| 170 | * @param mixed $data The data payload. |
| 171 | * |
| 172 | * @return string |
| 173 | * |
| 174 | * @since 1.0.0 |
| 175 | */ |
| 176 | protected function populate_stub($data) |
| 177 | { |
| 178 | return Str::replace(['{{class_name}}', '{{namespace}}'], [$data['seeder'], $data['namespace']], $data['stub']); |
| 179 | } |
| 180 | /** |
| 181 | * Prepare the command's synopsis and other metadata |
| 182 | * |
| 183 | * @return void |
| 184 | * |
| 185 | * @since 1.0.0 |
| 186 | */ |
| 187 | protected function prepare() |
| 188 | { |
| 189 | $this->summary('Create a new model class')->description("## EXAMPLES \n\n wp kirki make:class ExampleClass")->synopsis(Synopsis::type('positional')->name('classname')->description('The class name'))->synopsis(Synopsis::type('assoc')->name('folder')->description('The folder name')->optional()); |
| 190 | } |
| 191 | } |
| 192 |