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) {
21 "No config file found. Looked at the following places:\n"
22 . '- ' . implode ("\n- ", $this->config->cfgFiles)
26 $source = $this->config->loadSource();
27 $arEvents = $source->getEvents(
29 $this->config->daysPrev, $this->config->daysNext
31 usort($arEvents, '\\bdrem\\Event::compare');
32 $this->render($arEvents);
33 } catch (\Exception $e) {
34 $this->preRenderParameterError();
35 echo 'Exception: ' . $e->getCode() . ' - ' . $e->getMessage() . "\n";
40 protected function loadParameters()
42 $parser = new \Console_CommandLine();
43 $parser->description = 'Birthday reminder';
44 $parser->version = '0.1.0';
50 'long_name' => '--days-next',
51 'description' => 'Show NUM days after date',
53 'action' => 'StoreInt',
54 'default' => $this->config->daysNext,
61 'long_name' => '--previous',
62 'description' => 'Show NUM days before date',
64 'action' => 'StoreInt',
65 'default' => $this->config->daysPrev,
72 'long_name' => '--renderer',
73 'description' => 'Output mode',
74 'action' => 'StoreString',
81 'default' => 'console',
82 'add_list_option' => true,
89 'long_name' => '--stoponempty',
90 'description' => 'Output nothing when list is empty',
91 'action' => 'StoreTrue',
99 'long_name' => '--date',
100 'description' => 'Date to show events for',
101 'action' => 'StoreString'
107 protected function parseParameters($parser)
110 $result = $parser->parse();
111 // do something with the result object
112 $this->config->daysNext = $result->options['daysNext'];
113 $this->config->daysPrev = $result->options['daysPrev'];
114 $this->config->renderer = $result->options['renderer'];
115 $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
116 $this->config->setDate($result->options['date']);
118 } catch (\Exception $exc) {
119 $this->preRenderParameterError();
120 $parser->displayError($exc->getMessage());
124 protected function render($arEvents)
126 $r = $this->getRenderer();
127 $r->config = $this->config;
129 if ($this->config->stopOnEmpty && count($arEvents) == 0) {
130 $r->handleStopOnEmpty();
133 $r->renderAndOutput($arEvents);
136 protected function getRenderer()
138 $renderer = ucfirst($this->config->renderer);
139 if ($renderer == 'Htmltable') {
140 $renderer = 'HtmlTable';
142 $class = '\\bdrem\\Renderer_' . $renderer;
146 protected function handleCommands($res)
150 protected function preRenderParameterError()