PluginProbe ʕ •ᴥ•ʔ
Kirki – Freeform Page Builder, Website Builder & Customizer / 6.1.0
Kirki – Freeform Page Builder, Website Builder & Customizer v6.1.0
6.1.1 6.1.0 6.0.14 6.0.13 6.0.12 6.0.11 6.0.10 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 3.1.3 3.1.4 3.1.5 3.1.6 3.1.7 3.1.8 3.1.9 4.0.19 4.0.20 4.0.21 4.0.22 4.0.23 4.0.24 4.1 4.2.0 5.0.0 5.1.0 5.1.1 5.2.0 5.2.1 5.2.2 5.2.3 6.0.0 trunk 3.0.40 3.0.41 3.0.42 3.0.43 3.0.44 3.0.45 3.1.0 3.1.1 3.1.2
kirki / libraries / framework / Console / Commands / MakeControllerCommand.php
kirki / libraries / framework / Console / Commands Last commit date
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
MakeControllerCommand.php
171 lines
1 <?php
2
3 /**
4 * Generates a controller class stub in the app Controllers directory.
5 * Writes the file with the correct namespace and class name derived from the given argument.
6 * Follows the same stub-and-write pattern as other make commands.
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 MakeControllerCommand 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 controller
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/Controllers');
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 $content = $this->populate_stub($data);
100 $output_file = $data['output_file'];
101 if (File::exists($output_file)) {
102 \WP_CLI::error('Controller file already exists.');
103 }
104 File::put($output_file, $content);
105 $namespace = $data['namespace'];
106 $controller = $data['controller'];
107 \WP_CLI::success(\sprintf('Controller [%s\\%s] created successfully.', $namespace, $controller));
108 }
109 /**
110 * Get the data for the controller
111 *
112 * @return array
113 *
114 * @since 1.0.0
115 */
116 protected function data()
117 {
118 $controller = Str::pascal($this->args[0]);
119 $data = ['stub' => $this->get_stub(), 'controller' => $controller, 'namespace' => app()->qualify_app_namespace('Http/Controllers'), 'request_type_hint' => 'Request', 'output_file' => \sprintf('%s/%s.php', $this->output_dir, $controller)];
120 if (!empty($this->assoc['api'])) {
121 $data['namespace'] = app()->qualify_app_namespace('Http/Controllers/Api');
122 $data['output_file'] = \sprintf('%s/API/%s.php', $this->output_dir, $controller);
123 }
124 return $data;
125 }
126 /**
127 * Get the stub content
128 *
129 * @return string
130 *
131 * @since 1.0.0
132 */
133 protected function get_stub()
134 {
135 $is_resource = $this->assoc['resource'] ?? \false;
136 $stub_path = $is_resource ? $this->stub_path() . '/controllers/resource-controller.stub' : $this->stub_path() . '/controllers/controller.stub';
137 if (File::missing($stub_path)) {
138 \WP_CLI::error('Controller 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 $namespace = $data['namespace'];
154 $type_hint = $data['request_type_hint'];
155 $controller = $data['controller'];
156 $stub = $data['stub'];
157 return Str::replace(['{{class_name}}', '{{namespace}}', '{{request_type_hint}}'], [$controller, $namespace, $type_hint], $stub);
158 }
159 /**
160 * Prepare the command's synopsis and other metadata
161 *
162 * @return void
163 *
164 * @since 1.0.0
165 */
166 protected function prepare()
167 {
168 $this->summary('Create a new controller class')->description("## EXAMPLES \n\n wp kirki make:controller UserController")->synopsis(Synopsis::type('positional')->name('name')->description('The controller name'))->synopsis(Synopsis::type('flag')->name('api')->description('Create an API controller')->optional())->synopsis(Synopsis::type('flag')->name('resource')->description('Create a resource controller')->optional());
169 }
170 }
171