| 1 |
<?php |
| 2 |
|
| 3 |
namespace Illuminate\Support\Traits; |
| 4 |
|
| 5 |
use BadMethodCallException; |
| 6 |
use Error; |
| 7 |
|
| 8 |
trait ForwardsCalls |
| 9 |
{ |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
protected function forwardCallTo($object, $method, $parameters) |
| 21 |
{ |
| 22 |
try { |
| 23 |
return $object->{$method}(...$parameters); |
| 24 |
} catch (Error|BadMethodCallException $e) { |
| 25 |
$pattern = '~^Call to undefined method (?P<class>[^:]+)::(?P<method>[^\(]+)\(\)$~'; |
| 26 |
|
| 27 |
if (! preg_match($pattern, $e->getMessage(), $matches)) { |
| 28 |
throw $e; |
| 29 |
} |
| 30 |
|
| 31 |
if ($matches['class'] != get_class($object) || |
| 32 |
$matches['method'] != $method) { |
| 33 |
throw $e; |
| 34 |
} |
| 35 |
|
| 36 |
static::throwBadMethodCallException($method); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
protected function forwardDecoratedCallTo($object, $method, $parameters) |
| 51 |
{ |
| 52 |
$result = $this->forwardCallTo($object, $method, $parameters); |
| 53 |
|
| 54 |
return $result === $object ? $this : $result; |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
protected static function throwBadMethodCallException($method) |
| 66 |
{ |
| 67 |
throw new BadMethodCallException(sprintf( |
| 68 |
'Call to undefined method %s::%s()', static::class, $method |
| 69 |
)); |
| 70 |
} |
| 71 |
} |