/
ViewTask.php
466 lines (422 loc) · 11.9 KB
/
ViewTask.php
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 1.2.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Shell\Task;
use Cake\Console\Shell;
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
/**
* Task class for creating and updating view files.
*
*/
class ViewTask extends BakeTask {
/**
* Tasks to be loaded by this Task
*
* @var array
*/
public $tasks = ['Model', 'Template'];
/**
* path to View directory
*
* @var array
*/
public $pathFragment = 'Template/';
/**
* Name of the controller being used
*
* @var string
*/
public $controllerName = null;
/**
* Classname of the controller being used
*
* @var string
*/
public $controllerClass = null;
/**
* Name with plugin of the model being used
*
* @var string
*/
public $modelName = null;
/**
* The template file to use
*
* @var string
*/
public $template = null;
/**
* Actions to use for scaffolding
*
* @var array
*/
public $scaffoldActions = ['index', 'view', 'add', 'edit'];
/**
* An array of action names that don't require templates. These
* actions will not emit errors when doing bakeActions()
*
* @var array
*/
public $noTemplateActions = ['delete'];
/**
* Override initialize
*
* @return void
*/
public function initialize() {
$this->path = current(App::path('Template'));
}
/**
* Execution method always used for tasks
*
* @param string $name The name of the controller to bake views for.
* @param string $template The template to bake with.
* @param string $action The action to bake with.
* @return mixed
*/
public function main($name = null, $template = null, $action = null) {
parent::main();
if (empty($name)) {
$this->out('Possible tables to bake views for based on your current database:');
$this->Model->connection = $this->connection;
foreach ($this->Model->listAll() as $table) {
$this->out('- ' . $this->_controllerName($table));
}
return true;
}
$name = $this->_getName($name);
$controller = null;
if (!empty($this->params['controller'])) {
$controller = $this->params['controller'];
}
$this->controller($name, $controller);
$this->model($name);
if (isset($template)) {
$this->template = $template;
}
if (!$action) {
$action = $this->template;
}
if ($action) {
return $this->bake($action, true);
}
$vars = $this->_loadController();
$methods = $this->_methodsToBake();
foreach ($methods as $method) {
$content = $this->getContent($method, $vars);
if ($content) {
$this->bake($method, $content);
}
}
}
/**
* Set the model class for the table.
*
* @param string $table The table/model that is being baked.
* @return void
*/
public function model($table) {
$tableName = $this->_controllerName($table);
$plugin = null;
if (!empty($this->params['plugin'])) {
$plugin = $this->params['plugin'] . '.';
}
$this->modelName = $plugin . $tableName;
}
/**
* Set the controller related properties.
*
* @param string $table The table/model that is being baked.
* @param string $controller The controller name if specified.
* @return void
*/
public function controller($table, $controller = null) {
$tableName = $this->_controllerName($table);
if (empty($controller)) {
$controller = $tableName;
}
$this->controllerName = $controller;
$plugin = $prefix = null;
if (!empty($this->params['plugin'])) {
$plugin = $this->params['plugin'] . '.';
}
if (!empty($this->params['prefix'])) {
$prefix = $this->params['prefix'] . '/';
}
$this->controllerClass = App::className($plugin . $prefix . $controller, 'Controller', 'Controller');
}
/**
* Get the path base for views.
*
* @return string
*/
public function getPath() {
$path = parent::getPath();
if (!empty($this->params['prefix'])) {
$path .= $this->_camelize($this->params['prefix']) . DS;
}
$path .= $this->controllerName . DS;
return $path;
}
/**
* Get a list of actions that can / should have views baked for them.
*
* @return array Array of action names that should be baked
*/
protected function _methodsToBake() {
$base = Configure::read('App.namespace');
$methods = [];
if (class_exists($this->controllerClass)) {
$methods = array_diff(
array_map('strtolower', get_class_methods($this->controllerClass)),
array_map('strtolower', get_class_methods($base . '\Controller\AppController'))
);
}
if (empty($methods)) {
$methods = $this->scaffoldActions;
}
foreach ($methods as $i => $method) {
if ($method[0] === '_') {
unset($methods[$i]);
}
}
return $methods;
}
/**
* Bake All views for All controllers.
*
* @return void
*/
public function all() {
$this->Model->connection = $this->connection;
$tables = $this->Model->listAll();
foreach ($tables as $table) {
$this->main($table);
}
}
/**
* Loads Controller and sets variables for the template
* Available template variables:
*
* - 'modelClass'
* - 'primaryKey'
* - 'displayField'
* - 'singularVar'
* - 'pluralVar'
* - 'singularHumanName'
* - 'pluralHumanName'
* - 'fields'
* - 'keyFields'
* - 'schema'
*
* @return array Returns variables to be made available to a view template
*/
protected function _loadController() {
$modelObj = TableRegistry::get($this->modelName);
$primaryKey = (array)$modelObj->primaryKey();
$displayField = $modelObj->displayField();
$singularVar = $this->_singularName($this->controllerName);
$singularHumanName = $this->_singularHumanName($this->controllerName);
$schema = $modelObj->schema();
$fields = $schema->columns();
$associations = $this->_associations($modelObj);
$keyFields = [];
if (!empty($associations['BelongsTo'])) {
foreach ($associations['BelongsTo'] as $assoc) {
$keyFields[$assoc['foreignKey']] = $assoc['variable'];
}
}
$pluralVar = Inflector::variable($this->controllerName);
$pluralHumanName = $this->_pluralHumanName($this->controllerName);
return compact(
'modelClass', 'schema',
'primaryKey', 'displayField',
'singularVar', 'pluralVar',
'singularHumanName', 'pluralHumanName',
'fields', 'associations', 'keyFields'
);
}
/**
* Bake a view file for each of the supplied actions
*
* @param array $actions Array of actions to make files for.
* @param array $vars The context for generating views.
* @return void
*/
public function bakeActions(array $actions, $vars) {
foreach ($actions as $action) {
$content = $this->getContent($action, $vars);
$this->bake($action, $content);
}
}
/**
* handle creation of baking a custom action view file
*
* @return void
*/
public function customAction() {
$action = '';
while (!$action) {
$action = $this->in('Action Name? (use lowercase_underscored function name)');
if (!$action) {
$this->out('The action name you supplied was empty. Please try again.');
}
}
$this->out();
$this->hr();
$this->out('The following view will be created:');
$this->hr();
$this->out(sprintf('Controller Name: %s', $this->controllerName));
$this->out(sprintf('Action Name: %s', $action));
$this->out(sprintf('Path: %s', $this->getPath() . $this->controllerName . DS . Inflector::underscore($action) . ".ctp"));
$this->hr();
$looksGood = $this->in('Look okay?', ['y', 'n'], 'y');
if (strtolower($looksGood) === 'y') {
$this->bake($action, ' ');
return $this->_stop();
}
$this->out('Bake Aborted.');
}
/**
* Assembles and writes bakes the view file.
*
* @param string $action Action to bake.
* @param string $content Content to write.
* @return string Generated file content.
*/
public function bake($action, $content = '') {
if ($content === true) {
$content = $this->getContent($action);
}
if (empty($content)) {
return false;
}
$this->out("\n" . sprintf('Baking `%s` view file...', $action), 1, Shell::QUIET);
$path = $this->getPath();
$filename = $path . Inflector::underscore($action) . '.ctp';
$this->createFile($filename, $content);
return $content;
}
/**
* Builds content from template and variables
*
* @param string $action name to generate content to
* @param array $vars passed for use in templates
* @return string content from template
*/
public function getContent($action, $vars = null) {
if (!$vars) {
$vars = $this->_loadController();
}
$this->Template->set('action', $action);
$this->Template->set('plugin', $this->plugin);
$this->Template->set($vars);
$template = $this->getTemplate($action);
if ($template) {
return $this->Template->generate('views', $template);
}
return false;
}
/**
* Gets the template name based on the action name
*
* @param string $action name
* @return string template name
*/
public function getTemplate($action) {
if ($action != $this->template && in_array($action, $this->noTemplateActions)) {
return false;
}
if (!empty($this->template) && $action != $this->template) {
return $this->template;
}
$templatePath = $this->Template->getTemplatePath();
if (!empty($this->params['prefix'])) {
$prefixed = Inflector::underscore($this->params['prefix']) . '_' . $action;
if (file_exists($templatePath . 'views/' . $prefixed . '.ctp')) {
return $prefixed;
}
$generic = preg_replace('/(.*)(_add|_edit)$/', '\1_form', $prefixed);
if (file_exists($templatePath . 'views/' . $generic . '.ctp')) {
return $generic;
}
}
if (file_exists($templatePath . 'views/' . $action . '.ctp')) {
return $action;
}
if (in_array($action, ['add', 'edit'])) {
return 'form';
}
return $action;
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
'Bake views for a controller, using built-in or custom templates. '
)->addArgument('controller', [
'help' => 'Name of the controller views to bake. Can be Plugin.name as a shortcut for plugin baking.'
])->addArgument('action', [
'help' => "Will bake a single action's file. core templates are (index, add, edit, view)"
])->addArgument('alias', [
'help' => 'Will bake the template in <action> but create the filename after <alias>.'
])->addOption('controller', [
'help' => 'The controller name if you have a controller that does not follow conventions.'
])->addOption('prefix', [
'help' => 'The routing prefix to generate views for.',
])->addSubcommand('all', [
'help' => 'Bake all CRUD action views for all controllers. Requires models and controllers to exist.'
]);
return $parser;
}
/**
* Returns associations for controllers models.
*
* @param Table $model The model to build associations for.
* @return array associations
*/
protected function _associations(Table $model) {
$keys = ['BelongsTo', 'HasOne', 'HasMany', 'BelongsToMany'];
$associations = [];
foreach ($keys as $type) {
foreach ($model->associations()->type($type) as $assoc) {
$target = $assoc->target();
$assocName = $assoc->name();
$alias = $target->alias();
$modelClass = get_class($model);
if ($modelClass !== 'Cake\ORM\Table' && get_class($target) === $modelClass) {
continue;
}
$associations[$type][$assocName] = [
'property' => $assoc->property(),
'variable' => Inflector::variable($assocName),
'primaryKey' => (array)$target->primaryKey(),
'displayField' => $target->displayField(),
'foreignKey' => $assoc->foreignKey(),
'controller' => $alias,
'fields' => $target->schema()->columns(),
];
}
}
return $associations;
}
}