add cli option to activate debug log
[auerswald-callnotifier.git] / src / callnotifier / CLI.php
1 <?php
2 namespace callnotifier;
3
4
5 class CLI
6 {
7     protected $cliParser;
8     protected $config;
9
10     public function __construct()
11     {
12         $this->setupCli();
13     }
14
15     public function run()
16     {
17         $this->config = new Config();
18         try {
19             $result = $this->cliParser->parse();
20         } catch (\Exception $exc) {
21             $this->cliParser->displayError($exc->getMessage());
22         }
23
24         $this->fillConfig($this->config, $result);
25
26         $handler = new MessageHandler($this->config);
27         if ($result->options['debug'] || $result->options['debugEdss1']) {
28             $debugLogger = new Logger_Debug();
29             $handler->addLogger($debugLogger, '*');
30             if ($result->options['debugEdss1']) {
31                 $debugLogger->edss1MsgOnly = true;
32             }
33         }
34
35         if ($this->config->replayFile !== null) {
36             $sourceClass = 'callnotifier\Source_File';
37         } else {
38             $sourceClass = 'callnotifier\Source_Remote';
39         }
40         $source = new $sourceClass($this->config, $handler);
41         $source->run();
42     }
43
44     public function setupCli()
45     {
46         $p = new \Console_CommandLine();
47         $p->description = 'Notifies about incoming calls on an Auerswald COMpact 3000';
48         $p->version = '0.0.1';
49
50         $p->addOption(
51             'host',
52             array(
53                 'short_name'  => '-h',
54                 'long_name'   => '--host',
55                 'description' => 'IP of COMpact 3000',
56                 'help_name'   => 'IP',
57                 'action'      => 'StoreString'
58             )
59         );
60
61         $p->addOption(
62             'dumpFile',
63             array(
64                 'long_name'   => '--dump',
65                 'description' => 'dump messages into file for later replay',
66                 'help_name'   => 'FILE',
67                 'action'      => 'StoreString'
68             )
69         );
70         $p->addOption(
71             'replayFile',
72             array(
73                 'long_name'   => '--replay',
74                 'description' => "Replay messages from file instead from network",
75                 'help_name'   => 'FILE',
76                 'action'      => 'StoreString'
77             )
78         );
79
80         $p->addOption(
81             'debug',
82             array(
83                 'short_name'  => '-d',
84                 'long_name'   => '--debug',
85                 'description' => "Debug mode: Echo all received messages and events",
86                 'action'      => 'StoreTrue'
87             )
88         );
89         $p->addOption(
90             'debugEdss1',
91             array(
92                 'short_name'  => '-e',
93                 'long_name'   => '--debug-edss1',
94                 'description' => "Debug mode: Show EDSS1 messages only",
95                 'action'      => 'StoreTrue'
96             )
97         );
98
99         $this->cliParser = $p;
100     }
101
102     protected function fillConfig($config, $result)
103     {
104         $config->setIfNotEmpty('host', $result->options['host']);
105         $config->setIfNotEmpty('dumpFile', $result->options['dumpFile']);
106         $config->setIfNotEmpty('replayFile', $result->options['replayFile']);
107     }
108 }
109
110 ?>