1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18:
19: App::uses('AppShell', 'Console/Command');
20: App::uses('File', 'Utility');
21: App::uses('Folder', 'Utility');
22:
23: 24: 25: 26: 27:
28: class PluginTask extends AppShell {
29:
30: 31: 32: 33: 34:
35: public $path = null;
36:
37: 38: 39: 40: 41:
42: public $bootstrap = null;
43:
44: 45: 46: 47: 48:
49: public function initialize() {
50: $this->path = current(App::path('plugins'));
51: $this->bootstrap = APP . 'Config' . DS . 'bootstrap.php';
52: }
53:
54: 55: 56: 57: 58:
59: public function execute() {
60: if (isset($this->args[0])) {
61: $plugin = Inflector::camelize($this->args[0]);
62: $pluginPath = $this->_pluginPath($plugin);
63: if (is_dir($pluginPath)) {
64: $this->out(__d('cake_console', 'Plugin: %s already exists, no action taken', $plugin));
65: $this->out(__d('cake_console', 'Path: %s', $pluginPath));
66: return false;
67: } else {
68: $this->_interactive($plugin);
69: }
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: $File = 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: protected function _modifyBootstrap($plugin) {
176: $bootstrap = new File($this->bootstrap, false);
177: $contents = $bootstrap->read();
178: if (!preg_match("@\n\s*CakePlugin::loadAll@", $contents)) {
179: $bootstrap->append("\nCakePlugin::load('$plugin', array('bootstrap' => false, 'routes' => false));\n");
180: $this->out('');
181: $this->out(__d('cake_dev', '%s modified', $this->bootstrap));
182: }
183: }
184:
185: 186: 187: 188: 189: 190:
191: public function findPath($pathOptions) {
192: $valid = false;
193: foreach ($pathOptions as $i => $path) {
194: if (!is_dir($path)) {
195: array_splice($pathOptions, $i, 1);
196: }
197: }
198: $max = count($pathOptions);
199: while (!$valid) {
200: foreach ($pathOptions as $i => $option) {
201: $this->out($i + 1 . '. ' . $option);
202: }
203: $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
204: $choice = $this->in($prompt, null, 1);
205: if (intval($choice) > 0 && intval($choice) <= $max) {
206: $valid = true;
207: }
208: }
209: $this->path = $pathOptions[$choice - 1];
210: }
211:
212: 213: 214: 215: 216:
217: public function getOptionParser() {
218: $parser = parent::getOptionParser();
219: return $parser->description(__d('cake_console',
220: 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
221: 'Can create plugins in any of your bootstrapped plugin paths.'
222: ))->addArgument('name', array(
223: 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
224: ));
225: }
226:
227: }
228: