我只是创建一个简单的库Codeigniter以获取HMVC结构上的所有类和方法的清单,我在MY_Controller
上调用了该函数,但是在这里获取方法有些错误,当我var_dump
时得到了结果像这样
这是我的代码
public static function get_class_and_methods()
{
$classes = get_declared_classes();
$appdir = APPPATH."modules";
$appdirScan = array_diff(scandir($appdir), array('.', '..'));
echo "<pre>";
$arrAllModules = [];
foreach ($appdirScan as $keyParent => $module) {
$scanperModule = array_diff(preg_grep('~\.(php|phtml)$~', scandir($appdir."/".$module."/controllers")), array('.', '..'));
$arrAllModules[] = $scanperModule;
foreach ($scanperModule as $keyFChild => $value) {
// $a = file_get_contents($appdir."/".$module."/controllers/".$value);
$classes = self::getClassNameFromFile($appdir."/".$module."/controllers/".$value);
$class = new ReflectionClass($classes);
$methods = $class->getMethods();
foreach ($methods as $keySChild => $method) {
var_dump ($method);
}
}
}
$flatten = array_flatten(array_merge($arrAllModules));
die();
}
private static function getClassNameFromFile($filePathName)
{
$php_code = file_get_contents($filePathName);
$classes = array();
$tokens = token_get_all($php_code);
$count = count($tokens);
for ($i = 2; $i < $count; $i++) {
if ($tokens[$i - 2][0] == T_CLASS
&& $tokens[$i - 1][0] == T_WHITESPACE
&& $tokens[$i][0] == T_STRING
) {
$class_name = $tokens[$i][1];
$classes[] = $class_name;
}
}
return $classes[0];
}
function array_flatten($array) {
if (!is_array($array)) {
return FALSE;
}
$result = array();
foreach ($array as $key => $value) {
if (is_array($value)) {
$result = array_merge($result, array_flatten($value));
}
else {
$result[$key] = $value;
}
}
return $result;
}
我的代码和任何解决方案有问题吗?
谢谢