You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BoundMethod.php 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. namespace Illuminate\Container;
  3. use Closure;
  4. use ReflectionMethod;
  5. use ReflectionFunction;
  6. use InvalidArgumentException;
  7. class BoundMethod
  8. {
  9. /**
  10. * Call the given Closure / class@method and inject its dependencies.
  11. *
  12. * @param \Illuminate\Container\Container $container
  13. * @param callable|string $callback
  14. * @param array $parameters
  15. * @param string|null $defaultMethod
  16. * @return mixed
  17. *
  18. * @throws \ReflectionException
  19. * @throws \InvalidArgumentException
  20. */
  21. public static function call($container, $callback, array $parameters = [], $defaultMethod = null)
  22. {
  23. if (static::isCallableWithAtSign($callback) || $defaultMethod) {
  24. return static::callClass($container, $callback, $parameters, $defaultMethod);
  25. }
  26. return static::callBoundMethod($container, $callback, function () use ($container, $callback, $parameters) {
  27. return call_user_func_array(
  28. $callback, static::getMethodDependencies($container, $callback, $parameters)
  29. );
  30. });
  31. }
  32. /**
  33. * Call a string reference to a class using Class@method syntax.
  34. *
  35. * @param \Illuminate\Container\Container $container
  36. * @param string $target
  37. * @param array $parameters
  38. * @param string|null $defaultMethod
  39. * @return mixed
  40. *
  41. * @throws \InvalidArgumentException
  42. */
  43. protected static function callClass($container, $target, array $parameters = [], $defaultMethod = null)
  44. {
  45. $segments = explode('@', $target);
  46. // We will assume an @ sign is used to delimit the class name from the method
  47. // name. We will split on this @ sign and then build a callable array that
  48. // we can pass right back into the "call" method for dependency binding.
  49. $method = count($segments) === 2
  50. ? $segments[1] : $defaultMethod;
  51. if (is_null($method)) {
  52. throw new InvalidArgumentException('Method not provided.');
  53. }
  54. return static::call(
  55. $container, [$container->make($segments[0]), $method], $parameters
  56. );
  57. }
  58. /**
  59. * Call a method that has been bound to the container.
  60. *
  61. * @param \Illuminate\Container\Container $container
  62. * @param callable $callback
  63. * @param mixed $default
  64. * @return mixed
  65. */
  66. protected static function callBoundMethod($container, $callback, $default)
  67. {
  68. if (! is_array($callback)) {
  69. return $default instanceof Closure ? $default() : $default;
  70. }
  71. // Here we need to turn the array callable into a Class@method string we can use to
  72. // examine the container and see if there are any method bindings for this given
  73. // method. If there are, we can call this method binding callback immediately.
  74. $method = static::normalizeMethod($callback);
  75. if ($container->hasMethodBinding($method)) {
  76. return $container->callMethodBinding($method, $callback[0]);
  77. }
  78. return $default instanceof Closure ? $default() : $default;
  79. }
  80. /**
  81. * Normalize the given callback into a Class@method string.
  82. *
  83. * @param callable $callback
  84. * @return string
  85. */
  86. protected static function normalizeMethod($callback)
  87. {
  88. $class = is_string($callback[0]) ? $callback[0] : get_class($callback[0]);
  89. return "{$class}@{$callback[1]}";
  90. }
  91. /**
  92. * Get all dependencies for a given method.
  93. *
  94. * @param \Illuminate\Container\Container $container
  95. * @param callable|string $callback
  96. * @param array $parameters
  97. * @return array
  98. *
  99. * @throws \ReflectionException
  100. */
  101. protected static function getMethodDependencies($container, $callback, array $parameters = [])
  102. {
  103. $dependencies = [];
  104. foreach (static::getCallReflector($callback)->getParameters() as $parameter) {
  105. static::addDependencyForCallParameter($container, $parameter, $parameters, $dependencies);
  106. }
  107. return array_merge($dependencies, $parameters);
  108. }
  109. /**
  110. * Get the proper reflection instance for the given callback.
  111. *
  112. * @param callable|string $callback
  113. * @return \ReflectionFunctionAbstract
  114. *
  115. * @throws \ReflectionException
  116. */
  117. protected static function getCallReflector($callback)
  118. {
  119. if (is_string($callback) && strpos($callback, '::') !== false) {
  120. $callback = explode('::', $callback);
  121. }
  122. return is_array($callback)
  123. ? new ReflectionMethod($callback[0], $callback[1])
  124. : new ReflectionFunction($callback);
  125. }
  126. /**
  127. * Get the dependency for the given call parameter.
  128. *
  129. * @param \Illuminate\Container\Container $container
  130. * @param \ReflectionParameter $parameter
  131. * @param array $parameters
  132. * @param array $dependencies
  133. * @return void
  134. */
  135. protected static function addDependencyForCallParameter($container, $parameter,
  136. array &$parameters, &$dependencies)
  137. {
  138. if (array_key_exists($parameter->name, $parameters)) {
  139. $dependencies[] = $parameters[$parameter->name];
  140. unset($parameters[$parameter->name]);
  141. } elseif ($parameter->getClass() && array_key_exists($parameter->getClass()->name, $parameters)) {
  142. $dependencies[] = $parameters[$parameter->getClass()->name];
  143. unset($parameters[$parameter->getClass()->name]);
  144. } elseif ($parameter->getClass()) {
  145. $dependencies[] = $container->make($parameter->getClass()->name);
  146. } elseif ($parameter->isDefaultValueAvailable()) {
  147. $dependencies[] = $parameter->getDefaultValue();
  148. }
  149. }
  150. /**
  151. * Determine if the given string is in Class@method syntax.
  152. *
  153. * @param mixed $callback
  154. * @return bool
  155. */
  156. protected static function isCallableWithAtSign($callback)
  157. {
  158. return is_string($callback) && strpos($callback, '@') !== false;
  159. }
  160. }