PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/libs/Nette/Reflection/MethodReflection.php

https://code.google.com/
PHP | 225 lines | 103 code | 66 blank | 56 comment | 4 complexity | fead4e8cc7de8e4f9e3b50b414010dfd MD5 | raw file
Possible License(s): Apache-2.0, GPL-2.0
  1. <?php
  2. /**
  3. * This file is part of the Nette Framework.
  4. *
  5. * Copyright (c) 2004, 2010 David Grudl (http://davidgrudl.com)
  6. *
  7. * This source file is subject to the "Nette license", and/or
  8. * GPL license. For more information please see http://nette.org
  9. */
  10. namespace Nette\Reflection;
  11. use Nette,
  12. Nette\ObjectMixin;
  13. /**
  14. * Reports information about a method.
  15. *
  16. * @author David Grudl
  17. */
  18. class MethodReflection extends \ReflectionMethod
  19. {
  20. /**
  21. * @param string|object
  22. * @param string
  23. * @return Nette\Reflection\MethodReflection
  24. */
  25. public static function from($class, $method)
  26. {
  27. return new static(is_object($class) ? get_class($class) : $class, $method);
  28. }
  29. /**
  30. * @return array
  31. */
  32. public function getDefaultParameters()
  33. {
  34. $res = array();
  35. foreach (parent::getParameters() as $param) {
  36. $res[$param->getName()] = $param->isDefaultValueAvailable()
  37. ? $param->getDefaultValue()
  38. : NULL;
  39. if ($param->isArray()) {
  40. settype($res[$param->getName()], 'array');
  41. }
  42. }
  43. return $res;
  44. }
  45. /**
  46. * Invokes method using named parameters.
  47. * @param object
  48. * @param array
  49. * @return mixed
  50. */
  51. public function invokeNamedArgs($object, $args)
  52. {
  53. $res = array();
  54. $i = 0;
  55. foreach ($this->getDefaultParameters() as $name => $def) {
  56. if (isset($args[$name])) { // NULL treats as none value
  57. $val = $args[$name];
  58. if ($def !== NULL) {
  59. settype($val, gettype($def));
  60. }
  61. $res[$i++] = $val;
  62. } else {
  63. $res[$i++] = $def;
  64. }
  65. }
  66. return $this->invokeArgs($object, $res);
  67. }
  68. /**
  69. * @return Nette\Callback
  70. */
  71. public function getCallback()
  72. {
  73. return new Nette\Callback(parent::getDeclaringClass()->getName(), $this->getName());
  74. }
  75. public function __toString()
  76. {
  77. return 'Method ' . parent::getDeclaringClass()->getName() . '::' . $this->getName() . '()';
  78. }
  79. /********************* Reflection layer ****************d*g**/
  80. /**
  81. * @return Nette\Reflection\ClassReflection
  82. */
  83. public function getDeclaringClass()
  84. {
  85. return new ClassReflection(parent::getDeclaringClass()->getName());
  86. }
  87. /**
  88. * @return Nette\Reflection\ExtensionReflection
  89. */
  90. public function getExtension()
  91. {
  92. return ($name = $this->getExtensionName()) ? new ExtensionReflection($name) : NULL;
  93. }
  94. public function getParameters()
  95. {
  96. $me = array(parent::getDeclaringClass()->getName(), $this->getName());
  97. foreach ($res = parent::getParameters() as $key => $val) {
  98. $res[$key] = new ParameterReflection($me, $val->getName());
  99. }
  100. return $res;
  101. }
  102. /********************* Nette\Annotations support ****************d*g**/
  103. /**
  104. * Has method specified annotation?
  105. * @param string
  106. * @return bool
  107. */
  108. public function hasAnnotation($name)
  109. {
  110. $res = AnnotationsParser::getAll($this);
  111. return !empty($res[$name]);
  112. }
  113. /**
  114. * Returns an annotation value.
  115. * @param string
  116. * @return IAnnotation
  117. */
  118. public function getAnnotation($name)
  119. {
  120. $res = AnnotationsParser::getAll($this);
  121. return isset($res[$name]) ? end($res[$name]) : NULL;
  122. }
  123. /**
  124. * Returns all annotations.
  125. * @return array
  126. */
  127. public function getAnnotations()
  128. {
  129. return AnnotationsParser::getAll($this);
  130. }
  131. /********************* Nette\Object behaviour ****************d*g**/
  132. /**
  133. * @return Nette\Reflection\ClassReflection
  134. */
  135. public static function getReflection()
  136. {
  137. return new Nette\Reflection\ClassReflection(get_called_class());
  138. }
  139. public function __call($name, $args)
  140. {
  141. return ObjectMixin::call($this, $name, $args);
  142. }
  143. public function &__get($name)
  144. {
  145. return ObjectMixin::get($this, $name);
  146. }
  147. public function __set($name, $value)
  148. {
  149. return ObjectMixin::set($this, $name, $value);
  150. }
  151. public function __isset($name)
  152. {
  153. return ObjectMixin::has($this, $name);
  154. }
  155. public function __unset($name)
  156. {
  157. throw new \MemberAccessException("Cannot unset the property {$this->reflection->name}::\$$name.");
  158. }
  159. }