4 abstract class UserInterface
11 $this->config = new Config();
12 $this->config->load();
13 setlocale(LC_TIME, $this->config->locale);
15 $parser = $this->loadParameters();
16 $res = $this->parseParameters($parser);
17 $this->handleCommands($res);
19 if (!$this->config->cfgFileExists) {
20 throw new \Exception('No config file found');
23 $source = $this->config->loadSource();
24 $arEvents = $source->getEvents(
26 $this->config->daysPrev, $this->config->daysNext
28 usort($arEvents, '\\bdrem\\Event::compare');
29 $this->render($arEvents);
30 } catch (\Exception $e) {
31 $this->preRenderParameterError();
32 echo 'Exception: ' . $e->getCode() . ' - ' . $e->getMessage() . "\n";
37 protected function loadParameters()
39 $parser = new \Console_CommandLine();
40 $parser->description = 'Birthday reminder';
41 $parser->version = '0.1.0';
47 'long_name' => '--days-next',
48 'description' => 'Show NUM days after date',
50 'action' => 'StoreInt',
51 'default' => $this->config->daysNext,
58 'long_name' => '--previous',
59 'description' => 'Show NUM days before date',
61 'action' => 'StoreInt',
62 'default' => $this->config->daysPrev,
69 'long_name' => '--renderer',
70 'description' => 'Output mode',
71 'action' => 'StoreString',
78 'default' => 'console',
79 'add_list_option' => true,
86 'long_name' => '--stoponempty',
87 'description' => 'Output nothing when list is empty',
88 'action' => 'StoreTrue',
96 'long_name' => '--date',
97 'description' => 'Date to show events for',
98 'action' => 'StoreString'
104 protected function parseParameters($parser)
107 $result = $parser->parse();
108 // do something with the result object
109 $this->config->daysNext = $result->options['daysNext'];
110 $this->config->daysPrev = $result->options['daysPrev'];
111 $this->config->renderer = $result->options['renderer'];
112 $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
113 $this->config->setDate($result->options['date']);
115 } catch (\Exception $exc) {
116 $this->preRenderParameterError();
117 $parser->displayError($exc->getMessage());
121 protected function render($arEvents)
123 $r = $this->getRenderer();
124 $r->config = $this->config;
126 if ($this->config->stopOnEmpty && count($arEvents) == 0) {
127 $r->handleStopOnEmpty();
130 $r->renderAndOutput($arEvents);
133 protected function getRenderer()
135 $renderer = ucfirst($this->config->renderer);
136 if ($renderer == 'Htmltable') {
137 $renderer = 'HtmlTable';
139 $class = '\\bdrem\\Renderer_' . $renderer;
143 protected function handleCommands($res)
147 protected function preRenderParameterError()