ControllerMethodReflector.php 4.3 KB

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