1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26: if (!class_exists('File')) {
27: uses('file');
28: }
29: 30: 31: 32: 33: 34:
35: class PluginTask extends Shell {
36: 37: 38: 39:
40: var $tasks = array('Model', 'Controller', 'View');
41: 42: 43: 44: 45: 46:
47: var $path = null;
48: 49: 50: 51: 52:
53: function initialize() {
54: $this->path = APP . 'plugins' . DS;
55: }
56: 57: 58: 59: 60:
61: function execute() {
62: if (empty($this->params['skel'])) {
63: $this->params['skel'] = '';
64: if (is_dir(CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel') === true) {
65: $this->params['skel'] = CAKE_CORE_INCLUDE_PATH.DS.'cake'.DS.'console'.DS.'libs'.DS.'templates'.DS.'skel';
66: }
67: }
68:
69: $plugin = null;
70:
71: if (isset($this->args[0])) {
72: $plugin = Inflector::camelize($this->args[0]);
73: $pluginPath = Inflector::underscore($plugin) . DS;
74: $this->Dispatch->shiftArgs();
75: if (is_dir($this->path . $pluginPath)) {
76: $this->out(sprintf('Plugin: %s', $plugin));
77: $this->out(sprintf('Path: %s', $this->path . $pluginPath));
78: $this->hr();
79: } elseif (isset($this->args[0])) {
80: $this->err(sprintf('%s in path %s not found.', $plugin, $this->path . $pluginPath));
81: $this->_stop();
82: } else {
83: $this->__interactive($plugin);
84: }
85: }
86:
87: if (isset($this->args[0])) {
88: $task = Inflector::classify($this->args[0]);
89: $this->Dispatch->shiftArgs();
90: if (in_array($task, $this->tasks)) {
91: $this->{$task}->plugin = $plugin;
92: $this->{$task}->path = $this->path . $pluginPath . Inflector::underscore(Inflector::pluralize($task)) . DS;
93:
94: if (!is_dir($this->{$task}->path)) {
95: $this->err(sprintf(__("%s directory could not be found.\nBe sure you have created %s", true), $task, $this->{$task}->path));
96: }
97: $this->{$task}->loadTasks();
98: $this->{$task}->execute();
99: }
100: }
101: }
102:
103: 104: 105: 106: 107: 108:
109: function __interactive($plugin = null) {
110: while ($plugin === null) {
111: $plugin = $this->in(__('Enter the name of the plugin in CamelCase format', true));
112: }
113:
114: if (!$this->bake($plugin)) {
115: $this->err(sprintf(__("An error occured trying to bake: %s in %s", true), $plugin, $this->path . $pluginPath));
116: }
117: }
118:
119: 120: 121: 122: 123: 124: 125:
126: function bake($plugin) {
127:
128: $pluginPath = Inflector::underscore($plugin);
129:
130: $this->hr();
131: $this->out("Plugin Name: $plugin");
132: $this->out("Plugin Directory: {$this->path}{$pluginPath}");
133: $this->hr();
134:
135:
136: $looksGood = $this->in('Look okay?', array('y', 'n', 'q'), 'y');
137:
138: if (strtolower($looksGood) == 'y' || strtolower($looksGood) == 'yes') {
139: $verbose = $this->in(__('Do you want verbose output?', true), array('y', 'n'), 'n');
140:
141: $Folder = new Folder($this->path . $pluginPath);
142: $directories = array('models' . DS . 'behaviors', 'controllers' . DS . 'components', 'views' . DS . 'helpers');
143:
144: foreach ($directories as $directory) {
145: $Folder->create($this->path . $pluginPath . DS . $directory);
146: }
147:
148: if (strtolower($verbose) == 'y' || strtolower($verbose) == 'yes') {
149: foreach ($Folder->messages() as $message) {
150: $this->out($message);
151: }
152: }
153:
154: $errors = $Folder->errors();
155: if (!empty($errors)) {
156: return false;
157: }
158:
159: $controllerFileName = $pluginPath . '_app_controller.php';
160:
161: $out = "<?php\n\n";
162: $out .= "class {$plugin}AppController extends AppController {\n\n";
163: $out .= "}\n\n";
164: $out .= "?>";
165: $this->createFile($this->path . $pluginPath. DS . $controllerFileName, $out);
166:
167: $modelFileName = $pluginPath . '_app_model.php';
168:
169: $out = "<?php\n\n";
170: $out .= "class {$plugin}AppModel extends AppModel {\n\n";
171: $out .= "}\n\n";
172: $out .= "?>";
173: $this->createFile($this->path . $pluginPath . DS . $modelFileName, $out);
174:
175: $this->hr();
176: $this->out(sprintf(__("Created: %s in %s", true), $plugin, $this->path . $pluginPath));
177: $this->hr();
178: }
179:
180: return true;
181: }
182: 183: 184: 185: 186: 187:
188: function help() {
189: $this->hr();
190: $this->out("Usage: cake bake plugin <arg1> <arg2>...");
191: $this->hr();
192: $this->out('Commands:');
193: $this->out("\n\tplugin <name>\n\t\tbakes plugin directory structure");
194: $this->out("\n\tplugin <name> model\n\t\tbakes model. Run 'cake bake model help' for more info.");
195: $this->out("\n\tplugin <name> controller\n\t\tbakes controller. Run 'cake bake controller help' for more info.");
196: $this->out("\n\tplugin <name> view\n\t\tbakes view. Run 'cake bake view help' for more info.");
197: $this->out("");
198: $this->_stop();
199: }
200: }
201: ?>