| 13532 |
anikendra |
1 |
<?php
|
|
|
2 |
/**
|
|
|
3 |
* The Plugin Task handles creating an empty plugin, ready to be used
|
|
|
4 |
*
|
|
|
5 |
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
|
|
|
6 |
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
7 |
*
|
|
|
8 |
* Licensed under The MIT License
|
|
|
9 |
* For full copyright and license information, please see the LICENSE.txt
|
|
|
10 |
* Redistributions of files must retain the above copyright notice.
|
|
|
11 |
*
|
|
|
12 |
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
|
|
|
13 |
* @link http://cakephp.org CakePHP(tm) Project
|
|
|
14 |
* @since CakePHP(tm) v 1.2
|
|
|
15 |
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
16 |
*/
|
|
|
17 |
|
|
|
18 |
App::uses('AppShell', 'Console/Command');
|
|
|
19 |
App::uses('File', 'Utility');
|
|
|
20 |
App::uses('Folder', 'Utility');
|
|
|
21 |
|
|
|
22 |
/**
|
|
|
23 |
* The Plugin Task handles creating an empty plugin, ready to be used
|
|
|
24 |
*
|
|
|
25 |
* @package Cake.Console.Command.Task
|
|
|
26 |
*/
|
|
|
27 |
class PluginTask extends AppShell {
|
|
|
28 |
|
|
|
29 |
/**
|
|
|
30 |
* path to plugins directory
|
|
|
31 |
*
|
|
|
32 |
* @var array
|
|
|
33 |
*/
|
|
|
34 |
public $path = null;
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
* Path to the bootstrap file. Changed in tests.
|
|
|
38 |
*
|
|
|
39 |
* @var string
|
|
|
40 |
*/
|
|
|
41 |
public $bootstrap = null;
|
|
|
42 |
|
|
|
43 |
/**
|
|
|
44 |
* initialize
|
|
|
45 |
*
|
|
|
46 |
* @return void
|
|
|
47 |
*/
|
|
|
48 |
public function initialize() {
|
|
|
49 |
$this->path = current(App::path('plugins'));
|
|
|
50 |
$this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
/**
|
|
|
54 |
* Execution method always used for tasks
|
|
|
55 |
*
|
|
|
56 |
* @return void
|
|
|
57 |
*/
|
|
|
58 |
public function execute() {
|
|
|
59 |
if (isset($this->args[0])) {
|
|
|
60 |
$plugin = Inflector::camelize($this->args[0]);
|
|
|
61 |
$pluginPath = $this->_pluginPath($plugin);
|
|
|
62 |
if (is_dir($pluginPath)) {
|
|
|
63 |
$this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
|
|
|
64 |
$this->out(__d('cake_console', 'Path: %s', $pluginPath));
|
|
|
65 |
return false;
|
|
|
66 |
}
|
|
|
67 |
$this->_interactive($plugin);
|
|
|
68 |
} else {
|
|
|
69 |
return $this->_interactive();
|
|
|
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
/**
|
|
|
74 |
* Interactive interface
|
|
|
75 |
*
|
|
|
76 |
* @param string $plugin
|
|
|
77 |
* @return void
|
|
|
78 |
*/
|
|
|
79 |
protected function _interactive($plugin = null) {
|
|
|
80 |
while ($plugin === null) {
|
|
|
81 |
$plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (!$this->bake($plugin)) {
|
|
|
85 |
$this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
|
|
|
86 |
}
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
/**
|
|
|
90 |
* Bake the plugin, create directories and files
|
|
|
91 |
*
|
|
|
92 |
* @param string $plugin Name of the plugin in CamelCased format
|
|
|
93 |
* @return boolean
|
|
|
94 |
*/
|
|
|
95 |
public function bake($plugin) {
|
|
|
96 |
$pathOptions = App::path('plugins');
|
|
|
97 |
if (count($pathOptions) > 1) {
|
|
|
98 |
$this->findPath($pathOptions);
|
|
|
99 |
}
|
|
|
100 |
$this->hr();
|
|
|
101 |
$this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
|
|
|
102 |
$this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
|
|
|
103 |
$this->hr();
|
|
|
104 |
|
|
|
105 |
$looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
|
|
|
106 |
|
|
|
107 |
if (strtolower($looksGood) === 'y') {
|
|
|
108 |
$Folder = new Folder($this->path . $plugin);
|
|
|
109 |
$directories = array(
|
|
|
110 |
'Config' . DS . 'Schema',
|
|
|
111 |
'Model' . DS . 'Behavior',
|
|
|
112 |
'Model' . DS . 'Datasource',
|
|
|
113 |
'Console' . DS . 'Command' . DS . 'Task',
|
|
|
114 |
'Controller' . DS . 'Component',
|
|
|
115 |
'Lib',
|
|
|
116 |
'View' . DS . 'Helper',
|
|
|
117 |
'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
|
|
|
118 |
'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
|
|
|
119 |
'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
|
|
|
120 |
'Test' . DS . 'Fixture',
|
|
|
121 |
'Vendor',
|
|
|
122 |
'webroot'
|
|
|
123 |
);
|
|
|
124 |
|
|
|
125 |
foreach ($directories as $directory) {
|
|
|
126 |
$dirPath = $this->path . $plugin . DS . $directory;
|
|
|
127 |
$Folder->create($dirPath);
|
|
|
128 |
new File($dirPath . DS . 'empty', true);
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
foreach ($Folder->messages() as $message) {
|
|
|
132 |
$this->out($message, 1, Shell::VERBOSE);
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
$errors = $Folder->errors();
|
|
|
136 |
if (!empty($errors)) {
|
|
|
137 |
foreach ($errors as $message) {
|
|
|
138 |
$this->error($message);
|
|
|
139 |
}
|
|
|
140 |
return false;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
$controllerFileName = $plugin . 'AppController.php';
|
|
|
144 |
|
|
|
145 |
$out = "<?php\n\n";
|
|
|
146 |
$out .= "App::uses('AppController', 'Controller');\n\n";
|
|
|
147 |
$out .= "class {$plugin}AppController extends AppController {\n\n";
|
|
|
148 |
$out .= "}\n";
|
|
|
149 |
$this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
|
|
|
150 |
|
|
|
151 |
$modelFileName = $plugin . 'AppModel.php';
|
|
|
152 |
|
|
|
153 |
$out = "<?php\n\n";
|
|
|
154 |
$out .= "App::uses('AppModel', 'Model');\n\n";
|
|
|
155 |
$out .= "class {$plugin}AppModel extends AppModel {\n\n";
|
|
|
156 |
$out .= "}\n";
|
|
|
157 |
$this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
|
|
|
158 |
|
|
|
159 |
$this->_modifyBootstrap($plugin);
|
|
|
160 |
|
|
|
161 |
$this->hr();
|
|
|
162 |
$this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
|
|
|
163 |
}
|
|
|
164 |
|
|
|
165 |
return true;
|
|
|
166 |
}
|
|
|
167 |
|
|
|
168 |
/**
|
|
|
169 |
* Update the app's bootstrap.php file.
|
|
|
170 |
*
|
|
|
171 |
* @param string $plugin Name of plugin
|
|
|
172 |
* @return void
|
|
|
173 |
*/
|
|
|
174 |
protected function _modifyBootstrap($plugin) {
|
|
|
175 |
$bootstrap = new File($this->bootstrap, false);
|
|
|
176 |
$contents = $bootstrap->read();
|
|
|
177 |
if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
|
|
|
178 |
$bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
|
|
|
179 |
$this->out('');
|
|
|
180 |
$this->out(__d('cake_dev', '%s modified', $this->bootstrap));
|
|
|
181 |
}
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* find and change $this->path to the user selection
|
|
|
186 |
*
|
|
|
187 |
* @param array $pathOptions
|
|
|
188 |
* @return void
|
|
|
189 |
*/
|
|
|
190 |
public function findPath($pathOptions) {
|
|
|
191 |
$valid = false;
|
|
|
192 |
foreach ($pathOptions as $i => $path) {
|
|
|
193 |
if (!is_dir($path)) {
|
|
|
194 |
array_splice($pathOptions, $i, 1);
|
|
|
195 |
}
|
|
|
196 |
}
|
|
|
197 |
$max = count($pathOptions);
|
|
|
198 |
while (!$valid) {
|
|
|
199 |
foreach ($pathOptions as $i => $option) {
|
|
|
200 |
$this->out($i + 1 . '. ' . $option);
|
|
|
201 |
}
|
|
|
202 |
$prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
|
|
|
203 |
$choice = $this->in($prompt, null, 1);
|
|
|
204 |
if (intval($choice) > 0 && intval($choice) <= $max) {
|
|
|
205 |
$valid = true;
|
|
|
206 |
}
|
|
|
207 |
}
|
|
|
208 |
$this->path = $pathOptions[$choice - 1];
|
|
|
209 |
}
|
|
|
210 |
|
|
|
211 |
/**
|
|
|
212 |
* get the option parser for the plugin task
|
|
|
213 |
*
|
|
|
214 |
* @return void
|
|
|
215 |
*/
|
|
|
216 |
public function getOptionParser() {
|
|
|
217 |
$parser = parent::getOptionParser();
|
|
|
218 |
return $parser->description(__d('cake_console',
|
|
|
219 |
'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
|
|
|
220 |
'Can create plugins in any of your bootstrapped plugin paths.'
|
|
|
221 |
))->addArgument('name', array(
|
|
|
222 |
'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
|
|
|
223 |
));
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
}
|