-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathControllerCommand.php
More file actions
291 lines (261 loc) · 9.82 KB
/
ControllerCommand.php
File metadata and controls
291 lines (261 loc) · 9.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 0.1.0
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Bake\Command;
use Bake\Utility\TableScanner;
use Cake\Console\Arguments;
use Cake\Console\ConsoleIo;
use Cake\Console\ConsoleOptionParser;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
/**
* Task class for creating and updating controller files.
*/
class ControllerCommand extends BakeCommand
{
/**
* Path fragment for generated code.
*
* @var string
*/
public string $pathFragment = 'Controller/';
/**
* Execute the command.
*
* @param \Cake\Console\Arguments $args The command arguments.
* @param \Cake\Console\ConsoleIo $io The console io
* @return int|null The exit code or null for success
*/
public function execute(Arguments $args, ConsoleIo $io): ?int
{
$this->extractCommonProperties($args);
$name = $args->getArgument('name') ?? '';
$name = $this->_getName($name);
if (empty($name)) {
/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get($this->connection);
$scanner = new TableScanner($connection);
$io->out('Possible controllers based on your current database:');
foreach ($scanner->listUnskipped() as $table) {
$io->out('- ' . $this->_camelize($table));
}
return static::CODE_SUCCESS;
}
$controller = $this->_camelize($name);
$this->bake($controller, $args, $io);
return static::CODE_SUCCESS;
}
/**
* Assembles and writes a Controller file
*
* @param string $controllerName Controller name already pluralized and correctly cased.
* @param \Cake\Console\Arguments $args The console arguments
* @param \Cake\Console\ConsoleIo $io The console io
* @return void
*/
public function bake(string $controllerName, Arguments $args, ConsoleIo $io): void
{
$io->quiet(sprintf('Baking controller class for %s...', $controllerName));
$actions = [];
if (!$args->getOption('no-actions') && !$args->getOption('actions')) {
$actions = ['index', 'view', 'add', 'edit', 'delete'];
}
if ($args->getOption('actions')) {
$actions = array_map('trim', explode(',', (string)$args->getOption('actions')));
$actions = array_filter($actions);
}
if (!$args->getOption('actions') && Plugin::isLoaded('Authentication') && $controllerName === 'Users') {
$actions[] = 'login';
}
$helpers = $this->getHelpers($args);
$components = $this->getComponents($args);
$prefix = $this->getPrefix($args);
if ($prefix) {
$prefix = '\\' . str_replace('/', '\\', $prefix);
}
// Controllers default to importing AppController from `App`
$baseNamespace = $namespace = Configure::read('App.namespace');
if ($this->plugin) {
$namespace = $this->_pluginNamespace($this->plugin);
}
// If the plugin has an AppController other plugin controllers
// should inherit from it.
if ($this->plugin && class_exists("{$namespace}\Controller\AppController")) {
$baseNamespace = $namespace;
}
$currentModelName = $controllerName;
$plugin = $this->plugin;
if ($plugin) {
$plugin .= '.';
}
if ($this->getTableLocator()->exists($plugin . $currentModelName)) {
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName);
} else {
$modelObj = $this->getTableLocator()->get($plugin . $currentModelName, [
'connectionName' => $this->connection,
]);
}
$pluralName = $this->_variableName($currentModelName);
$singularName = $this->_singularName($currentModelName);
$singularHumanName = $this->_singularHumanName($controllerName);
$pluralHumanName = $this->_variableName($controllerName);
// Handle cases where singular and plural are identical (e.g., "news", "sheep")
// to avoid variable collisions in generated controller code
if ($singularName === $pluralName) {
$singularName .= 'Entity';
}
$defaultModel = sprintf('%s\Model\Table\%sTable', $namespace, $controllerName);
if (!class_exists($defaultModel)) {
$defaultModel = null;
}
$entityClassName = $this->_entityName($modelObj->getAlias());
$data = compact(
'actions',
'components',
'currentModelName',
'defaultModel',
'entityClassName',
'helpers',
'modelObj',
'namespace',
'baseNamespace',
'plugin',
'pluralHumanName',
'pluralName',
'prefix',
'singularHumanName',
'singularName',
);
$data['name'] = $controllerName;
$this->bakeController($controllerName, $data, $args, $io);
$this->bakeTest($controllerName, $args, $io);
}
/**
* Generate the controller code
*
* @param string $controllerName The name of the controller.
* @param array $data The data to turn into code.
* @param \Cake\Console\Arguments $args The console args
* @param \Cake\Console\ConsoleIo $io The console io
* @return void
*/
public function bakeController(string $controllerName, array $data, Arguments $args, ConsoleIo $io): void
{
$data += [
'name' => null,
'namespace' => null,
'prefix' => null,
'actions' => null,
'helpers' => null,
'components' => null,
'plugin' => null,
'pluginPath' => null,
];
$contents = $this->createTemplateRenderer()
->set($data)
->generate('Bake.Controller/controller');
$path = $this->getPath($args);
$filename = $path . $controllerName . 'Controller.php';
$io->createFile($filename, $contents, $this->force);
}
/**
* Assembles and writes a unit test file
*
* @param string $className Controller class name
* @param \Cake\Console\Arguments $args The console arguments
* @param \Cake\Console\ConsoleIo $io The console io
* @return void
*/
public function bakeTest(string $className, Arguments $args, ConsoleIo $io): void
{
if ($args->getOption('no-test')) {
return;
}
$test = new TestCommand();
$testArgs = new Arguments(
['controller', $className],
$args->getOptions(),
['type', 'name'],
);
$test->execute($testArgs, $io);
}
/**
* Get the list of components for the controller.
*
* @param \Cake\Console\Arguments $args The console arguments
* @return array<string>
*/
public function getComponents(Arguments $args): array
{
$components = [];
if ($args->getOption('components')) {
$components = explode(',', (string)$args->getOption('components'));
$components = array_values(array_filter(array_map('trim', $components)));
} else {
if (Plugin::isLoaded('Authorization')) {
$components[] = 'Authorization.Authorization';
}
}
return $components;
}
/**
* Get the list of helpers for the controller.
*
* @param \Cake\Console\Arguments $args The console arguments
* @return array<string>
*/
public function getHelpers(Arguments $args): array
{
$helpers = [];
if ($args->getOption('helpers')) {
$helpers = explode(',', (string)$args->getOption('helpers'));
$helpers = array_values(array_filter(array_map('trim', $helpers)));
}
return $helpers;
}
/**
* Gets the option parser instance and configures it.
*
* @param \Cake\Console\ConsoleOptionParser $parser The console option parser
* @return \Cake\Console\ConsoleOptionParser
*/
public function buildOptionParser(ConsoleOptionParser $parser): ConsoleOptionParser
{
$parser = $this->_setCommonOptions($parser);
$parser->setDescription(
'Bake a controller skeleton.',
)->addArgument('name', [
'help' => 'Name of the controller to bake (without the `Controller` suffix). ' .
'You can use Plugin.name to bake controllers into plugins.',
])->addOption('components', [
'help' => 'The comma separated list of components to use.',
])->addOption('helpers', [
'help' => 'The comma separated list of helpers to use.',
])->addOption('prefix', [
'help' => 'The namespace/routing prefix to use.',
])->addOption('actions', [
'help' => 'The comma separated list of actions to generate. ' .
'You can include custom methods provided by your template set here.',
])->addOption('no-test', [
'boolean' => true,
'help' => 'Do not generate a test skeleton.',
])->addOption('no-actions', [
'boolean' => true,
'help' => 'Do not generate basic CRUD action methods.',
]);
return $parser;
}
}