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: class Horde_Kolab_Cli
30: {
31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: static public function main(array $parameters = array())
42: {
43: $modular = self::_prepareModular($parameters);
44: if (empty($parameters['output'])) {
45: if (!class_exists('Horde_Cli')) {
46: throw new Horde_Kolab_Cli_Exception('The Horde_Cli package seems to be missing (Class Horde_Cli is missing)!');
47: }
48: $cli = Horde_Cli::init();
49: } else {
50: $cli = $parameters['output'];
51: }
52: $parser = $modular->createParser();
53: list($options, $arguments) = $parser->parseArgs();
54: if (count($arguments) == 0) {
55: $parser->printHelp();
56: } else {
57: try {
58: if (!empty($options['config'])) {
59: if (!file_exists($options['config'])) {
60: throw new Horde_Kolab_Cli_Exception(
61: sprintf(
62: 'The specified config file %s does not exist!',
63: $options['config']
64: )
65: );
66: }
67: global $conf;
68: include $options['config'];
69: foreach ($conf as $key => $value) {
70: $options->ensureValue($key, $value);
71: }
72: }
73: if (empty($options['host'])) {
74: $options['host'] = 'localhost';
75: }
76: if (empty($options['driver'])) {
77: $options['driver'] = 'horde';
78: }
79: $world = array();
80: foreach ($modular->getModules() as $module) {
81: $modular->getProvider()
82: ->getModule($module)
83: ->handleArguments($options, $arguments, $world);
84: }
85: if (!empty($options['timed'])
86: && class_exists('Horde_Support_Timer')) {
87: $timer = new Horde_Support_Timer();
88: $timer->push();
89: } else {
90: $timer = false;
91: }
92: $modular->getProvider()
93: ->getModule(ucfirst($arguments[0]))
94: ->run($cli, $options, $arguments, $world);
95: if (!empty($options['timed'])) {
96: if ($timer) {
97: $cli->message(floor($timer->pop() * 1000) . ' ms');
98: } else {
99: $cli->message('The class Horde_Support_Timer seems to be missing!');
100: }
101: }
102: } catch (Horde_Cli_Modular_Exception $e) {
103: $parser->printHelp();
104: }
105: }
106: }
107:
108: static private function _prepareModular(array $parameters = array())
109: {
110: return new Horde_Cli_Modular(
111: array(
112: 'parser' => array(
113: 'class' => empty($parameters['parser']['class']) ? 'Horde_Argv_Parser' : $parameters['parser']['class'],
114: 'usage' => Horde_Kolab_Cli_Translation::t(
115: "[options] MODULE ACTION\n\nPossible MODULEs and ACTIONs:\n\n"
116: )
117: ),
118: 'modules' => array(
119: 'directory' => dirname(__FILE__) . '/Cli/Module',
120: ),
121: 'provider' => array(
122: 'prefix' => 'Horde_Kolab_Cli_Module_'
123: )
124: )
125: );
126: }
127: }