|
@@ -98,13 +98,13 @@ class Api extends Command
|
|
|
foreach ($files as $name => $file) {
|
|
|
if (!$file->isDir() && $file->getExtension() == 'php') {
|
|
|
$filePath = $file->getRealPath();
|
|
|
- $classes[] = $this->get_class_from_file($filePath);
|
|
|
+ $classes[] = $this->getClassFromFile($filePath);
|
|
|
}
|
|
|
}
|
|
|
} else {
|
|
|
foreach ($controller as $index => $item) {
|
|
|
$filePath = $moduleDir . Config::get('url_controller_layer') . DS . $item . '.php';
|
|
|
- $classes[] = $this->get_class_from_file($filePath);
|
|
|
+ $classes[] = $this->getClassFromFile($filePath);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -129,67 +129,61 @@ class Api extends Command
|
|
|
}
|
|
|
|
|
|
|
|
|
- * get full qualified class name
|
|
|
+ * 从文件获取命名空间和类名
|
|
|
*
|
|
|
- * @param string $path_to_file
|
|
|
+ * @param string $filename
|
|
|
* @return string
|
|
|
- * @author JBYRNE http:
|
|
|
*/
|
|
|
- protected function get_class_from_file($path_to_file)
|
|
|
+ protected function getClassFromFile($filename)
|
|
|
{
|
|
|
-
|
|
|
- $contents = file_get_contents($path_to_file);
|
|
|
-
|
|
|
-
|
|
|
- $namespace = $class = "";
|
|
|
-
|
|
|
-
|
|
|
- $getting_namespace = $getting_class = false;
|
|
|
-
|
|
|
-
|
|
|
- foreach (token_get_all($contents) as $token) {
|
|
|
-
|
|
|
-
|
|
|
- if (is_array($token) && $token[0] == T_NAMESPACE) {
|
|
|
- $getting_namespace = true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if (is_array($token) && $token[0] == T_CLASS) {
|
|
|
- $getting_class = true;
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if ($getting_namespace === true) {
|
|
|
-
|
|
|
-
|
|
|
- if (is_array($token) && in_array($token[0], version_compare(PHP_VERSION, '8.0.0', '<') ? [T_STRING, T_NS_SEPARATOR] : [T_NAME_QUALIFIED])) {
|
|
|
-
|
|
|
-
|
|
|
- $namespace .= $token[1];
|
|
|
- } elseif ($token === ';') {
|
|
|
-
|
|
|
-
|
|
|
- $getting_namespace = false;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- if ($getting_class === true) {
|
|
|
-
|
|
|
-
|
|
|
- if (is_array($token) && $token[0] == T_STRING) {
|
|
|
-
|
|
|
-
|
|
|
- $class = $token[1];
|
|
|
-
|
|
|
-
|
|
|
- break;
|
|
|
+ $getNext = null;
|
|
|
+ $isNamespace = false;
|
|
|
+ $skipNext = false;
|
|
|
+ $namespace = '';
|
|
|
+ $class = '';
|
|
|
+ foreach (\PhpToken::tokenize(file_get_contents($filename)) as $token) {
|
|
|
+ if (!$token->isIgnorable()) {
|
|
|
+ $name = $token->getTokenName();
|
|
|
+ switch ($name) {
|
|
|
+ case 'T_NAMESPACE':
|
|
|
+ $isNamespace = true;
|
|
|
+ break;
|
|
|
+ case 'T_EXTENDS':
|
|
|
+ case 'T_USE':
|
|
|
+ case 'T_IMPLEMENTS':
|
|
|
+ $skipNext = true;
|
|
|
+ break;
|
|
|
+ case 'T_CLASS':
|
|
|
+ if ($skipNext) {
|
|
|
+ $skipNext = false;
|
|
|
+ } else {
|
|
|
+ $getNext = strtolower(substr($name, 2));
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 'T_NAME_QUALIFIED':
|
|
|
+ case 'T_NS_SEPARATOR':
|
|
|
+ case 'T_STRING':
|
|
|
+ case ';':
|
|
|
+ if ($isNamespace) {
|
|
|
+ if ($name == ';') {
|
|
|
+ $isNamespace = false;
|
|
|
+ } else {
|
|
|
+ $namespace .= $token->text;
|
|
|
+ }
|
|
|
+ } elseif ($skipNext) {
|
|
|
+ $skipNext = false;
|
|
|
+ } elseif ($getNext == 'class') {
|
|
|
+ $class = $token->text;
|
|
|
+ $getNext = null;
|
|
|
+ break 2;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ default:
|
|
|
+ $getNext = null;
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-
|
|
|
- return $namespace ? $namespace . '\\' . $class : $class;
|
|
|
+ return $namespace . '\\' . $class;
|
|
|
}
|
|
|
}
|