|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace MichaelRubel\EnhancedContainer\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Support\Arr; |
|
8
|
|
|
use MichaelRubel\EnhancedContainer\Exceptions\PropertyNotFoundException; |
|
9
|
|
|
|
|
10
|
|
|
trait HelpsProxies |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @param object|class-string $class |
|
14
|
|
|
* @param array $dependencies |
|
15
|
|
|
* @param string|null $context |
|
16
|
|
|
* |
|
17
|
|
|
* @return object |
|
18
|
|
|
*/ |
|
19
|
58 |
|
public function resolvePassedClass(object|string $class, array $dependencies = [], ?string $context = null): object |
|
20
|
|
|
{ |
|
21
|
58 |
|
if (is_object($class)) { |
|
22
|
15 |
|
return $class; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
57 |
|
if (! is_null($context) && isset(app()->contextual[$context])) { |
|
26
|
1 |
|
$class = app()->contextual[$context][$class] ?? $class; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
try { |
|
30
|
57 |
|
if (! empty($dependencies) && ! Arr::isAssoc($dependencies)) { |
|
31
|
2 |
|
$constructor = (new \ReflectionClass($class))->getConstructor(); |
|
32
|
|
|
|
|
33
|
2 |
|
if ($constructor) { |
|
34
|
2 |
|
$dependencies = $this->makeContainerParameters( |
|
35
|
2 |
|
$constructor->getParameters(), |
|
36
|
|
|
$dependencies |
|
37
|
|
|
); |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
57 |
|
return resolve($class, $dependencies); |
|
42
|
2 |
|
} catch (\Throwable $exception) { |
|
43
|
2 |
|
throw new \BadMethodCallException( |
|
44
|
2 |
|
$exception->getMessage() |
|
45
|
|
|
); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param object $class |
|
51
|
|
|
* @param string $method |
|
52
|
|
|
* @param array $parameters |
|
53
|
|
|
* |
|
54
|
|
|
* @return array |
|
55
|
|
|
* @throws \ReflectionException |
|
56
|
|
|
*/ |
|
57
|
47 |
|
public function getPassedParameters(object $class, string $method, array $parameters): array |
|
58
|
|
|
{ |
|
59
|
47 |
|
if (empty($parameters)) { |
|
60
|
24 |
|
return $parameters; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
25 |
|
$reflectionMethod = new \ReflectionMethod($class, $method); |
|
64
|
|
|
|
|
65
|
25 |
|
return $this->makeContainerParameters( |
|
66
|
25 |
|
$reflectionMethod->getParameters(), |
|
67
|
|
|
$parameters |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Combine parameters to make it container-readable. |
|
73
|
|
|
* |
|
74
|
|
|
* @param array $parameters |
|
75
|
|
|
* @param array $toCombine |
|
76
|
|
|
* |
|
77
|
|
|
* @return array |
|
78
|
|
|
*/ |
|
79
|
26 |
|
public function makeContainerParameters(array $parameters, array $toCombine): array |
|
80
|
|
|
{ |
|
81
|
26 |
|
return collect($parameters) |
|
82
|
26 |
|
->slice(0, count($toCombine)) |
|
83
|
26 |
|
->map(fn ($param) => $param->getName()) |
|
84
|
26 |
|
->combine(array_slice($toCombine, 0, count($parameters))) |
|
85
|
26 |
|
->all(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Convert the object to its namespace. |
|
90
|
|
|
* |
|
91
|
|
|
* @param object|string $object |
|
92
|
|
|
* |
|
93
|
|
|
* @return string |
|
94
|
|
|
*/ |
|
95
|
66 |
|
public function convertToNamespace(object|string $object): string |
|
96
|
|
|
{ |
|
97
|
66 |
|
return is_string($object) |
|
98
|
53 |
|
? $object |
|
99
|
66 |
|
: $object::class; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Handle property error. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $name |
|
106
|
|
|
* @param object $instance |
|
107
|
|
|
* |
|
108
|
|
|
* @throws PropertyNotFoundException |
|
109
|
|
|
*/ |
|
110
|
3 |
|
public function throwPropertyNotFoundException(string $name, object $instance): self |
|
111
|
|
|
{ |
|
112
|
3 |
|
throw new PropertyNotFoundException(sprintf( |
|
113
|
3 |
|
'Call to undefined property %s::%s()', |
|
114
|
|
|
$name, |
|
115
|
3 |
|
$instance::class |
|
116
|
|
|
)); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|