load config file from different locations, including phar
[bdrem.git] / src / bdrem / UserInterface.php
1 <?php
2 namespace bdrem;
3
4 abstract class UserInterface
5 {
6     protected $config;
7
8     public function run()
9     {
10         try {
11             $this->config = new Config();
12             $this->config->load();
13             setlocale(LC_TIME, $this->config->locale);
14
15             $parser = $this->loadParameters();
16             $res = $this->parseParameters($parser);
17             $this->handleCommands($res);
18
19             if (!$this->config->cfgFileExists) {
20                 throw new \Exception(
21                     "No config file found. Looked at the following places:\n"
22                     . '- ' . implode ("\n- ", $this->config->cfgFiles)
23                 );
24             }
25
26             $source = $this->config->loadSource();
27             $arEvents = $source->getEvents(
28                 $this->config->date,
29                 $this->config->daysPrev, $this->config->daysNext
30             );
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";
36             exit(1);
37         }
38     }
39
40     protected function loadParameters()
41     {
42         $parser = new \Console_CommandLine();
43         $parser->description = 'Birthday reminder';
44         $parser->version = '0.1.0';
45
46         $parser->addOption(
47             'daysNext',
48             array(
49                 'short_name'  => '-n',
50                 'long_name'   => '--days-next',
51                 'description' => 'Show NUM days after date',
52                 'help_name'   => 'NUM',
53                 'action'      => 'StoreInt',
54                 'default'     => $this->config->daysNext,
55             )
56         );
57         $parser->addOption(
58             'daysPrev',
59             array(
60                 'short_name'  => '-p',
61                 'long_name'   => '--previous',
62                 'description' => 'Show NUM days before date',
63                 'help_name'   => 'NUM',
64                 'action'      => 'StoreInt',
65                 'default'     => $this->config->daysPrev,
66             )
67         );
68         $parser->addOption(
69             'renderer',
70             array(
71                 'short_name'  => '-r',
72                 'long_name'   => '--renderer',
73                 'description' => 'Output mode',
74                 'action'      => 'StoreString',
75                 'choices'     => array(
76                     'console',
77                     'html',
78                     'htmltable',
79                     'mail',
80                 ),
81                 'default'     => 'console',
82                 'add_list_option' => true,
83             )
84         );
85         $parser->addOption(
86             'stopOnEmpty',
87             array(
88                 'short_name'  => '-e',
89                 'long_name'   => '--stoponempty',
90                 'description' => 'Output nothing when list is empty',
91                 'action'      => 'StoreTrue',
92                 'default'     => false
93             )
94         );
95         $parser->addOption(
96             'date',
97             array(
98                 'short_name'  => '-d',
99                 'long_name'   => '--date',
100                 'description' => 'Date to show events for',
101                 'action'      => 'StoreString'
102             )
103         );
104         return $parser;
105     }
106
107     protected function parseParameters($parser)
108     {
109         try {
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']);
117             return $result;
118         } catch (\Exception $exc) {
119             $this->preRenderParameterError();
120             $parser->displayError($exc->getMessage());
121         }
122     }
123
124     protected function render($arEvents)
125     {
126         $r = $this->getRenderer();
127         $r->config = $this->config;
128
129         if ($this->config->stopOnEmpty && count($arEvents) == 0) {
130             $r->handleStopOnEmpty();
131             return;
132         }
133         $r->renderAndOutput($arEvents);
134     }
135
136     protected function getRenderer()
137     {
138         $renderer = ucfirst($this->config->renderer);
139         if ($renderer == 'Htmltable') {
140             $renderer = 'HtmlTable';
141         }
142         $class = '\\bdrem\\Renderer_' . $renderer;
143         return new $class();
144     }
145
146     protected function handleCommands($res)
147     {
148     }
149
150     protected function preRenderParameterError()
151     {
152     }
153 }
154 ?>