1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
require_once(dirname(__FILE__)."/../Extensions.php"); |
4
|
|
|
require_once(dirname(__FILE__)."/Prototypes.php"); |
5
|
|
|
|
6
|
|
|
class PHP_Shell_Extensions_InlineHelp implements PHP_Shell_Extension { |
7
|
|
|
public function register() { |
8
|
|
|
$cmd = PHP_Shell_Commands::getInstance(); |
9
|
|
|
|
10
|
|
|
$cmd->registerCommand('#^\? #', $this, 'cmdHelp', '? <var>', |
11
|
|
|
'show the DocComment a Class, Method or Function'.PHP_EOL. |
12
|
|
|
' e.g.: ? fopen(), ? PHP_Shell, ? $__shell'); |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* handle the '?' commands |
17
|
|
|
* |
18
|
|
|
* With the help of the Reflection Class we extract the DocComments and display them |
19
|
|
|
* For internal Functions we extract the prototype from the php source. |
20
|
|
|
* |
21
|
|
|
* ? Class::method() |
22
|
|
|
* ? $obj->method() |
23
|
|
|
* ? Class::property |
24
|
|
|
* ? $obj::property |
25
|
|
|
* ? Class |
26
|
|
|
* ? $obj |
27
|
|
|
* ? function() |
28
|
|
|
* |
29
|
|
|
* The license of the PHP_Shell class |
30
|
|
|
* ? license |
31
|
|
|
* |
32
|
|
|
* @return string the help text |
33
|
|
|
*/ |
34
|
|
|
public function cmdHelp($l) { |
|
|
|
|
35
|
|
|
if ("? " == substr($l, 0, strlen("? "))) { |
36
|
|
|
$str = substr($l, 2); |
37
|
|
|
|
38
|
|
|
$cmd = ''; |
39
|
|
|
|
40
|
|
|
if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) { |
41
|
|
|
/* ? Class::method() */ |
42
|
|
|
|
43
|
|
|
$class = $a[1]; |
44
|
|
|
$method = $a[2]; |
45
|
|
|
|
46
|
|
|
if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($class.'::'.$method))) { |
47
|
|
|
|
48
|
|
|
$cmd = sprintf("/**\n* %s\n\n* @params %s\n* @return %s\n*/\n", |
49
|
|
|
$proto['description'], |
50
|
|
|
$proto['params'], |
51
|
|
|
$proto['return'] |
52
|
|
|
); |
53
|
|
|
} else if (class_exists($class, false)) { |
54
|
|
|
$c = new ReflectionClass($class); |
55
|
|
|
|
56
|
|
|
if ($c->hasMethod($method)) { |
57
|
|
|
$cmd = $c->getMethod($method)->getDocComment(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\(\s*\)\s*#', $str, $a)) { |
61
|
|
|
/* ? $obj->method() */ |
62
|
|
View Code Duplication |
if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) { |
|
|
|
|
63
|
|
|
$class = get_class($GLOBALS[$a[1]]); |
64
|
|
|
$method = $a[2]; |
65
|
|
|
|
66
|
|
|
$c = new ReflectionClass($class); |
67
|
|
|
|
68
|
|
|
if ($c->hasMethod($method)) { |
69
|
|
|
$cmd = $c->getMethod($method)->getDocComment(); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} else if (preg_match('#^([A-Za-z0-9_]+)::([a-zA-Z0-9_]+)\s*$#', $str, $a)) { |
73
|
|
|
/* ? Class::property */ |
74
|
|
|
$class = $a[1]; |
75
|
|
|
$property = $a[2]; |
76
|
|
|
if (class_exists($class, false)) { |
77
|
|
|
$c = new ReflectionClass($class); |
78
|
|
|
|
79
|
|
|
if ($c->hasProperty($property)) { |
80
|
|
|
$cmd = $c->getProperty($property)->getDocComment(); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} else if (preg_match('#^\$([A-Za-z0-9_]+)->([a-zA-Z0-9_]+)\s*$#', $str, $a)) { |
84
|
|
|
/* ? $obj->property */ |
85
|
|
View Code Duplication |
if (isset($GLOBALS[$a[1]]) && is_object($GLOBALS[$a[1]])) { |
|
|
|
|
86
|
|
|
$class = get_class($GLOBALS[$a[1]]); |
87
|
|
|
$method = $a[2]; |
|
|
|
|
88
|
|
|
|
89
|
|
|
$c = new ReflectionClass($class); |
90
|
|
|
|
91
|
|
|
if ($c->hasProperty($property)) { |
|
|
|
|
92
|
|
|
$cmd = $c->getProperty($property)->getDocComment(); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
} else if (preg_match('#^([A-Za-z0-9_]+)$#', $str, $a)) { |
97
|
|
|
/* ? Class */ |
98
|
|
|
if (class_exists($a[1], false)) { |
99
|
|
|
$c = new ReflectionClass($a[1]); |
100
|
|
|
$cmd = $c->getDocComment(); |
101
|
|
|
} |
102
|
|
|
} else if (preg_match('#^\$([A-Za-z0-9_]+)$#', $str, $a)) { |
103
|
|
|
/* ? $object */ |
104
|
|
|
$obj = $a[1]; |
105
|
|
|
if (isset($GLOBALS[$obj]) && is_object($GLOBALS[$obj])) { |
106
|
|
|
$class = get_class($GLOBALS[$obj]); |
107
|
|
|
|
108
|
|
|
$c = new ReflectionClass($class); |
109
|
|
|
$cmd = $c->getDocComment(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
} else if (preg_match('#^([A-Za-z0-9_]+)\(\s*\)$#', $str, $a)) { |
113
|
|
|
/* ? function() */ |
114
|
|
|
$func = $a[1]; |
115
|
|
|
|
116
|
|
|
if (false !== ($proto = PHP_ShellPrototypes::getInstance()->get($func))) { |
117
|
|
|
$cmd = sprintf("/**\n* %s\n*\n* @params %s\n* @return %s\n*/\n", |
118
|
|
|
$proto['description'], |
119
|
|
|
$proto['params'], |
120
|
|
|
$proto['return'] |
121
|
|
|
); |
122
|
|
|
} else if (function_exists($func)) { |
123
|
|
|
$c = new ReflectionFunction($func); |
124
|
|
|
$cmd = $c->getDocComment(); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if ($cmd == '') { |
129
|
|
|
$cmd = var_export(sprintf('no help found for \'%s\'', $str), 1); |
130
|
|
|
} else { |
131
|
|
|
$cmd = var_export($cmd, 1); |
132
|
|
|
} |
133
|
|
|
} else if ("?" == $l) { |
134
|
|
|
$cmd = $this->getHelp(); |
|
|
|
|
135
|
|
|
$cmd = var_export($cmd, 1); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $cmd; |
|
|
|
|
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: