1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: App::uses('AppShell', 'Console/Command');
21: App::uses('File', 'Utility');
22: App::uses('Folder', 'Utility');
23:
24: 25: 26: 27: 28:
29: class PluginTask extends AppShell {
30:
31: 32: 33: 34: 35:
36: public $path = null;
37:
38: 39: 40: 41: 42:
43: public $bootstrap = null;
44:
45: 46: 47: 48: 49:
50: public function initialize() {
51: $this->path = current(App::path('plugins'));
52: $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
53: }
54:
55: 56: 57: 58: 59:
60: public function execute() {
61: if (isset($this->args[0])) {
62: $plugin = Inflector::camelize($this->args[0]);
63: $pluginPath = $this->_pluginPath($plugin);
64: if (is_dir($pluginPath)) {
65: $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
66: $this->out(__d('cake_console', 'Path: %s', $pluginPath));
67: return false;
68: }
69: $this->_interactive($plugin);
70: } else {
71: return $this->_interactive();
72: }
73: }
74:
75: 76: 77: 78: 79: 80:
81: protected function _interactive($plugin = null) {
82: while ($plugin === null) {
83: $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
84: }
85:
86: if (!$this->bake($plugin)) {
87: $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
88: }
89: }
90:
91: 92: 93: 94: 95: 96:
97: public function bake($plugin) {
98: $pathOptions = App::path('plugins');
99: if (count($pathOptions) > 1) {
100: $this->findPath($pathOptions);
101: }
102: $this->hr();
103: $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
104: $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
105: $this->hr();
106:
107: $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
108:
109: if (strtolower($looksGood) === 'y') {
110: $Folder = new Folder($this->path . $plugin);
111: $directories = array(
112: 'Config' . DS . 'Schema',
113: 'Model' . DS . 'Behavior',
114: 'Model' . DS . 'Datasource',
115: 'Console' . DS . 'Command' . DS . 'Task',
116: 'Controller' . DS . 'Component',
117: 'Lib',
118: 'View' . DS . 'Helper',
119: 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
120: 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
121: 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
122: 'Test' . DS . 'Fixture',
123: 'Vendor',
124: 'webroot'
125: );
126:
127: foreach ($directories as $directory) {
128: $dirPath = $this->path . $plugin . DS . $directory;
129: $Folder->create($dirPath);
130: new File($dirPath . DS . 'empty', true);
131: }
132:
133: foreach ($Folder->messages() as $message) {
134: $this->out($message, 1, Shell::VERBOSE);
135: }
136:
137: $errors = $Folder->errors();
138: if (!empty($errors)) {
139: foreach ($errors as $message) {
140: $this->error($message);
141: }
142: return false;
143: }
144:
145: $controllerFileName = $plugin . 'AppController.php';
146:
147: $out = "<?php\n\n";
148: $out .= "App::uses('AppController', 'Controller');\n\n";
149: $out .= "class {$plugin}AppController extends AppController {\n\n";
150: $out .= "}\n";
151: $this->createFile($this->path . $plugin . DS . 'Controller' . DS . $controllerFileName, $out);
152:
153: $modelFileName = $plugin . 'AppModel.php';
154:
155: $out = "<?php\n\n";
156: $out .= "App::uses('AppModel', 'Model');\n\n";
157: $out .= "class {$plugin}AppModel extends AppModel {\n\n";
158: $out .= "}\n";
159: $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
160:
161: $this->_modifyBootstrap($plugin);
162:
163: $this->hr();
164: $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
165: }
166:
167: return true;
168: }
169:
170: 171: 172: 173: 174: 175:
176: protected function _modifyBootstrap($plugin) {
177: $bootstrap = new File($this->bootstrap, false);
178: $contents = $bootstrap->read();
179: if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
180: $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
181: $this->out('');
182: $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
183: }
184: }
185:
186: 187: 188: 189: 190: 191:
192: public function findPath($pathOptions) {
193: $valid = false;
194: foreach ($pathOptions as $i => $path) {
195: if (!is_dir($path)) {
196: array_splice($pathOptions, $i, 1);
197: }
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: