4 abstract class UserInterface
11 $this->config = new Config();
12 $parser = $this->loadParameters();
13 $res = $this->parseParameters($parser);
15 $this->config->load();
16 if (!$this->config->cfgFileExists) {
18 "No config file found. Looked at the following places:\n"
19 . '- ' . implode ("\n- ", $this->config->cfgFiles)
23 setlocale(LC_TIME, $this->config->locale);
24 $this->handleCommands($res);
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 'Error: ' . $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',
61 'long_name' => '--previous',
62 'description' => 'Show NUM days before date',
64 'action' => 'StoreInt',
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 'short_name' => '-c',
108 'long_name' => '--config',
109 'help_name' => 'FILE',
110 'description' => 'Path to configuration file',
111 'action' => 'StoreString'
118 protected function parseParameters($parser)
121 $result = $parser->parse();
123 if ($result->options['configfile'] !== null) {
124 $this->config->cfgFiles = array($result->options['configfile']);
127 $this->config->daysNext = $result->options['daysNext'];
128 $this->config->daysPrev = $result->options['daysPrev'];
129 $this->config->renderer = $result->options['renderer'];
130 $this->config->stopOnEmpty = $result->options['stopOnEmpty'];
131 $this->config->setDate($result->options['date']);
133 } catch (\Exception $exc) {
134 $this->preRenderParameterError();
135 $parser->displayError($exc->getMessage());
139 protected function render($arEvents)
141 $r = $this->getRenderer();
142 $r->config = $this->config;
144 if ($this->config->stopOnEmpty && count($arEvents) == 0) {
145 $r->handleStopOnEmpty();
148 $r->renderAndOutput($arEvents);
151 protected function getRenderer()
153 $renderer = ucfirst($this->config->renderer);
154 if ($renderer == 'Htmltable') {
155 $renderer = 'HtmlTable';
157 $class = '\\bdrem\\Renderer_' . $renderer;
161 protected function handleCommands($res)
165 protected function preRenderParameterError()