|
#!/usr/bin/env php |
|
<?php |
|
|
|
function help() |
|
{ |
|
echo <<<HELP |
|
Usage of phpdoc |
|
|
|
Command: |
|
|
|
phpdoc [name] [lang] |
|
|
|
name: explode, SplFileObject::__construct, etc... |
|
lang: ja, en, etc... |
|
|
|
Example: |
|
|
|
$ phpdoc explode # function |
|
$ phpdoc SplFileObject # class |
|
$ phpdoc Traversable # interface |
|
$ phpdoc PHP_EOL # constant |
|
$ phpdoc PDO::FETCH_ASSOC # object constant |
|
$ phpdoc PDO::quote # object method |
|
$ phpdoc MCRYPT_MODE_ECB # extension constant |
|
|
|
HELP; |
|
|
|
exit; |
|
} |
|
|
|
if (!isset($argv[1])) { |
|
help(); |
|
} |
|
|
|
$name = $argv[1]; |
|
$id = ''; |
|
$lang = isset($argv[2]) ? $argv[2] : 'ja'; |
|
$functions = get_defined_functions()['internal']; |
|
$interfaces = get_declared_interfaces(); |
|
$constants = array_keys(get_defined_constants(true)['Core']); |
|
$classes = get_declared_classes(); |
|
|
|
// 組み込み関数 |
|
if (in_array($name, $functions)) { |
|
$type = 'function'; |
|
|
|
// 予約済みクラス |
|
} elseif (in_array($name, $classes)) { |
|
$type = 'class'; |
|
|
|
// 予約済みインタフェース |
|
} elseif (in_array($name, $interfaces)) { |
|
$type = 'class'; |
|
|
|
// 予約済み定数 |
|
} elseif (in_array($name, $constants)) { |
|
$id = '#constant.'.str_replace('_', '-', strtolower($name)); |
|
$type = 'reserved'; |
|
$name = 'constants'; |
|
|
|
// オブジェクト定数 or メソッド |
|
} elseif (strpos($name, '::') !== false) { |
|
list($class, $name) = explode('::', $name); |
|
$ref = new ReflectionClass($class); |
|
|
|
// メソッド |
|
if ($ref->hasMethod($name)) { |
|
$type = strtolower($class); |
|
$name = strtolower($name); |
|
|
|
// 定数 |
|
} elseif ($ref->hasConstant($name)) { |
|
$type = strtolower($class); |
|
$id = "#{$type}.constants.".str_replace('_', '-', strtolower($name)); |
|
$name = 'constants'; |
|
} |
|
|
|
} else { |
|
$categorized_constants = get_defined_constants(true); |
|
foreach ($categorized_constants as $ext => $constants) { |
|
if (!isset($constants[$name])) continue; |
|
|
|
$type = strtolower($ext); |
|
$name = 'constants'; |
|
} |
|
|
|
if (!isset($type)) { |
|
throw new Exception("Not found:{$name}"); |
|
} |
|
} |
|
|
|
$name = str_replace('_', '-', $name); |
|
$name = strtolower($name); |
|
system("open http://php.net/manual/{$lang}/{$type}.{$name}.php{$id}"); |