Extractor.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. <?php
  2. namespace app\admin\command\Api\library;
  3. use Exception;
  4. /**
  5. * Class imported from https://github.com/eriknyk/Annotations
  6. * @author Erik Amaru Ortiz https://github.com/eriknyk‎
  7. *
  8. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  9. * @author Calin Rada <rada.calin@gmail.com>
  10. */
  11. class Extractor
  12. {
  13. /**
  14. * Static array to store already parsed annotations
  15. * @var array
  16. */
  17. private static $annotationCache;
  18. private static $classAnnotationCache;
  19. private static $classMethodAnnotationCache;
  20. private static $classPropertyValueCache;
  21. /**
  22. * Indicates that annotations should has strict behavior, 'false' by default
  23. * @var boolean
  24. */
  25. private $strict = false;
  26. /**
  27. * Stores the default namespace for Objects instance, usually used on methods like getMethodAnnotationsObjects()
  28. * @var string
  29. */
  30. public $defaultNamespace = '';
  31. /**
  32. * Sets strict variable to true/false
  33. * @param bool $value boolean value to indicate that annotations to has strict behavior
  34. */
  35. public function setStrict($value)
  36. {
  37. $this->strict = (bool)$value;
  38. }
  39. /**
  40. * Sets default namespace to use in object instantiation
  41. * @param string $namespace default namespace
  42. */
  43. public function setDefaultNamespace($namespace)
  44. {
  45. $this->defaultNamespace = $namespace;
  46. }
  47. /**
  48. * Gets default namespace used in object instantiation
  49. * @return string $namespace default namespace
  50. */
  51. public function getDefaultAnnotationNamespace()
  52. {
  53. return $this->defaultNamespace;
  54. }
  55. /**
  56. * Gets all anotations with pattern @SomeAnnotation() from a given class
  57. *
  58. * @param string $className class name to get annotations
  59. * @return array self::$classAnnotationCache all annotated elements
  60. */
  61. public static function getClassAnnotations($className)
  62. {
  63. if (!isset(self::$classAnnotationCache[$className])) {
  64. $class = new \ReflectionClass($className);
  65. $annotationArr = self::parseAnnotations($class->getDocComment());
  66. $annotationArr['ApiTitle'] = !isset($annotationArr['ApiTitle'][0]) || !trim($annotationArr['ApiTitle'][0]) ? [$class->getShortName()] : $annotationArr['ApiTitle'];
  67. self::$classAnnotationCache[$className] = $annotationArr;
  68. }
  69. return self::$classAnnotationCache[$className];
  70. }
  71. /**
  72. * 获取类所有方法的属性配置
  73. * @param $className
  74. * @return mixed
  75. * @throws \ReflectionException
  76. */
  77. public static function getClassMethodAnnotations($className)
  78. {
  79. $class = new \ReflectionClass($className);
  80. foreach ($class->getMethods() as $object) {
  81. self::$classMethodAnnotationCache[$className][$object->name] = self::getMethodAnnotations($className, $object->name);
  82. }
  83. return self::$classMethodAnnotationCache[$className];
  84. }
  85. public static function getClassPropertyValues($className)
  86. {
  87. $class = new \ReflectionClass($className);
  88. foreach ($class->getProperties() as $object) {
  89. self::$classPropertyValueCache[$className][$object->name] = self::getClassPropertyValue($className, $object->name);
  90. }
  91. return self::$classMethodAnnotationCache[$className];
  92. }
  93. public static function getAllClassAnnotations()
  94. {
  95. return self::$classAnnotationCache;
  96. }
  97. public static function getAllClassMethodAnnotations()
  98. {
  99. return self::$classMethodAnnotationCache;
  100. }
  101. public static function getAllClassPropertyValues()
  102. {
  103. return self::$classPropertyValueCache;
  104. }
  105. public static function getClassPropertyValue($className, $property)
  106. {
  107. $_SERVER['REQUEST_METHOD'] = 'GET';
  108. $reflectionClass = new \ReflectionClass($className);
  109. $reflectionProperty = $reflectionClass->getProperty($property);
  110. $reflectionProperty->setAccessible(true);
  111. return $reflectionProperty->getValue($reflectionClass->newInstanceWithoutConstructor());
  112. }
  113. /**
  114. * Gets all anotations with pattern @SomeAnnotation() from a determinated method of a given class
  115. *
  116. * @param string $className class name
  117. * @param string $methodName method name to get annotations
  118. * @return array self::$annotationCache all annotated elements of a method given
  119. */
  120. public static function getMethodAnnotations($className, $methodName)
  121. {
  122. if (!isset(self::$annotationCache[$className . '::' . $methodName])) {
  123. try {
  124. $method = new \ReflectionMethod($className, $methodName);
  125. $class = new \ReflectionClass($className);
  126. if (!$method->isPublic() || $method->isConstructor()) {
  127. $annotations = array();
  128. } else {
  129. $annotations = self::consolidateAnnotations($method, $class);
  130. }
  131. } catch (\ReflectionException $e) {
  132. $annotations = array();
  133. }
  134. self::$annotationCache[$className . '::' . $methodName] = $annotations;
  135. }
  136. return self::$annotationCache[$className . '::' . $methodName];
  137. }
  138. /**
  139. * Gets all anotations with pattern @SomeAnnotation() from a determinated method of a given class
  140. * and instance its abcAnnotation class
  141. *
  142. * @param string $className class name
  143. * @param string $methodName method name to get annotations
  144. * @return array self::$annotationCache all annotated objects of a method given
  145. */
  146. public function getMethodAnnotationsObjects($className, $methodName)
  147. {
  148. $annotations = $this->getMethodAnnotations($className, $methodName);
  149. $objects = array();
  150. $i = 0;
  151. foreach ($annotations as $annotationClass => $listParams) {
  152. $annotationClass = ucfirst($annotationClass);
  153. $class = $this->defaultNamespace . $annotationClass . 'Annotation';
  154. // verify is the annotation class exists, depending if Annotations::strict is true
  155. // if not, just skip the annotation instance creation.
  156. if (!class_exists($class)) {
  157. if ($this->strict) {
  158. throw new Exception(sprintf('Runtime Error: Annotation Class Not Found: %s', $class));
  159. } else {
  160. // silent skip & continue
  161. continue;
  162. }
  163. }
  164. if (empty($objects[$annotationClass])) {
  165. $objects[$annotationClass] = new $class();
  166. }
  167. foreach ($listParams as $params) {
  168. if (is_array($params)) {
  169. foreach ($params as $key => $value) {
  170. $objects[$annotationClass]->set($key, $value);
  171. }
  172. } else {
  173. $objects[$annotationClass]->set($i++, $params);
  174. }
  175. }
  176. }
  177. return $objects;
  178. }
  179. private static function consolidateAnnotations($method, $class)
  180. {
  181. $dockblockClass = $class->getDocComment();
  182. $docblockMethod = $method->getDocComment();
  183. $methodName = $method->getName();
  184. $methodAnnotations = self::parseAnnotations($docblockMethod);
  185. $methodAnnotations['ApiTitle'] = !isset($methodAnnotations['ApiTitle'][0]) || !trim($methodAnnotations['ApiTitle'][0]) ? [$method->getName()] : $methodAnnotations['ApiTitle'];
  186. $classAnnotations = self::parseAnnotations($dockblockClass);
  187. $classAnnotations['ApiTitle'] = !isset($classAnnotations['ApiTitle'][0]) || !trim($classAnnotations['ApiTitle'][0]) ? [$class->getShortName()] : $classAnnotations['ApiTitle'];
  188. if (isset($methodAnnotations['ApiInternal']) || $methodName == '_initialize' || $methodName == '_empty') {
  189. return [];
  190. }
  191. $properties = $class->getDefaultProperties();
  192. $noNeedLogin = isset($properties['noNeedLogin']) ? is_array($properties['noNeedLogin']) ? $properties['noNeedLogin'] : [$properties['noNeedLogin']] : [];
  193. $noNeedRight = isset($properties['noNeedRight']) ? is_array($properties['noNeedRight']) ? $properties['noNeedRight'] : [$properties['noNeedRight']] : [];
  194. preg_match_all("/\*[\s]+(.*)(\\r\\n|\\r|\\n)/U", str_replace('/**', '', $docblockMethod), $methodArr);
  195. preg_match_all("/\*[\s]+(.*)(\\r\\n|\\r|\\n)/U", str_replace('/**', '', $dockblockClass), $classArr);
  196. if (!isset($methodAnnotations['ApiMethod'])) {
  197. $methodAnnotations['ApiMethod'] = ['get'];
  198. }
  199. if (!isset($methodAnnotations['ApiWeigh'])) {
  200. $methodAnnotations['ApiWeigh'] = [0];
  201. }
  202. if (!isset($methodAnnotations['ApiSummary'])) {
  203. $methodAnnotations['ApiSummary'] = $methodAnnotations['ApiTitle'];
  204. }
  205. if ($methodAnnotations) {
  206. foreach ($classAnnotations as $name => $valueClass) {
  207. if (count($valueClass) !== 1) {
  208. continue;
  209. }
  210. if ($name === 'ApiRoute') {
  211. if (isset($methodAnnotations[$name])) {
  212. $methodAnnotations[$name] = [rtrim($valueClass[0], '/') . $methodAnnotations[$name][0]];
  213. } else {
  214. $methodAnnotations[$name] = [rtrim($valueClass[0], '/') . '/' . $method->getName()];
  215. }
  216. }
  217. if ($name === 'ApiSector') {
  218. $methodAnnotations[$name] = $valueClass;
  219. }
  220. }
  221. }
  222. if (!isset($methodAnnotations['ApiRoute'])) {
  223. $urlArr = [];
  224. $className = $class->getName();
  225. list($prefix, $suffix) = explode('\\' . \think\Config::get('url_controller_layer') . '\\', $className);
  226. $prefixArr = explode('\\', $prefix);
  227. $suffixArr = explode('\\', $suffix);
  228. if ($prefixArr[0] == \think\Config::get('app_namespace')) {
  229. $prefixArr[0] = '';
  230. }
  231. $urlArr = array_merge($urlArr, $prefixArr);
  232. $urlArr[] = implode('.', array_map(function ($item) {
  233. return \think\Loader::parseName($item);
  234. }, $suffixArr));
  235. $urlArr[] = $method->getName();
  236. $methodAnnotations['ApiRoute'] = [implode('/', $urlArr)];
  237. }
  238. if (!isset($methodAnnotations['ApiSector'])) {
  239. $methodAnnotations['ApiSector'] = isset($classAnnotations['ApiSector']) ? $classAnnotations['ApiSector'] : $classAnnotations['ApiTitle'];
  240. }
  241. if (!isset($methodAnnotations['ApiParams'])) {
  242. $params = self::parseCustomAnnotations($docblockMethod, 'param');
  243. foreach ($params as $k => $v) {
  244. $arr = explode(' ', preg_replace("/[\s]+/", " ", $v));
  245. $methodAnnotations['ApiParams'][] = [
  246. 'name' => isset($arr[1]) ? str_replace('$', '', $arr[1]) : '',
  247. 'nullable' => false,
  248. 'type' => isset($arr[0]) ? $arr[0] : 'string',
  249. 'description' => isset($arr[2]) ? $arr[2] : ''
  250. ];
  251. }
  252. }
  253. $methodAnnotations['ApiPermissionLogin'] = [!in_array('*', $noNeedLogin) && !in_array($methodName, $noNeedLogin)];
  254. $methodAnnotations['ApiPermissionRight'] = !$methodAnnotations['ApiPermissionLogin'][0] ? false : [!in_array('*', $noNeedRight) && !in_array($methodName, $noNeedRight)];
  255. return $methodAnnotations;
  256. }
  257. /**
  258. * Parse annotations
  259. *
  260. * @param string $docblock
  261. * @param string $name
  262. * @return array parsed annotations params
  263. */
  264. private static function parseCustomAnnotations($docblock, $name = 'param')
  265. {
  266. $annotations = array();
  267. $docblock = substr($docblock, 3, -2);
  268. if (preg_match_all('/@' . $name . '(?:\s*(?:\(\s*)?(.*?)(?:\s*\))?)??\s*(?:\n|\*\/)/', $docblock, $matches)) {
  269. foreach ($matches[1] as $k => $v) {
  270. $annotations[] = $v;
  271. }
  272. }
  273. return $annotations;
  274. }
  275. /**
  276. * Parse annotations
  277. *
  278. * @param string $docblock
  279. * @return array parsed annotations params
  280. */
  281. private static function parseAnnotations($docblock)
  282. {
  283. $annotations = array();
  284. // Strip away the docblock header and footer to ease parsing of one line annotations
  285. $docblock = substr($docblock, 3, -2);
  286. if (preg_match_all('/@(?<name>[A-Za-z_-]+)[\s\t]*\((?<args>(?:(?!\)).)*)\)\r?/s', $docblock, $matches)) {
  287. $numMatches = count($matches[0]);
  288. for ($i = 0; $i < $numMatches; ++$i) {
  289. $name = $matches['name'][$i];
  290. $value = '';
  291. // annotations has arguments
  292. if (isset($matches['args'][$i])) {
  293. $argsParts = trim($matches['args'][$i]);
  294. if ($name == 'ApiReturn') {
  295. $value = $argsParts;
  296. } elseif ($matches['args'][$i] != '') {
  297. $argsParts = preg_replace("/\{(\w+)\}/", '#$1#', $argsParts);
  298. $value = self::parseArgs($argsParts);
  299. if (is_string($value)) {
  300. $value = preg_replace("/\#(\w+)\#/", '{$1}', $argsParts);
  301. }
  302. }
  303. }
  304. $annotations[$name][] = $value;
  305. }
  306. }
  307. if (stripos($docblock, '@ApiInternal') !== false) {
  308. $annotations['ApiInternal'] = [true];
  309. }
  310. if (!isset($annotations['ApiTitle'])) {
  311. preg_match_all("/\*[\s]+(.*)(\\r\\n|\\r|\\n)/U", str_replace('/**', '', $docblock), $matchArr);
  312. $title = isset($matchArr[1]) && isset($matchArr[1][0]) ? $matchArr[1][0] : '';
  313. $annotations['ApiTitle'] = [$title];
  314. }
  315. return $annotations;
  316. }
  317. /**
  318. * Parse individual annotation arguments
  319. *
  320. * @param string $content arguments string
  321. * @return array annotated arguments
  322. */
  323. private static function parseArgs($content)
  324. {
  325. // Replace initial stars
  326. $content = preg_replace('/^\s*\*/m', '', $content);
  327. $data = array();
  328. $len = strlen($content);
  329. $i = 0;
  330. $var = '';
  331. $val = '';
  332. $level = 1;
  333. $prevDelimiter = '';
  334. $nextDelimiter = '';
  335. $nextToken = '';
  336. $composing = false;
  337. $type = 'plain';
  338. $delimiter = null;
  339. $quoted = false;
  340. $tokens = array('"', '"', '{', '}', ',', '=');
  341. while ($i <= $len) {
  342. $prev_c = substr($content, $i - 1, 1);
  343. $c = substr($content, $i++, 1);
  344. if ($c === '"' && $prev_c !== "\\") {
  345. $delimiter = $c;
  346. //open delimiter
  347. if (!$composing && empty($prevDelimiter) && empty($nextDelimiter)) {
  348. $prevDelimiter = $nextDelimiter = $delimiter;
  349. $val = '';
  350. $composing = true;
  351. $quoted = true;
  352. } else {
  353. // close delimiter
  354. if ($c !== $nextDelimiter) {
  355. throw new Exception(sprintf(
  356. "Parse Error: enclosing error -> expected: [%s], given: [%s]",
  357. $nextDelimiter,
  358. $c
  359. ));
  360. }
  361. // validating syntax
  362. if ($i < $len) {
  363. if (',' !== substr($content, $i, 1) && '\\' !== $prev_c) {
  364. throw new Exception(sprintf(
  365. "Parse Error: missing comma separator near: ...%s<--",
  366. substr($content, ($i - 10), $i)
  367. ));
  368. }
  369. }
  370. $prevDelimiter = $nextDelimiter = '';
  371. $composing = false;
  372. $delimiter = null;
  373. }
  374. } elseif (!$composing && in_array($c, $tokens)) {
  375. switch ($c) {
  376. case '=':
  377. $prevDelimiter = $nextDelimiter = '';
  378. $level = 2;
  379. $composing = false;
  380. $type = 'assoc';
  381. $quoted = false;
  382. break;
  383. case ',':
  384. $level = 3;
  385. // If composing flag is true yet,
  386. // it means that the string was not enclosed, so it is parsing error.
  387. if ($composing === true && !empty($prevDelimiter) && !empty($nextDelimiter)) {
  388. throw new Exception(sprintf(
  389. "Parse Error: enclosing error -> expected: [%s], given: [%s]",
  390. $nextDelimiter,
  391. $c
  392. ));
  393. }
  394. $prevDelimiter = $nextDelimiter = '';
  395. break;
  396. case '{':
  397. $subc = '';
  398. $subComposing = true;
  399. while ($i <= $len) {
  400. $c = substr($content, $i++, 1);
  401. if (isset($delimiter) && $c === $delimiter) {
  402. throw new Exception(sprintf(
  403. "Parse Error: Composite variable is not enclosed correctly."
  404. ));
  405. }
  406. if ($c === '}') {
  407. $subComposing = false;
  408. break;
  409. }
  410. $subc .= $c;
  411. }
  412. // if the string is composing yet means that the structure of var. never was enclosed with '}'
  413. if ($subComposing) {
  414. throw new Exception(sprintf(
  415. "Parse Error: Composite variable is not enclosed correctly. near: ...%s'",
  416. $subc
  417. ));
  418. }
  419. $val = self::parseArgs($subc);
  420. break;
  421. }
  422. } else {
  423. if ($level == 1) {
  424. $var .= $c;
  425. } elseif ($level == 2) {
  426. $val .= $c;
  427. }
  428. }
  429. if ($level === 3 || $i === $len) {
  430. if ($type == 'plain' && $i === $len) {
  431. $data = self::castValue($var);
  432. } else {
  433. $data[trim($var)] = self::castValue($val, !$quoted);
  434. }
  435. $level = 1;
  436. $var = $val = '';
  437. $composing = false;
  438. $quoted = false;
  439. }
  440. }
  441. return $data;
  442. }
  443. /**
  444. * Try determinate the original type variable of a string
  445. *
  446. * @param string $val string containing possibles variables that can be cast to bool or int
  447. * @param boolean $trim indicate if the value passed should be trimmed after to try cast
  448. * @return mixed returns the value converted to original type if was possible
  449. */
  450. private static function castValue($val, $trim = false)
  451. {
  452. if (is_array($val)) {
  453. foreach ($val as $key => $value) {
  454. $val[$key] = self::castValue($value);
  455. }
  456. } elseif (is_string($val)) {
  457. if ($trim) {
  458. $val = trim($val);
  459. }
  460. $val = stripslashes($val);
  461. $tmp = strtolower($val);
  462. if ($tmp === 'false' || $tmp === 'true') {
  463. $val = $tmp === 'true';
  464. } elseif (is_numeric($val)) {
  465. return $val + 0;
  466. }
  467. unset($tmp);
  468. }
  469. return $val;
  470. }
  471. }