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.

ForwardsCalls.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Illuminate\Support\Traits;
  3. use Error;
  4. use BadMethodCallException;
  5. trait ForwardsCalls
  6. {
  7. /**
  8. * Forward a method call to the given object.
  9. *
  10. * @param mixed $object
  11. * @param string $method
  12. * @param array $parameters
  13. * @return mixed
  14. *
  15. * @throws \BadMethodCallException
  16. */
  17. protected function forwardCallTo($object, $method, $parameters)
  18. {
  19. try {
  20. return $object->{$method}(...$parameters);
  21. } catch (Error | BadMethodCallException $e) {
  22. $pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~';
  23. if (! preg_match($pattern, $e->getMessage(), $matches)) {
  24. throw $e;
  25. }
  26. if ($matches['class'] != get_class($object) ||
  27. $matches['method'] != $method) {
  28. throw $e;
  29. }
  30. static::throwBadMethodCallException($method);
  31. }
  32. }
  33. /**
  34. * Throw a bad method call exception for the given method.
  35. *
  36. * @param string $method
  37. * @return void
  38. *
  39. * @throws \BadMethodCallException
  40. */
  41. protected static function throwBadMethodCallException($method)
  42. {
  43. throw new BadMethodCallException(sprintf(
  44. 'Call to undefined method %s::%s()', static::class, $method
  45. ));
  46. }
  47. }