Extractor.php 18 KB

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