1<?php
2/**
3 * VerbosePrint.php VerbosePrint Extension
4 *
5 * PHP version 5
6 *
7 * @category  Extension
8 * @package   PHP_Shell
9 * @author    Jan Kneschke <jan@kneschke.de>
10 * @copyright 2006 Jan Kneschke
11 * @license   MIT <http://www.opensource.org/licenses/mit-license.php>
12 * @version   SVN: $Id$
13 * @link      http://pear.php.net/package/PHP_Shell
14 */
15
16/**
17 * PHP_Shell_Extensions_VerbosePrint
18 *
19 * @category  Extension
20 * @package   PHP_Shell
21 * @author    Jan Kneschke <jan@kneschke.de>
22 * @copyright 2006 Jan Kneschke
23 * @license   MIT <http://www.opensource.org/licenses/mit-license.php>
24 * @version   Release: @package_version@
25 * @link      http://pear.php.net/package/PHP_Shell
26 */
27class PHP_Shell_Extensions_VerbosePrint implements PHP_Shell_Extension
28{
29    protected $opt_verbose = false;
30    protected $oneshot_verbose = false;
31
32    /**
33     * Register a command
34     *
35     * @return void
36     */
37    public function register()
38    {
39        $cmd = PHP_Shell_Commands::getInstance();
40        $cmd->registerCommand(
41            '#^p #',
42            $this,
43            'cmdPrint',
44            'p <var>',
45            'print the variable verbosly'
46        );
47
48        $opt = PHP_Shell_Options::getInstance();
49        $opt->registerOption('verboseprint', $this, 'optSetVerbose');
50
51    }
52
53    /**
54     * handle the 'p ' command
55     *
56     * set the verbose flag
57     *
58     * @param string $l Line
59     *
60     * @return string the pure command-string without the 'p ' command
61     */
62    public function cmdPrint($l)
63    {
64        $this->oneshot_verbose = true;
65
66        $cmd = substr($l, 2);
67
68        return $cmd;
69    }
70
71    /**
72     * Set verbose
73     *
74     * @param string $key   Unused
75     * @param string $value One of 'false', 'on', etc
76     *
77     * @return void
78     */
79    public function optSetVerbose($key, $value)
80    {
81        switch($value) {
82        case "true":
83        case "on":
84        case "1":
85            $this->opt_verbose = true;
86            break;
87        default:
88            $this->opt_verbose = false;
89            break;
90        }
91    }
92
93    /**
94     * check if we have a verbose print-out
95     *
96     * @return bool 1 if verbose, 0 otherwise
97     */
98    public function isVerbose()
99    {
100        $v = $this->opt_verbose || $this->oneshot_verbose;
101
102        $this->oneshot_verbose = false;
103
104        return $v;
105    }
106}
107
108
109