summaryrefslogtreecommitdiff
path: root/framework/3rdParty/PhpShell/PHP/Shell/Commands.php
blob: 3d4bf5b2a7bdcc5f6e6ba0749d40ba1cbc95e254 (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
<?php

/**
* Commands for the PHP_Shell
*
* Extensions can register their own commands for the shell like the
* InlineHelp Extension which provides inline help for all functions
*
* It uses the pattern '? <string>' to catch the cmdline strings.
*
* registerCommand() should be called by the extensions in the register()
* method. Its parameters are
* - the regex which matches the command
* - the object and the method to call if the command is matched
* - the human readable command string and the description for the help
*/
class PHP_Shell_Commands {
    /*
    * instance of the current class
    *
    * @var PHP_Shell_Commands
    */
    static protected $instance;

    /**
    * registered commands
    *
    * array('quit' => ... )
    *
    * @var array
    * @see registerCommand
    */
    protected $commands = array();

    /**
    * register your own command for the shell
    *
    * @param string $regex a regex to match against the input line
    * @param string $obj a Object
    * @param string $method a method in the object to call of the regex matches
    * @param string $cmd the command string for the help
    * @param string $help the full help description for this command
    */
    public function registerCommand($regex, $obj, $method, $cmd, $help) {
        $this->commands[] = array(
            'regex' => $regex,
            'obj' => $obj,
            'method' => $method,
            'command' => $cmd,
            'description' => $help
        );
    }

    /**
    * return a copy of the commands array
    *
    * @return all commands
    */
    public function getCommands() {
        return $this->commands;
    }

    static function getInstance() {
        if (is_null(self::$instance)) {
            $class = __CLASS__;
            self::$instance = new $class();
        }
        return self::$instance;
    }
}