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 function initialize() {
43: $this->path = current(App::path('plugins'));
44: }
45:
46: 47: 48: 49: 50:
51: public function execute() {
52: if (isset($this->args[0])) {
53: $plugin = Inflector::camelize($this->args[0]);
54: $pluginPath = $this->_pluginPath($plugin);
55: if (is_dir($pluginPath)) {
56: $this->out(__d('cake_console', 'Plugin: %s', $plugin));
57: $this->out(__d('cake_console', 'Path: %s', $pluginPath));
58: } else {
59: $this->_interactive($plugin);
60: }
61: } else {
62: return $this->_interactive();
63: }
64: }
65:
66: 67: 68: 69: 70: 71:
72: protected function _interactive($plugin = null) {
73: while ($plugin === null) {
74: $plugin = $this->in(__d('cake_console', 'Enter the name of the plugin in CamelCase format'));
75: }
76:
77: if (!$this->bake($plugin)) {
78: $this->error(__d('cake_console', "An error occurred trying to bake: %s in %s", $plugin, $this->path . $plugin));
79: }
80: }
81:
82: 83: 84: 85: 86: 87:
88: public function bake($plugin) {
89: $pathOptions = App::path('plugins');
90: if (count($pathOptions) > 1) {
91: $this->findPath($pathOptions);
92: }
93: $this->hr();
94: $this->out(__d('cake_console', "<info>Plugin Name:</info> %s", $plugin));
95: $this->out(__d('cake_console', "<info>Plugin Directory:</info> %s", $this->path . $plugin));
96: $this->hr();
97:
98: $looksGood = $this->in(__d('cake_console', 'Look okay?'), array('y', 'n', 'q'), 'y');
99:
100: if (strtolower($looksGood) == 'y') {
101: $Folder = new Folder($this->path . $plugin);
102: $directories = array(
103: 'Config' . DS . 'Schema',
104: 'Model' . DS . 'Behavior',
105: 'Model' . DS . 'Datasource',
106: 'Console' . DS . 'Command' . DS . 'Task',
107: 'Controller' . DS . 'Component',
108: 'Lib',
109: 'View' . DS . 'Helper',
110: 'Test' . DS . 'Case' . DS . 'Controller' . DS . 'Component',
111: 'Test' . DS . 'Case' . DS . 'View' . DS . 'Helper',
112: 'Test' . DS . 'Case' . DS . 'Model' . DS . 'Behavior',
113: 'Test' . DS . 'Fixture',
114: 'Vendor',
115: 'webroot'
116: );
117:
118: foreach ($directories as $directory) {
119: $dirPath = $this->path . $plugin . DS . $directory;
120: $Folder->create($dirPath);
121: $File = new File($dirPath . DS . 'empty', true);
122: }
123:
124: foreach ($Folder->messages() as $message) {
125: $this->out($message, 1, Shell::VERBOSE);
126: }
127:
128: $errors = $Folder->errors();
129: if (!empty($errors)) {
130: return false;
131: }
132:
133: $controllerFileName = $plugin . 'AppController.php';
134:
135: $out = "<?php\n\n";
136: $out .= "class {$plugin}AppController extends AppController {\n\n";
137: $out .= "}\n\n";
138: $this->createFile($this->path . $plugin. DS . 'Controller' . DS . $controllerFileName, $out);
139:
140: $modelFileName = $plugin . 'AppModel.php';
141:
142: $out = "<?php\n\n";
143: $out .= "class {$plugin}AppModel extends AppModel {\n\n";
144: $out .= "}\n\n";
145: $this->createFile($this->path . $plugin . DS . 'Model' . DS . $modelFileName, $out);
146:
147: $this->hr();
148: $this->out(__d('cake_console', '<success>Created:</success> %s in %s', $plugin, $this->path . $plugin), 2);
149: }
150:
151: return true;
152: }
153:
154: 155: 156: 157: 158: 159:
160: public function findPath($pathOptions) {
161: $valid = false;
162: foreach ($pathOptions as $i => $path) {
163: if (!is_dir($path)) {
164: array_splice($pathOptions, $i, 1);
165: }
166: }
167: $max = count($pathOptions);
168: while (!$valid) {
169: foreach ($pathOptions as $i => $option) {
170: $this->out($i + 1 .'. ' . $option);
171: }
172: $prompt = __d('cake_console', 'Choose a plugin path from the paths above.');
173: $choice = $this->in($prompt);
174: if (intval($choice) > 0 && intval($choice) <= $max) {
175: $valid = true;
176: }
177: }
178: $this->path = $pathOptions[$choice - 1];
179: }
180:
181: 182: 183: 184: 185:
186: public function getOptionParser() {
187: $parser = parent::getOptionParser();
188: return $parser->description(__d('cake_console',
189: 'Create the directory structure, AppModel and AppController classes for a new plugin. ' .
190: 'Can create plugins in any of your bootstrapped plugin paths.'
191: ))->addArgument('name', array(
192: 'help' => __d('cake_console', 'CamelCased name of the plugin to create.')
193: ));
194:
195: }
196:
197: }
198: