ReflectorItem.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds the PhpMyAdmin\Di\ReflectorItem class
  5. *
  6. * @package PhpMyAdmin\Di
  7. */
  8. namespace PhpMyAdmin\Di;
  9. /**
  10. * Reflector manager
  11. *
  12. * @package PhpMyAdmin\Di
  13. */
  14. abstract class ReflectorItem implements Item
  15. {
  16. /** @var Container */
  17. private $_container;
  18. /** @var \Reflector */
  19. private $_reflector;
  20. /**
  21. * Constructor
  22. *
  23. * @param Container $container Container
  24. * @param mixed $definition Definition
  25. */
  26. public function __construct(Container $container, $definition)
  27. {
  28. $this->_container = $container;
  29. $this->_reflector = self::_resolveReflector($definition);
  30. }
  31. /**
  32. * Invoke the reflector with given parameters
  33. *
  34. * @param array $params Parameters
  35. * @return mixed
  36. */
  37. protected function invoke(array $params = array())
  38. {
  39. $args = array();
  40. $reflector = $this->_reflector;
  41. if ($reflector instanceof \ReflectionClass) {
  42. $constructor = $reflector->getConstructor();
  43. if (isset($constructor)) {
  44. $args = $this->_resolveArgs(
  45. $constructor->getParameters(),
  46. $params
  47. );
  48. }
  49. return $reflector->newInstanceArgs($args);
  50. }
  51. /** @var \ReflectionFunctionAbstract $reflector */
  52. $args = $this->_resolveArgs(
  53. $reflector->getParameters(),
  54. $params
  55. );
  56. if ($reflector instanceof \ReflectionMethod) {
  57. /** @var \ReflectionMethod $reflector */
  58. return $reflector->invokeArgs(null, $args);
  59. }
  60. /** @var \ReflectionFunction $reflector */
  61. return $reflector->invokeArgs($args);
  62. }
  63. /**
  64. * Getting required arguments with given parameters
  65. *
  66. * @param \ReflectionParameter[] $required Arguments
  67. * @param array $params Parameters
  68. *
  69. *@return array
  70. */
  71. private function _resolveArgs($required, array $params = array())
  72. {
  73. $args = array();
  74. foreach ($required as $param) {
  75. $name = $param->getName();
  76. $type = $param->getClass();
  77. if (isset($type)) {
  78. $type = $type->getName();
  79. }
  80. if (isset($params[$name])) {
  81. $args[] = $params[$name];
  82. } elseif (is_string($type) && isset($params[$type])) {
  83. $args[] = $params[$type];
  84. } else {
  85. try {
  86. $content = $this->_container->get($name);
  87. if (isset($content)) {
  88. $args[] = $content;
  89. } elseif (is_string($type)) {
  90. $args[] = $this->_container->get($type);
  91. } else {
  92. $args[] = null;
  93. }
  94. } catch (NotFoundException $e) {
  95. $args[] = null;
  96. }
  97. }
  98. }
  99. return $args;
  100. }
  101. /**
  102. * Resolve the reflection
  103. *
  104. * @param mixed $definition Definition
  105. *
  106. * @return \Reflector
  107. */
  108. private static function _resolveReflector($definition)
  109. {
  110. if (function_exists($definition)) {
  111. return new \ReflectionFunction($definition);
  112. }
  113. if (is_string($definition)) {
  114. $definition = explode('::', $definition);
  115. }
  116. if (!isset($definition[1])) {
  117. return new \ReflectionClass($definition[0]);
  118. }
  119. return new \ReflectionMethod($definition[0], $definition[1]);
  120. }
  121. }