1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: App::uses('AppShell', 'Console/Command');
19: App::uses('File', 'Utility');
20: App::uses('Folder', 'Utility');
21:
22: 23: 24: 25: 26:
27: class PluginTask extends AppShell {
28:
29: 30: 31: 32: 33:
34: public $path = null;
35:
36: 37: 38: 39: 40:
41: public $bootstrap = null;
42:
43: 44: 45: 46: 47:
48: public function initialize() {
49: $this->path = current(App::path('plugins'));
50: $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
51: }
52:
53: 54: 55: 56: 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: 75: 76: 77: 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: 91: 92: 93: 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: 170: 171: 172: 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: 186: 187: 188: 189:
190: public function findPath($pathOptions) {
191: $valid = false;
192: foreach ($pathOptions as $i => $path) {
193: if (!is_dir($path)) {
194: unset($pathOptions[$i]);
195: }
196: }
197: $pathOptions = array_values($pathOptions);
198:
199: $max = count($pathOptions);
200: while (!$valid) {
201: foreach ($pathOptions as $i => $option) {
202: $this->out($i + 1 . '. ' . $option);
203: }
204: $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
205: $choice = $this->in($prompt, null, 1);
206: if (intval($choice) > 0 && intval($choice) <= $max) {
207: $valid = true;
208: }
209: }
210: $this->path = $pathOptions[$choice - 1];
211: }
212:
213: 214: 215: 216: 217:
218: public function getOptionParser() {
219: $parser = parent::getOptionParser();
220: return $parser->description(__d('cake_console',
221: 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
222: 'Can create plugins in any of your bootstrapped plugin paths.'
223: ))->addArgument('name', array(
224: 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
225: ));
226: }
227:
228: }
229: