|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Imanghafoori\SmartFacades; |
|
4
|
|
|
|
|
5
|
|
|
use TypeError; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
use RuntimeException; |
|
8
|
|
|
use Illuminate\Support\Facades\Facade as LaravelFacade; |
|
9
|
|
|
|
|
10
|
|
|
class Facade extends LaravelFacade |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Handle dynamic, static calls to the object. |
|
14
|
|
|
* |
|
15
|
|
|
* @param string $method |
|
16
|
|
|
* @param array $args |
|
17
|
|
|
* @return mixed |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \RuntimeException |
|
20
|
|
|
* @throws \ReflectionException |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public static function __callStatic($method, $args) |
|
23
|
|
|
{ |
|
24
|
4 |
|
$instance = static::getFacadeRoot(); |
|
25
|
|
|
|
|
26
|
4 |
|
if (! $instance) { |
|
27
|
|
|
throw new RuntimeException('A facade root has not been set.'); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
try { |
|
31
|
4 |
|
return $instance->$method(...$args); |
|
32
|
4 |
|
} catch (TypeError $error) { |
|
33
|
4 |
|
$params = (new ReflectionMethod($instance, $method))->getParameters(); |
|
34
|
4 |
|
self::addMissingDependencies($params, $args); |
|
|
|
|
|
|
35
|
4 |
|
return $instance->$method(...$args); |
|
36
|
|
|
} |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Adds missing dependencies to the user-provided input. |
|
41
|
|
|
* |
|
42
|
|
|
* @param ReflectionParameter[] $parameters |
|
43
|
|
|
* @param array $inputData |
|
44
|
|
|
*/ |
|
45
|
4 |
|
private static function addMissingDependencies($parameters, array &$inputData) |
|
46
|
|
|
{ |
|
47
|
4 |
|
foreach ($parameters as $i => $parameter) { |
|
48
|
|
|
// Injects missing type hinted parameters within the array |
|
49
|
4 |
|
$class = $parameter->getClass()->name ?? false; |
|
50
|
4 |
|
if ($class && ! ($inputData[$i] ?? false) instanceof $class) { |
|
51
|
4 |
|
array_splice($inputData, $i, 0, [self::$app[$class]]); |
|
52
|
3 |
|
} elseif (! array_key_exists($i, $inputData) && $parameter->isDefaultValueAvailable()) { |
|
53
|
4 |
|
$inputData[] = $parameter->getDefaultValue(); |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
4 |
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: