1: <?php
2:
3: namespace cli_db;
4:
5: require_once SHARED . 'classes/CLI_Command.php';
6:
7: interface Table {
8:
9: 10: 11: 12: 13: 14: 15: 16:
17: static function getKeys();
18:
19: 20: 21:
22: static function getSubCommands();
23:
24: 25: 26:
27: static function getPropelClass();
28: }
29:
30: 31: 32: 33:
34: abstract class AbstractTable implements \CLI_Command, Table {
35:
36: 37: 38: 39: 40: 41:
42: private static function processSubCommand($command, $subcommand_name, $keys) {
43:
44: $submcd = $command->addCommand($subcommand_name);
45:
46:
47: $keys = array_merge(array(
48: 'short' => array(
49: 'actions' => array(
50: 'insert' => 'optional',
51: ),
52: 'description' => 'if set, will just output the ID of newly inserted line on success',
53: 'action' => 'StoreTrue'
54: ),
55: 'noconfirm' => array(
56: 'actions' => array(
57: 'delete' => 'optional',
58: ),
59: 'description' => 'if set, will not ask for confirmation on delete',
60: 'action' => 'StoreTrue'
61: ),
62: ), $keys);
63:
64:
65: foreach ($keys as $key => $data) {
66:
67: if (isset($data['actions'][$subcommand_name]) && ($data['actions'][$subcommand_name] == 'optional' || $data['actions'][$subcommand_name] == 'required')) {
68:
69: $stdopts = array(
70: 'long_name' => '--' . $key,
71: 'help_name' => $key
72: );
73:
74: $extraopts = array_diff_key($data, array('actions' => null, 'colname' => null));
75: $options = array_merge($stdopts, $extraopts);
76:
77: $options['description'] = sprintf('(%2$s) %1$s', $data['description'], $data['actions'][$subcommand_name]);
78:
79: $option = $submcd->addOption($key, $options);
80: }
81: }
82: }
83:
84: 85: 86: 87: 88:
89: public static function CLI_getCommand(\Console_CommandLine $parser) {
90:
91: $command = $parser->addCommand(call_user_func(array(get_called_class(), 'CLI_commandName')), array(
92: 'description' => call_user_func(array(get_called_class(), 'CLI_commandDescription'))
93: ));
94:
95:
96: $keys = call_user_func(array(get_called_class(), 'getKeys'));
97:
98: $subcommands = call_user_func(array(get_called_class(), 'getSubCommands'));
99:
100:
101: foreach ($subcommands as $cmd) {
102: self::processSubCommand($command, $cmd, $keys);
103: }
104:
105: return $command;
106: }
107:
108: 109: 110: 111: 112: 113:
114: public static function CLI_checkRequiredOpts(\Console_CommandLine_Result $command) {
115:
116: if (!is_object($command->command))
117: return;
118:
119: $subcommand_name = $command->command_name;
120: $subcommand_options = $command->command->options;
121:
122:
123: $keys = call_user_func(array(get_called_class(), 'getKeys'));
124:
125: foreach ($keys as $key => $data) {
126: if (isset($data['actions'][$subcommand_name]) && $data['actions'][$subcommand_name] == 'required')
127: if (!isset($subcommand_options[$key]))
128: throw new \Exception(sprintf('option --%s has to be set', $key));
129: }
130: }
131:
132: 133: 134: 135: 136: 137:
138: public static function CLI_execute(\Console_CommandLine_Result $command, \Console_CommandLine $parser) {
139:
140: if (!is_object($command->command))
141:
142: $parser->commands[call_user_func(array(get_called_class(), 'CLI_commandName'))]->displayUsage();
143:
144: $subcommand_name = $command->command_name;
145: $subcommand_options = $command->command->options;
146:
147: $keys = call_user_func(array(get_called_class(), 'getKeys'));
148: $subcommands = call_user_func(array(get_called_class(), 'getSubCommands'));
149:
150: if (!in_array($subcommand_name, $subcommands))
151: return false;
152:
153:
154: call_user_func(array(get_called_class(), 'command_' . $subcommand_name), $subcommand_options, $keys);
155: }
156:
157: 158: 159: 160: 161:
162: public static function prepareQueryResult($res) {
163: $keys = call_user_func(array(get_called_class(), 'getKeys'));
164:
165: $column_keys = array();
166: foreach ($keys as $key => $val) {
167: if (isset($val['colname']) && $val['colname'] != null)
168: $column_keys[$key] = $val['colname'];
169: }
170:
171:
172: $ret = array();
173: foreach ($res as $row) {
174: $ret_row = array();
175:
176: foreach ($column_keys as $key => $val)
177:
178: $ret_row[$key] = call_user_func(array($row, "get" . $val));
179: $ret[] = $ret_row;
180: }
181: return $ret;
182: }
183:
184: 185: 186: 187: 188:
189: public static function printTable($headers, $data) {
190: $tbl = new \Console_Table();
191: $tbl->setHeaders($headers);
192: $tbl->addData($data);
193: echo $tbl->getTable();
194: }
195:
196:
197: 198: 199: 200: 201:
202: protected static function command_insert_set_defaults(\BaseObject $item) {
203:
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: protected static function setKeys($options, $keys, $cmdname, \BaseObject $propelitem) {
214: foreach ($keys as $key => $data) {
215: if (!isset($data['colname']) || !isset($data['actions']) || !isset($data['actions'][$cmdname]))
216: continue;
217: if ($data['actions'][$cmdname] == 'required' || $data['actions'][$cmdname] == 'internal')
218: $propelitem->{"set" . $data['colname']}($options[$key]);
219: else if ($data['actions'][$cmdname] == 'optional' && isset($options[$key]))
220: $propelitem->{"set" . $data['colname']}($options[$key]);
221: }
222: }
223:
224: 225: 226: 227: 228:
229: protected static function command_insert($options, $keys) {
230: $propel_class = call_user_func(array(get_called_class(), 'getPropelClass'));
231: $item = new $propel_class();
232: self::setKeys($options, $keys, 'insert', $item);
233:
234: call_user_func(array(get_called_class(), 'command_insert_set_defaults'), $item);
235:
236: $lines = $item->save();
237: if (isset($options['short']) && $options['short'])
238: print $item->getPrimaryKey();
239: else {
240: printf("%d line(s) inserted.\nNew item ID is %d.\n", $lines, $item->getPrimaryKey());
241: }
242: }
243:
244: 245: 246: 247: 248:
249: protected static function command_update($options, $keys) {
250: $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
251: $q = new $propel_class;
252:
253: $item = $q->findOneBy($keys['id']['colname'], $options['id']);
254: if ($item == null) {
255: trigger_error(sprintf("No contact found for id %d.\n", $options['id']), E_USER_ERROR);
256: }
257:
258: foreach ($keys as $key => $data) {
259: if ($key != 'id' && isset($data['colname']) && isset($options[$key]))
260: $item->{"set" . $data['colname']}($options[$key]);
261: }
262:
263: $lines = $item->save();
264: printf("%d line(s) udpated.\n", $lines);
265: }
266:
267: 268: 269: 270: 271: 272:
273: public static function command_delete_confirm($options, $message = "This will delete a row from the database.\n") {
274: if (isset($options['noconfirm']) && $options['noconfirm'])
275: return true;
276:
277: echo $message;
278: echo "Coninue (yes/no)\n> ";
279: while (!in_array($line = trim(fgets(STDIN)), array('yes', 'no'))) {
280:
281: echo "enter one of (yes/no):\n> ";
282: }
283: return $line == 'yes';
284: }
285:
286: 287: 288: 289: 290:
291: protected static function command_delete($options, $keys) {
292:
293: $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
294: $q = new $propel_class;
295:
296: $item = $q->findOneBy($keys['id']['colname'], $options['id']);
297:
298: $cmdname = call_user_func(array(get_called_class(), 'CLI_commandName'));
299:
300: if ($item == null) {
301: trigger_error(sprintf("No $cmdname found for id %d.\n", $options['id']), E_USER_ERROR);
302: }
303: if (self::command_delete_confirm($options)) {
304: $item->delete();
305: printf("$cmdname with id %d deleted successfully.\n", $options['id']);
306: }
307: }
308:
309: 310: 311: 312: 313:
314: protected static function command_details($options, $keys) {
315: $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
316: $q = new $propel_class;
317:
318: $item = $q->findOneBy($keys['id']['colname'], $options['id']);
319: if ($item == null) {
320: $cmdname = call_user_func(array(get_called_class(), 'CLI_commandName'));
321: trigger_error(sprintf("No $cmdname found for id %d.\n", $options['id']), E_USER_ERROR);
322: }
323:
324: $table_keys = array_keys(array_filter($keys, function($val) {
325: return isset($val['colname']);
326: }));
327: $results = self::prepareQueryResult(array($item));
328: self::printTable($table_keys, $results);
329: }
330:
331: 332: 333: 334: 335:
336: protected static function command_list($options, $keys) {
337:
338: $propel_class = call_user_func(array(get_called_class(), 'getPropelClass')) . 'Query';
339: $q = new $propel_class;
340:
341: $table_keys = array_keys(array_filter($keys, function($val) {
342: return isset($val['colname']);
343: }));
344: $results = self::prepareQueryResult($q->find());
345: self::printTable($table_keys, $results);
346: }
347:
348:
349: }
350:
351: ?>
352: