ControllerMethodReflector.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Bernhard Posselt <dev@bernhard-posselt.com>
  7. * @author Bjoern Schiessle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  10. * @author Joas Schilling <coding@schilljs.com>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Morris Jobke <hey@morrisjobke.de>
  13. * @author Olivier Paroz <github@oparoz.com>
  14. * @author Roeland Jago Douma <roeland@famdouma.nl>
  15. * @author Thomas Müller <thomas.mueller@tmit.eu>
  16. *
  17. * @license AGPL-3.0
  18. *
  19. * This code is free software: you can redistribute it and/or modify
  20. * it under the terms of the GNU Affero General Public License, version 3,
  21. * as published by the Free Software Foundation.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU Affero General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU Affero General Public License, version 3,
  29. * along with this program. If not, see <http://www.gnu.org/licenses/>
  30. *
  31. */
  32. namespace OC\AppFramework\Utility;
  33. use OCP\AppFramework\Utility\IControllerMethodReflector;
  34. /**
  35. * Reads and parses annotations from doc comments
  36. */
  37. class ControllerMethodReflector implements IControllerMethodReflector {
  38. public $annotations = [];
  39. private $types = [];
  40. private $parameters = [];
  41. /**
  42. * @param object $object an object or classname
  43. * @param string $method the method which we want to inspect
  44. */
  45. public function reflect($object, string $method) {
  46. $reflection = new \ReflectionMethod($object, $method);
  47. $docs = $reflection->getDocComment();
  48. if ($docs !== false) {
  49. // extract everything prefixed by @ and first letter uppercase
  50. preg_match_all('/^\h+\*\h+@(?P<annotation>[A-Z]\w+)((?P<parameter>.*))?$/m', $docs, $matches);
  51. foreach ($matches['annotation'] as $key => $annontation) {
  52. $annontation = strtolower($annontation);
  53. $annotationValue = $matches['parameter'][$key];
  54. if (isset($annotationValue[0]) && $annotationValue[0] === '(' && $annotationValue[\strlen($annotationValue) - 1] === ')') {
  55. $cutString = substr($annotationValue, 1, -1);
  56. $cutString = str_replace(' ', '', $cutString);
  57. $splittedArray = explode(',', $cutString);
  58. foreach ($splittedArray as $annotationValues) {
  59. [$key, $value] = explode('=', $annotationValues);
  60. $this->annotations[$annontation][$key] = $value;
  61. }
  62. continue;
  63. }
  64. $this->annotations[$annontation] = [$annotationValue];
  65. }
  66. // extract type parameter information
  67. preg_match_all('/@param\h+(?P<type>\w+)\h+\$(?P<var>\w+)/', $docs, $matches);
  68. $this->types = array_combine($matches['var'], $matches['type']);
  69. }
  70. foreach ($reflection->getParameters() as $param) {
  71. // extract type information from PHP 7 scalar types and prefer them over phpdoc annotations
  72. $type = $param->getType();
  73. if ($type instanceof \ReflectionNamedType) {
  74. $this->types[$param->getName()] = $type->getName();
  75. }
  76. $default = null;
  77. if ($param->isOptional()) {
  78. $default = $param->getDefaultValue();
  79. }
  80. $this->parameters[$param->name] = $default;
  81. }
  82. }
  83. /**
  84. * Inspects the PHPDoc parameters for types
  85. * @param string $parameter the parameter whose type comments should be
  86. * parsed
  87. * @return string|null type in the type parameters (@param int $something)
  88. * would return int or null if not existing
  89. */
  90. public function getType(string $parameter) {
  91. if (array_key_exists($parameter, $this->types)) {
  92. return $this->types[$parameter];
  93. }
  94. return null;
  95. }
  96. /**
  97. * @return array the arguments of the method with key => default value
  98. */
  99. public function getParameters(): array {
  100. return $this->parameters;
  101. }
  102. /**
  103. * Check if a method contains an annotation
  104. * @param string $name the name of the annotation
  105. * @return bool true if the annotation is found
  106. */
  107. public function hasAnnotation(string $name): bool {
  108. $name = strtolower($name);
  109. return array_key_exists($name, $this->annotations);
  110. }
  111. /**
  112. * Get optional annotation parameter by key
  113. *
  114. * @param string $name the name of the annotation
  115. * @param string $key the string of the annotation
  116. * @return string
  117. */
  118. public function getAnnotationParameter(string $name, string $key): string {
  119. $name = strtolower($name);
  120. if (isset($this->annotations[$name][$key])) {
  121. return $this->annotations[$name][$key];
  122. }
  123. return '';
  124. }
  125. }