1 | <?php |
---|
2 | /** |
---|
3 | * The Plugin Task handles creating an empty plugin, ready to be used |
---|
4 | * |
---|
5 | * PHP versions 4 and 5 |
---|
6 | * |
---|
7 | * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) |
---|
8 | * Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
9 | * |
---|
10 | * Licensed under The MIT License |
---|
11 | * Redistributions of files must retain the above copyright notice. |
---|
12 | * |
---|
13 | * @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org) |
---|
14 | * @link http://cakephp.org CakePHP(tm) Project |
---|
15 | * @package cake |
---|
16 | * @subpackage cake.cake.console.libs.tasks |
---|
17 | * @since CakePHP(tm) v 1.2 |
---|
18 | * @license MIT License (http://www.opensource.org/licenses/mit-license.php) |
---|
19 | */ |
---|
20 | |
---|
21 | /** |
---|
22 | * Task class for creating a plugin |
---|
23 | * |
---|
24 | * @package cake |
---|
25 | * @subpackage cake.cake.console.libs.tasks |
---|
26 | */ |
---|
27 | class PluginTask extends Shell { |
---|
28 | |
---|
29 | /** |
---|
30 | * Tasks |
---|
31 | * |
---|
32 | */ |
---|
33 | var $tasks = array('Model', 'Controller', 'View'); |
---|
34 | |
---|
35 | /** |
---|
36 | * path to CONTROLLERS directory |
---|
37 | * |
---|
38 | * @var array |
---|
39 | * @access public |
---|
40 | */ |
---|
41 | var $path = null; |
---|
42 | |
---|
43 | /** |
---|
44 | * initialize |
---|
45 | * |
---|
46 | * @return void |
---|
47 | */ |
---|
48 | function initialize() { |
---|
49 | $this->path = APP . 'plugins' . DS; |
---|
50 | } |
---|
51 | |
---|
52 | /** |
---|
53 | * Execution method always used for tasks |
---|
54 | * |
---|
55 | * @return void |
---|
56 | */ |
---|
57 | function execute() { |
---|
58 | if (empty($this->params['skel'])) { |
---|
59 | $this->params['skel'] = ''; |
---|
60 | if (is_dir(CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel') === true) { |
---|
61 | $this->params['skel'] = CAKE_CORE_INCLUDE_PATH . DS . CAKE . 'console' . DS . 'templates' . DS . 'skel'; |
---|
62 | } |
---|
63 | } |
---|
64 | $plugin = null; |
---|
65 | |
---|
66 | if (isset($this->args[0])) { |
---|
67 | $plugin = Inflector::camelize($this->args[0]); |
---|
68 | $pluginPath = $this->_pluginPath($plugin); |
---|
69 | $this->Dispatch->shiftArgs(); |
---|
70 | if (is_dir($pluginPath)) { |
---|
71 | $this->out(sprintf(__('Plugin: %s', true), $plugin)); |
---|
72 | $this->out(sprintf(__('Path: %s', true), $pluginPath)); |
---|
73 | } elseif (isset($this->args[0])) { |
---|
74 | $this->err(sprintf(__('%s in path %s not found.', true), $plugin, $pluginPath)); |
---|
75 | $this->_stop(); |
---|
76 | } else { |
---|
77 | $this->__interactive($plugin); |
---|
78 | } |
---|
79 | } else { |
---|
80 | return $this->__interactive(); |
---|
81 | } |
---|
82 | |
---|
83 | if (isset($this->args[0])) { |
---|
84 | $task = Inflector::classify($this->args[0]); |
---|
85 | $this->Dispatch->shiftArgs(); |
---|
86 | if (in_array($task, $this->tasks)) { |
---|
87 | $this->{$task}->plugin = $plugin; |
---|
88 | $this->{$task}->path = $pluginPath . Inflector::underscore(Inflector::pluralize($task)) . DS; |
---|
89 | |
---|
90 | if (!is_dir($this->{$task}->path)) { |
---|
91 | $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path)); |
---|
92 | } |
---|
93 | $this->{$task}->loadTasks(); |
---|
94 | return $this->{$task}->execute(); |
---|
95 | } |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /** |
---|
100 | * Interactive interface |
---|
101 | * |
---|
102 | * @access private |
---|
103 | * @return void |
---|
104 | */ |
---|
105 | function __interactive($plugin = null) { |
---|
106 | while ($plugin === null) { |
---|
107 | $plugin = $this->in(__('Enter the name of the plugin in CamelCase format', true)); |
---|
108 | } |
---|
109 | |
---|
110 | if (!$this->bake($plugin)) { |
---|
111 | $this->err(sprintf(__("An error occured trying to bake: %s in %s", true), $plugin, $this->path . Inflector::underscore($pluginPath))); |
---|
112 | } |
---|
113 | } |
---|
114 | |
---|
115 | /** |
---|
116 | * Bake the plugin, create directories and files |
---|
117 | * |
---|
118 | * @params $plugin name of the plugin in CamelCased format |
---|
119 | * @access public |
---|
120 | * @return bool |
---|
121 | */ |
---|
122 | function bake($plugin) { |
---|
123 | $pluginPath = Inflector::underscore($plugin); |
---|
124 | |
---|
125 | $pathOptions = App::path('plugins'); |
---|
126 | if (count($pathOptions) > 1) { |
---|
127 | $this->findPath($pathOptions); |
---|
128 | } |
---|
129 | $this->hr(); |
---|
130 | $this->out(sprintf(__("Plugin Name: %s", true), $plugin)); |
---|
131 | $this->out(sprintf(__("Plugin Directory: %s", true), $this->path . $pluginPath)); |
---|
132 | $this->hr(); |
---|
133 | |
---|
134 | $looksGood = $this->in(__('Look okay?', true), array('y', 'n', 'q'), 'y'); |
---|
135 | |
---|
136 | if (strtolower($looksGood) == 'y') { |
---|
137 | $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n'); |
---|
138 | |
---|
139 | $Folder =& new Folder($this->path . $pluginPath); |
---|
140 | $directories = array( |
---|
141 | 'config' . DS . 'schema', |
---|
142 | 'models' . DS . 'behaviors', |
---|
143 | 'models' . DS . 'datasources', |
---|
144 | 'controllers' . DS . 'components', |
---|
145 | 'libs', |
---|
146 | 'views' . DS . 'helpers', |
---|
147 | 'tests' . DS . 'cases' . DS . 'components', |
---|
148 | 'tests' . DS . 'cases' . DS . 'helpers', |
---|
149 | 'tests' . DS . 'cases' . DS . 'behaviors', |
---|
150 | 'tests' . DS . 'cases' . DS . 'controllers', |
---|
151 | 'tests' . DS . 'cases' . DS . 'models', |
---|
152 | 'tests' . DS . 'groups', |
---|
153 | 'tests' . DS . 'fixtures', |
---|
154 | 'vendors', |
---|
155 | 'vendors' . DS . 'shells' . DS . 'tasks', |
---|
156 | 'webroot' |
---|
157 | ); |
---|
158 | |
---|
159 | foreach ($directories as $directory) { |
---|
160 | $dirPath = $this->path . $pluginPath . DS . $directory; |
---|
161 | $Folder->create($dirPath); |
---|
162 | $File =& new File($dirPath . DS . 'empty', true); |
---|
163 | } |
---|
164 | |
---|
165 | if (strtolower($verbose) == 'y') { |
---|
166 | foreach ($Folder->messages() as $message) { |
---|
167 | $this->out($message); |
---|
168 | } |
---|
169 | } |
---|
170 | |
---|
171 | $errors = $Folder->errors(); |
---|
172 | if (!empty($errors)) { |
---|
173 | return false; |
---|
174 | } |
---|
175 | |
---|
176 | $controllerFileName = $pluginPath . '_app_controller.php'; |
---|
177 | |
---|
178 | $out = "<?php\n\n"; |
---|
179 | $out .= "class {$plugin}AppController extends AppController {\n\n"; |
---|
180 | $out .= "}\n\n"; |
---|
181 | $out .= "?>"; |
---|
182 | $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out); |
---|
183 | |
---|
184 | $modelFileName = $pluginPath . '_app_model.php'; |
---|
185 | |
---|
186 | $out = "<?php\n\n"; |
---|
187 | $out .= "class {$plugin}AppModel extends AppModel {\n\n"; |
---|
188 | $out .= "}\n\n"; |
---|
189 | $out .= "?>"; |
---|
190 | $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out); |
---|
191 | |
---|
192 | $this->hr(); |
---|
193 | $this->out(sprintf(__("Created: %s in %s", true), $plugin, $this->path . $pluginPath)); |
---|
194 | $this->hr(); |
---|
195 | } |
---|
196 | |
---|
197 | return true; |
---|
198 | } |
---|
199 | |
---|
200 | /** |
---|
201 | * find and change $this->path to the user selection |
---|
202 | * |
---|
203 | * @return void |
---|
204 | */ |
---|
205 | function findPath($pathOptions) { |
---|
206 | $valid = false; |
---|
207 | $max = count($pathOptions); |
---|
208 | while (!$valid) { |
---|
209 | foreach ($pathOptions as $i => $option) { |
---|
210 | $this->out($i + 1 .'. ' . $option); |
---|
211 | } |
---|
212 | $prompt = __('Choose a plugin path from the paths above.', true); |
---|
213 | $choice = $this->in($prompt); |
---|
214 | if (intval($choice) > 0 && intval($choice) <= $max) { |
---|
215 | $valid = true; |
---|
216 | } |
---|
217 | } |
---|
218 | $this->path = $pathOptions[$choice - 1]; |
---|
219 | } |
---|
220 | |
---|
221 | /** |
---|
222 | * Help |
---|
223 | * |
---|
224 | * @return void |
---|
225 | * @access public |
---|
226 | */ |
---|
227 | function help() { |
---|
228 | $this->hr(); |
---|
229 | $this->out("Usage: cake bake plugin <arg1> <arg2>..."); |
---|
230 | $this->hr(); |
---|
231 | $this->out('Commands:'); |
---|
232 | $this->out(); |
---|
233 | $this->out("plugin <name>"); |
---|
234 | $this->out("\tbakes plugin directory structure"); |
---|
235 | $this->out(); |
---|
236 | $this->out("plugin <name> model"); |
---|
237 | $this->out("\tbakes model. Run 'cake bake model help' for more info."); |
---|
238 | $this->out(); |
---|
239 | $this->out("plugin <name> controller"); |
---|
240 | $this->out("\tbakes controller. Run 'cake bake controller help' for more info."); |
---|
241 | $this->out(); |
---|
242 | $this->out("plugin <name> view"); |
---|
243 | $this->out("\tbakes view. Run 'cake bake view help' for more info."); |
---|
244 | $this->out(); |
---|
245 | $this->_stop(); |
---|
246 | } |
---|
247 | } |
---|