summaryrefslogtreecommitdiff
path: root/framework/3rdParty/PhpShell/PHP/Shell/Extensions.php
blob: 9b210c47c3b7d46e871e46c56668168d9def7209 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

/**
* the interface for all shell extensions 
*
* Extension can hook into the execution of the shell
*
* examples:
* - execution time for parsing and execute
* - colours for the output
* - inline help
*
*  
*/
interface PHP_Shell_Extension {
    public function register();
}

/**
* storage class for Shell Extensions
*
* 
*/
class PHP_Shell_Extensions {
    /**
    * @var PHP_Shell_Extensions
    */
    static protected $instance;

    /**
    * storage for the extension
    *
    * @var array
    */
    protected $exts = array();

    /**
    * the extension object gives access to the register objects
    * through the a simple $exts->name->...
    *
    * @param string registered name of the extension 
    * @return PHP_Shell_Extension object handle
    */
    public function __get($key) {
        if (!isset($this->exts[$key])) {
            throw new Exception("Extension $s is not known.");
        }
        return $this->exts[$key];
    }

    /**
    * register set of extensions
    *
    * @param array set of (name, class-name) pairs
    */
    public function registerExtensions($exts) {
        foreach ($exts as $k => $v) {
            $this->registerExtension($k, $v);
        }
    }

    /**
    * register a single extension
    *
    * @param string name of the registered extension
    * @param PHP_Shell_Extension the extension object
    */
    public function registerExtension($k, PHP_Shell_Extension $obj) {
        $obj->register();

        $this->exts[$k] = $obj;
    }

    /**
    * @return object a singleton of the class 
    */
    static function getInstance() {
        if (is_null(self::$instance)) {
            $class = __CLASS__;
            self::$instance = new $class();
        }
        return self::$instance;
    }
}