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: 27: 28: 29:
30: class ConsoleShell extends Shell {
31: 32: 33: 34: 35: 36:
37: var $associations = array('hasOne', 'hasMany', 'belongsTo', 'hasAndBelongsToMany');
38: 39: 40: 41: 42: 43:
44: var $badCommandChars = array('$', ';');
45: 46: 47: 48: 49: 50:
51: var $models = array();
52: 53: 54: 55: 56:
57: function initialize() {
58: require_once CAKE . 'dispatcher.php';
59: $this->Dispatcher = new Dispatcher();
60: $this->models = Configure::listObjects('model');
61: App::import('Model', $this->models);
62:
63: foreach ($this->models as $model) {
64: $class = Inflector::camelize(r('.php', '', $model));
65: $this->models[$model] = $class;
66: $this->{$class} =& new $class();
67: }
68: $this->out('Model classes:');
69: $this->out('--------------');
70:
71: foreach ($this->models as $model) {
72: $this->out(" - {$model}");
73: }
74: $this->__loadRoutes();
75: }
76: 77: 78: 79: 80:
81: function help() {
82: $this->main('help');
83: }
84: 85: 86: 87: 88:
89: function main($command = null) {
90: while (true) {
91: if (empty($command)) {
92: $command = trim($this->in(''));
93: }
94:
95: switch ($command) {
96: case 'help':
97: $this->out('Console help:');
98: $this->out('-------------');
99: $this->out('The interactive console is a tool for testing parts of your app before you commit code');
100: $this->out('');
101: $this->out('Model testing:');
102: $this->out('To test model results, use the name of your model without a leading $');
103: $this->out('e.g. Foo->find("all")');
104: $this->out('');
105: $this->out('To dynamically set associations, you can do the following:');
106: $this->out("\tModelA bind <association> ModelB");
107: $this->out("where the supported assocations are hasOne, hasMany, belongsTo, hasAndBelongsToMany");
108: $this->out('');
109: $this->out('To dynamically remove associations, you can do the following:');
110: $this->out("\t ModelA unbind <association> ModelB");
111: $this->out("where the supported associations are the same as above");
112: $this->out('');
113: $this->out("To save a new field in a model, you can do the following:");
114: $this->out("\tModelA->save(array('foo' => 'bar', 'baz' => 0))");
115: $this->out("where you are passing a hash of data to be saved in the format");
116: $this->out("of field => value pairs");
117: $this->out('');
118: $this->out("To get column information for a model, use the following:");
119: $this->out("\tModelA columns");
120: $this->out("which returns a list of columns and their type");
121: $this->out('');
122: $this->out('Route testing:');
123: $this->out('To test URLs against your app\'s route configuration, type:');
124: $this->out("\tRoute <url>");
125: $this->out("where url is the path to your your action plus any query parameters, minus the");
126: $this->out("application's base path");
127: $this->out('');
128: $this->out('To reload your routes config (config/routes.php), do the following:');
129: $this->out("\tRoutes reload");
130: $this->out('');
131: $this->out('');
132: $this->out('To show all connected routes, do the following:');
133: $this->out("\tRoutes show");
134: $this->out('');
135: break;
136: case 'quit':
137: case 'exit':
138: return true;
139: break;
140: case 'models':
141: $this->out('Model classes:');
142: $this->hr();
143: foreach ($this->models as $model) {
144: $this->out(" - {$model}");
145: }
146: break;
147: case (preg_match("/^(\w+) bind (\w+) (\w+)/", $command, $tmp) == true):
148: foreach ($tmp as $data) {
149: $data = strip_tags($data);
150: $data = str_replace($this->badCommandChars, "", $data);
151: }
152:
153: $modelA = $tmp[1];
154: $association = $tmp[2];
155: $modelB = $tmp[3];
156:
157: if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations)) {
158: $this->{$modelA}->bindModel(array($association => array($modelB => array('className' => $modelB))), false);
159: $this->out("Created $association association between $modelA and $modelB");
160: } else {
161: $this->out("Please verify you are using valid models and association types");
162: }
163: break;
164: case (preg_match("/^(\w+) unbind (\w+) (\w+)/", $command, $tmp) == true):
165: foreach ($tmp as $data) {
166: $data = strip_tags($data);
167: $data = str_replace($this->badCommandChars, "", $data);
168: }
169:
170: $modelA = $tmp[1];
171: $association = $tmp[2];
172: $modelB = $tmp[3];
173:
174:
175: $currentAssociations = $this->{$modelA}->getAssociated();
176: $validCurrentAssociation = false;
177:
178: foreach ($currentAssociations as $model => $currentAssociation) {
179: if ($model == $modelB && $association == $currentAssociation) {
180: $validCurrentAssociation = true;
181: }
182: }
183:
184: if ($this->__isValidModel($modelA) && $this->__isValidModel($modelB) && in_array($association, $this->associations) && $validCurrentAssociation) {
185: $this->{$modelA}->unbindModel(array($association => array($modelB)));
186: $this->out("Removed $association association between $modelA and $modelB");
187: } else {
188: $this->out("Please verify you are using valid models, valid current association, and valid association types");
189: }
190: break;
191: case (strpos($command, "->find") > 0):
192:
193: $command = strip_tags($command);
194: $command = str_replace($this->badCommandChars, "", $command);
195:
196:
197: list($modelToCheck, $tmp) = explode('->', $command);
198:
199: if ($this->__isValidModel($modelToCheck)) {
200: $findCommand = "\$data = \$this->$command;";
201: @eval($findCommand);
202:
203: if (is_array($data)) {
204: foreach ($data as $idx => $results) {
205: if (is_numeric($idx)) {
206: foreach ($results as $modelName => $result) {
207: $this->out("$modelName");
208:
209: foreach ($result as $field => $value) {
210: if (is_array($value)) {
211: foreach ($value as $field2 => $value2) {
212: $this->out("\t$field2: $value2");
213: }
214:
215: $this->out("");
216: } else {
217: $this->out("\t$field: $value");
218: }
219: }
220: }
221: } else {
222: $this->out($idx);
223:
224: foreach ($results as $field => $value) {
225: if (is_array($value)) {
226: foreach ($value as $field2 => $value2) {
227: $this->out("\t$field2: $value2");
228: }
229:
230: $this->out("");
231: } else {
232: $this->out("\t$field: $value");
233: }
234: }
235: }
236: }
237: } else {
238: $this->out("\nNo result set found");
239: }
240: } else {
241: $this->out("$modelToCheck is not a valid model");
242: }
243:
244: break;
245: case (strpos($command, '->save') > 0):
246:
247: $command = strip_tags($command);
248: $command = str_replace($this->badCommandChars, "", $command);
249: list($modelToSave, $tmp) = explode("->", $command);
250:
251: if ($this->__isValidModel($modelToSave)) {
252:
253: list($foo, $data) = explode("->save", $command);
254: $data = preg_replace('/^\(*(array)?\(*(.+?)\)*$/i', '\\2', $data);
255: $saveCommand = "\$this->{$modelToSave}->save(array('{$modelToSave}' => array({$data})));";
256: @eval($saveCommand);
257: $this->out('Saved record for ' . $modelToSave);
258: }
259: break;
260: case (preg_match("/^(\w+) columns/", $command, $tmp) == true):
261: $modelToCheck = strip_tags(str_replace($this->badCommandChars, "", $tmp[1]));
262:
263: if ($this->__isValidModel($modelToCheck)) {
264:
265: $fieldsCommand = "\$data = \$this->{$modelToCheck}->getColumnTypes();";
266: @eval($fieldsCommand);
267:
268: if (is_array($data)) {
269: foreach ($data as $field => $type) {
270: $this->out("\t{$field}: {$type}");
271: }
272: }
273: } else {
274: $this->out("Please verify that you selected a valid model");
275: }
276: break;
277: case (preg_match("/^routes\s+reload/i", $command, $tmp) == true):
278: $router =& Router::getInstance();
279: if (!$this->__loadRoutes()) {
280: $this->out("There was an error loading the routes config. Please check that the file");
281: $this->out("exists and is free of parse errors.");
282: break;
283: }
284: $this->out("Routes configuration reloaded, " . count($router->routes) . " routes connected");
285: break;
286: case (preg_match("/^routes\s+show/i", $command, $tmp) == true):
287: $router =& Router::getInstance();
288: $this->out(implode("\n", Set::extract($router->routes, '{n}.0')));
289: break;
290: case (preg_match("/^route\s+(.*)/i", $command, $tmp) == true):
291: $this->out(var_export(Router::parse($tmp[1]), true));
292: break;
293: default:
294: $this->out("Invalid command\n");
295: break;
296: }
297: $command = '';
298: }
299: }
300: 301: 302: 303: 304: 305: 306:
307: function __isValidModel($modelToCheck) {
308: return in_array($modelToCheck, $this->models);
309: }
310: 311: 312: 313: 314: 315: 316:
317: function __loadRoutes() {
318: $router =& Router::getInstance();
319:
320: $router->reload();
321: extract($router->getNamedExpressions());
322:
323: if (!@include(CONFIGS . 'routes.php')) {
324: return false;
325: }
326: $router->parse('/');
327:
328: foreach (array_keys($router->getNamedExpressions()) as $var) {
329: unset(${$var});
330: }
331: for ($i = 0; $i < count($router->routes); $i++) {
332: $router->compile($i);
333: }
334: return true;
335: }
336: }
337: ?>