/
PluginTask.php
328 lines (292 loc) · 9.37 KB
/
PluginTask.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
<?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\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\Shell\Task\BakeTask;
/**
* The Plugin Task handles creating an empty plugin, ready to be used
*
*/
class PluginTask extends BakeTask {
/**
* Path to the bootstrap file. Changed in tests.
*
* @var string
*/
public $bootstrap = null;
/**
* Tasks this task uses.
*
* @var array
*/
public $tasks = ['Template', 'Project'];
/**
* initialize
*
* @return void
*/
public function initialize() {
$this->path = current(App::path('Plugin'));
$this->bootstrap = ROOT . DS . 'config' . DS . 'bootstrap.php';
}
/**
* Execution method always used for tasks
*
* @param string $name The name of the plugin to bake.
* @return void
*/
public function main($name = null) {
if (empty($name)) {
$this->err('<error>You must provide a plugin name in CamelCase format.</error>');
$this->err('To make an "Example" plugin, run <info>`cake bake plugin Example`</info>.');
return false;
}
$plugin = $this->_camelize($name);
$pluginPath = $this->_pluginPath($plugin);
if (is_dir($pluginPath)) {
$this->out(sprintf('Plugin: %s already exists, no action taken', $plugin));
$this->out(sprintf('Path: %s', $pluginPath));
return false;
}
if (!$this->bake($plugin)) {
$this->error(sprintf("An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
}
}
/**
* Bake the plugin, create directories and files
*
* @param string $plugin Name of the plugin in CamelCased format
* @return bool
*/
public function bake($plugin) {
$pathOptions = App::path('Plugin');
if (count($pathOptions) > 1) {
$this->findPath($pathOptions);
}
$this->hr();
$this->out(sprintf("<info>Plugin Name:</info> %s", $plugin));
$this->out(sprintf("<info>Plugin Directory:</info> %s", $this->path . $plugin));
$this->hr();
$classBase = 'src';
$looksGood = $this->in('Look okay?', ['y', 'n', 'q'], 'y');
if (strtolower($looksGood) === 'y') {
$Folder = new Folder($this->path . $plugin);
$directories = [
'config',
$classBase . DS . 'Model' . DS . 'Behavior',
$classBase . DS . 'Model' . DS . 'Table',
$classBase . DS . 'Model' . DS . 'Entity',
$classBase . DS . 'Shell' . DS . 'Task',
$classBase . DS . 'Controller' . DS . 'Component',
$classBase . DS . 'View' . DS . 'Helper',
$classBase . DS . 'Template',
'tests' . DS . 'TestCase' . DS . 'Controller' . DS . 'Component',
'tests' . DS . 'TestCase' . DS . 'View' . DS . 'Helper',
'tests' . DS . 'TestCase' . DS . 'Model' . DS . 'Behavior',
'tests' . DS . 'Fixture',
'webroot'
];
foreach ($directories as $directory) {
$dirPath = $this->path . $plugin . DS . $directory;
$Folder->create($dirPath);
new File($dirPath . DS . 'empty', true);
}
foreach ($Folder->messages() as $message) {
$this->out($message, 1, Shell::VERBOSE);
}
$errors = $Folder->errors();
if (!empty($errors)) {
foreach ($errors as $message) {
$this->error($message);
}
return false;
}
$controllerFileName = 'AppController.php';
$out = "<?php\n\n";
$out .= "namespace {$plugin}\\Controller;\n\n";
$out .= "use App\\Controller\\AppController as BaseController;\n\n";
$out .= "class AppController extends BaseController {\n\n";
$out .= "}\n";
$this->createFile($this->path . $plugin . DS . $classBase . DS . 'Controller' . DS . $controllerFileName, $out);
$emptyFile = $this->path . 'empty';
$this->_deleteEmptyFile($emptyFile);
$hasAutoloader = $this->_modifyAutoloader($plugin, $this->path);
$this->_generateRoutes($plugin, $this->path);
$this->_modifyBootstrap($plugin, $hasAutoloader);
$this->_generatePhpunitXml($plugin, $this->path);
$this->_generateTestBootstrap($plugin, $this->path);
$this->hr();
$this->out(sprintf('<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
}
return true;
}
/**
* Update the app's bootstrap.php file.
*
* @param string $plugin Name of plugin
* @param bool $hasAutoloader Whether or not there is an autoloader configured for
* the plugin
* @return void
*/
protected function _modifyBootstrap($plugin, $hasAutoloader) {
$bootstrap = new File($this->bootstrap, false);
$contents = $bootstrap->read();
if (!preg_match("@\n\s*Plugin::loadAll@", $contents)) {
$autoload = $hasAutoloader ? null : "'autoload' => true, ";
$bootstrap->append(sprintf(
"\nPlugin::load('%s', [%s'bootstrap' => false, 'routes' => true]);\n",
$plugin,
$autoload
));
$this->out('');
$this->out(sprintf('%s modified', $this->bootstrap));
}
}
/**
* Generate a routes file for the plugin being baked.
*
* @param string $plugin The plugin to generate routes for.
* @param string $path The path to save the routes.php file in.
* @return void
*/
protected function _generateRoutes($plugin, $path) {
$this->Template->set([
'plugin' => $plugin,
]);
$this->out('Generating routes.php file...');
$out = $this->Template->generate('config', 'routes');
$file = $path . $plugin . DS . 'config' . DS . 'routes.php';
$this->createFile($file, $out);
}
/**
* Generate a phpunit.xml stub for the plugin.
*
* @param string $plugin Name of plugin
* @param string $path The path to save the phpunit.xml file to.
* @return void
*/
protected function _generatePhpunitXml($plugin, $path) {
$this->Template->set([
'plugin' => $plugin,
'path' => $path
]);
$this->out('Generating phpunit.xml file...');
$out = $this->Template->generate('test', 'phpunit.xml');
$file = $path . $plugin . DS . 'phpunit.xml';
$this->createFile($file, $out);
}
/**
* Generate a Test/bootstrap.php stub for the plugin.
*
* @param string $plugin Name of plugin
* @param string $path The path to save the phpunit.xml file to.
* @return void
*/
protected function _generateTestBootstrap($plugin, $path) {
$this->Template->set([
'plugin' => $plugin,
'path' => $path,
'root' => ROOT
]);
$this->out('Generating tests/bootstrap.php file...');
$out = $this->Template->generate('test', 'bootstrap');
$file = $path . $plugin . DS . 'tests' . DS . 'bootstrap.php';
$this->createFile($file, $out);
}
/**
* Modifies App's composer.json to include the plugin and tries to call
* composer dump-autoload to refresh the autoloader cache
*
* @param string $plugin Name of plugin
* @param string $path The path to save the phpunit.xml file to.
* @return bool True if composer could be modified correctly
*/
protected function _modifyAutoloader($plugin, $path) {
$path = dirname($path);
$file = $path . DS . 'composer.json';
if (!file_exists($file)) {
return false;
}
$config = json_decode(file_get_contents($file), true);
$config['autoload']['psr-4'][$plugin . '\\'] = "./plugins/$plugin/src";
$config['autoload-dev']['psr-4'][$plugin . '\\Test\\'] = "./plugins/$plugin/tests";
$this->out('<info>Modifying composer autoloader</info>');
$out = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$this->createFile($file, $out);
$composer = $this->Project->findComposer();
if (!$composer) {
$this->error('Could not locate composer, Add composer to your PATH, or use the -composer option.');
return false;
}
try {
chdir($path);
$command = 'php ' . escapeshellarg($composer) . ' dump-autoload';
$this->callProcess($command);
} catch (\RuntimeException $e) {
$error = $e->getMessage();
$this->error(sprintf('Could not run `composer dump-autoload`: %s', $error));
return false;
}
return true;
}
/**
* find and change $this->path to the user selection
*
* @param array $pathOptions The list of paths to look in.
* @return void
*/
public function findPath(array $pathOptions) {
$valid = false;
foreach ($pathOptions as $i => $path) {
if (!is_dir($path)) {
unset($pathOptions[$i]);
}
}
$pathOptions = array_values($pathOptions);
$max = count($pathOptions);
while (!$valid) {
foreach ($pathOptions as $i => $option) {
$this->out($i + 1 . '. ' . $option);
}
$prompt = 'Choose a plugin path from the paths above.';
$choice = $this->in($prompt, null, 1);
if ((int)$choice > 0 && (int)$choice <= $max) {
$valid = true;
}
}
$this->path = $pathOptions[$choice - 1];
}
/**
* Gets the option parser instance and configures it.
*
* @return \Cake\Console\ConsoleOptionParser
*/
public function getOptionParser() {
$parser = parent::getOptionParser();
$parser->description(
'Create the directory structure, AppController class and testing setup for a new plugin. ' .
'Can create plugins in any of your bootstrapped plugin paths.'
)->addArgument('name', [
'help' => 'CamelCased name of the plugin to create.'
])->addOption('composer', [
'default' => ROOT . '/composer.phar',
'help' => 'The path to the composer executable.'
])->removeOption('plugin');
return $parser;
}
}