/
BakeShell.php
261 lines (237 loc) · 7.09 KB
/
BakeShell.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
<?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;
use Cake\Cache\Cache;
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Datasource\ConnectionManager;
use Cake\Model\Model;
use Cake\Core\ConventionsTrait;
use Cake\Utility\Inflector;
/**
* Command-line code generation utility to automate programmer chores.
*
* Bake is CakePHP's code generation script, which can help you kickstart
* application development by writing fully functional skeleton controllers,
* models, and views. Going further, Bake can also write Unit Tests for you.
*
* @link http://book.cakephp.org/3.0/en/console-and-shells/code-generation-with-bake.html
*/
class BakeShell extends Shell {
use ConventionsTrait;
/**
* The connection being used.
*
* @var string
*/
public $connection = 'default';
/**
* Assign $this->connection to the active task if a connection param is set.
*
* @return void
*/
public function startup() {
parent::startup();
Configure::write('debug', true);
Cache::disable();
$task = $this->_camelize($this->command);
if (isset($this->{$task}) && !in_array($task, ['Project'])) {
if (isset($this->params['connection'])) {
$this->{$task}->connection = $this->params['connection'];
}
}
if (isset($this->params['connection'])) {
$this->connection = $this->params['connection'];
}
}
/**
* Override main() to handle action
*
* @return mixed
*/
public function main() {
$connections = ConnectionManager::configured();
if (empty($connections)) {
$this->out('Your database configuration was not found.');
$this->out('Add your database connection information to config/app.php.');
return false;
}
$this->out('The following commands can be used to generate skeleton code for your application.');
$this->out('');
$this->out('<info>Available bake commands:</info>');
$this->out('');
foreach ($this->tasks as $task) {
list($p, $name) = pluginSplit($task);
$this->out('- ' . Inflector::underscore($name));
}
$this->out('');
$this->out('By using <info>`cake bake [name]`</info> you can invoke a specific bake task.');
}
/**
* Locate the tasks bake will use.
*
* Scans the following paths for tasks that are subclasses of
* Cake\Shell\Task\BakeTask:
*
* - Cake/Shell/Task/
* - App/Shell/Task/
* - Shell/Task for each loaded plugin
*
* @return void
*/
public function loadTasks() {
$tasks = [];
$tasks = $this->_findTasks($tasks, CAKE, 'Cake');
$tasks = $this->_findTasks($tasks, APP, Configure::read('App.namespace'));
foreach (Plugin::loaded() as $plugin) {
$tasks = $this->_findTasks(
$tasks,
Plugin::classPath($plugin),
$plugin,
$plugin
);
}
$this->tasks = array_values($tasks);
parent::loadTasks();
}
/**
* Append matching tasks in $path to the $tasks array.
*
* @param array $tasks The task list to modify and return.
* @param string $path The base path to look in.
* @param string $namespace The base namespace.
* @param string $prefix The prefix to append.
* @return array Updated tasks.
*/
protected function _findTasks($tasks, $path, $namespace, $prefix = false) {
$path .= 'Shell/Task';
if (!is_dir($path)) {
return $tasks;
}
$candidates = $this->_findClassFiles($path, $namespace);
$classes = $this->_findTaskClasses($candidates);
foreach ($classes as $class) {
list($ns, $name) = namespaceSplit($class);
$name = substr($name, 0, -4);
$fullName = ($prefix ? $prefix . '.' : '') . $name;
$tasks[$name] = $fullName;
}
return $tasks;
}
/**
* Find task classes in a given path.
*
* @param string $path The path to scan.
* @param string $namespace Namespace.
* @return array An array of files that may contain bake tasks.
*/
protected function _findClassFiles($path, $namespace) {
$iterator = new \DirectoryIterator($path);
$candidates = [];
foreach ($iterator as $item) {
if ($item->isDot() || $item->isDir()) {
continue;
}
$name = $item->getBasename('.php');
$candidates[] = $namespace . '\Shell\Task\\' . $name;
}
return $candidates;
}
/**
* Find bake tasks in a given set of files.
*
* @param array $files The array of files.
* @return array An array of matching classes.
*/
protected function _findTaskClasses($files) {
$classes = [];
foreach ($files as $className) {
if (!class_exists($className)) {
continue;
}
$reflect = new \ReflectionClass($className);
if (!$reflect->isInstantiable()) {
continue;
}
if (!$reflect->isSubclassOf('Cake\Shell\Task\BakeTask')) {
continue;
}
$classes[] = $className;
}
return $classes;
}
/**
* Quickly bake the MVC
*
* @param string $name Name.
* @return void
*/
public function all($name = null) {
$this->out('Bake All');
$this->hr();
if (!empty($this->params['connection'])) {
$this->connection = $this->params['connection'];
}
if (empty($name)) {
$this->Model->connection = $this->connection;
$this->out('Possible model names based on your database:');
foreach ($this->Model->listAll() as $table) {
$this->out('- ' . $table);
}
$this->out('Run <info>`cake bake all [name]`</info> to generate skeleton files.');
return false;
}
foreach (['Model', 'Controller', 'View'] as $task) {
$this->{$task}->connection = $this->connection;
}
$name = $this->_modelName($name);
$this->Model->bake($name);
$this->Controller->bake($name);
$this->View->main($name);
$this->out('<success>Bake All complete.</success>', 1, Shell::QUIET);
return true;
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
'The Bake script generates controllers, views and models for your application.' .
' If run with no command line arguments, Bake guides the user through the class creation process.' .
' You can customize the generation process by telling Bake where different parts of your application are using command line arguments.'
)->addSubcommand('all', [
'help' => 'Bake a complete MVC skeleton. Optional: <name> of a model.',
])->addOption('connection', [
'help' => 'Database connection to use in conjunction with `bake all`.',
'short' => 'c',
'default' => 'default'
])->addOption('template', [
'short' => 't',
'help' => 'Theme to use when baking code.'
]);
foreach ($this->_taskMap as $task => $config) {
$taskParser = $this->{$task}->getOptionParser();
$parser->addSubcommand(Inflector::underscore($task), [
'help' => $taskParser->description(),
'parser' => $taskParser
]);
}
return $parser;
}
}