ControllerMethodReflector.php 3.6 KB

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