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 $this->parseParameters($parser);
18 if (!$this->config->cfgFileExists) {
19 throw new \Exception('No config file found');
22 $source = $this->config->loadSource();
23 $arEvents = $source->getEvents(
25 $this->config->daysPrev, $this->config->daysNext
27 usort($arEvents, '\\bdrem\\Event::compare');
28 $this->render($arEvents);
29 } catch (\Exception $e) {
30 $this->preRenderParameterError();
31 echo 'Exception: ' . $e->getCode() . ' - ' . $e->getMessage() . "\n";
36 protected function loadParameters()
38 $parser = new \Console_CommandLine();
39 $parser->description = 'Birthday reminder';
40 $parser->version = '0.1.0';
46 'long_name' => '--days-next',
47 'description' => 'Show NUM days after date',
49 'action' => 'StoreInt',
50 'default' => $this->config->daysNext,
57 'long_name' => '--previous',
58 'description' => 'Show NUM days before date',
60 'action' => 'StoreInt',
61 'default' => $this->config->daysPrev,
68 'long_name' => '--renderer',
69 'description' => 'Output mode',
70 'action' => 'StoreString',
77 'default' => 'console',
78 'add_list_option' => true,
85 'long_name' => '--stoponempty',
86 'description' => 'Output nothing when list is empty',
87 'action' => 'StoreTrue',
95 'long_name' => '--date',
96 'description' => 'Date to show events for',
97 'action' => 'StoreString'
103 protected function parseParameters($parser)
106 $result = $parser->parse();
107 // do something with the result object
108 $this->config->daysNext = $result->options['daysNext'];
109 $this->config->daysPrev = $result->options['daysPrev'];
110 $this->config->renderer = $result->options['renderer'];
111 $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
112 $this->config->setDate($result->options['date']);
113 } catch (\Exception $exc) {
114 $this->preRenderParameterError();
115 $parser->displayError($exc->getMessage());
119 protected function render($arEvents)
121 $r = $this->getRenderer();
122 $r->config = $this->config;
124 if ($this->config->stopOnEmpty && count($arEvents) == 0) {
125 $r->handleStopOnEmpty();
128 $r->renderAndOutput($arEvents);
131 protected function getRenderer()
133 $renderer = ucfirst($this->config->renderer);
134 if ($renderer == 'Htmltable') {
135 $renderer = 'HtmlTable';
137 $class = '\\bdrem\\Renderer_' . $renderer;
141 protected function preRenderParameterError()