add option to specify config file
[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             $parser = $this->loadParameters();
13             $res = $this->parseParameters($parser);
14
15             $this->config->load();
16             if (!$this->config->cfgFileExists) {
17                 throw new \Exception(
18                     "No config file found. Looked at the following places:\n"
19                     . '- ' . implode ("\n- ", $this->config->cfgFiles)
20                 );
21             }
22
23             setlocale(LC_TIME, $this->config->locale);
24             $this->handleCommands($res);
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 'Error: ' . $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'     => null,
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'     => null,
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         $parser->addOption(
105             'configfile',
106             array(
107                 'short_name'  => '-c',
108                 'long_name'   => '--config',
109                 'help_name'   => 'FILE',
110                 'description' => 'Path to configuration file',
111                 'action'      => 'StoreString'
112             )
113         );
114
115         return $parser;
116     }
117
118     protected function parseParameters($parser)
119     {
120         try {
121             $result = $parser->parse();
122
123             if ($result->options['configfile'] !== null) {
124                 $this->config->cfgFiles = array($result->options['configfile']);
125             }
126
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']);
132             return $result;
133         } catch (\Exception $exc) {
134             $this->preRenderParameterError();
135             $parser->displayError($exc->getMessage());
136         }
137     }
138
139     protected function render($arEvents)
140     {
141         $r = $this->getRenderer();
142         $r->config = $this->config;
143
144         if ($this->config->stopOnEmpty && count($arEvents) == 0) {
145             $r->handleStopOnEmpty();
146             return;
147         }
148         $r->renderAndOutput($arEvents);
149     }
150
151     protected function getRenderer()
152     {
153         $renderer = ucfirst($this->config->renderer);
154         if ($renderer == 'Htmltable') {
155             $renderer = 'HtmlTable';
156         }
157         $class = '\\bdrem\\Renderer_' . $renderer;
158         return new $class();
159     }
160
161     protected function handleCommands($res)
162     {
163     }
164
165     protected function preRenderParameterError()
166     {
167     }
168 }
169 ?>