0525a37634819c231ab7c769ad6a87e2a056550e
[bdrem.git] / src / bdrem / Cli.php
1 <?php
2 /**
3  * Part of bdrem
4  *
5  * PHP version 5
6  *
7  * @category  Tools
8  * @package   Bdrem
9  * @author    Christian Weiske <cweiske@cweiske.de>
10  * @copyright 2014 Christian Weiske
11  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
12  * @link      http://cweiske.de/bdrem.htm
13  */
14 namespace bdrem;
15
16 /**
17  * Command line user interface for the terminal/shell.
18  * Renders an ASCII table by default.
19  *
20  * @category  Tools
21  * @package   Bdrem
22  * @author    Christian Weiske <cweiske@cweiske.de>
23  * @copyright 2014 Christian Weiske
24  * @license   http://www.gnu.org/licenses/agpl.html GNU AGPL v3
25  * @version   Release: @package_version@
26  * @link      http://cweiske.de/bdrem.htm
27  */
28 class Cli extends UserInterface
29 {
30     /**
31      * Load parameters for the CLI option parser.
32      * Set the default renderer to "console" and adds some CLI-only commands
33      * like "readme" and "config".
34      *
35      * @return \Console_CommandLine CLI option parser
36      */
37     protected function loadParameters()
38     {
39         $parser = parent::loadParameters();
40         //set default renderer to console
41         $parser->options['renderer']->default = 'console';
42
43         //only on CLI
44         $parser->addCommand(
45             'readme', array(
46                 'description' => 'Show README.rst file'
47             )
48         );
49         $parser->addCommand(
50             'config', array(
51                 'description' => 'Extract configuration file'
52             )
53         );
54
55         return $parser;
56     }
57
58     /**
59      * Handle any commands given on the CLI
60      *
61      * @param object $res Command line parameters and options
62      *
63      * @return void
64      */
65     protected function handleCommands($res)
66     {
67         if ($res->command_name == '') {
68             return;
69         } else if ($res->command_name == 'readme') {
70             $this->showReadme();
71         } else if ($res->command_name == 'config') {
72             $this->extractConfig();
73         } else {
74             throw new \Exception('Unknown command');
75         }
76     }
77
78     /**
79      * Handle the "readme" command and output the readme.
80      *
81      * @return void
82      */
83     protected function showReadme()
84     {
85         readfile(__DIR__ . '/../../README.rst');
86         exit();
87     }
88
89     /**
90      * Handle the "config" command and output the default configuration
91      *
92      * @return void
93      */
94     protected function extractConfig()
95     {
96         readfile(__DIR__ . '/../../data/bdrem.config.php.dist');
97         exit();
98     }
99 }
100 ?>