1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace Mvc5\Resolver; |
7
|
|
|
|
8
|
|
|
use Mvc5\Exception; |
9
|
|
|
|
10
|
|
|
class Builder |
11
|
|
|
extends \ReflectionClass |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var array|self[] |
15
|
|
|
*/ |
16
|
|
|
protected static $class = []; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var \ReflectionMethod |
20
|
|
|
*/ |
21
|
|
|
protected $constructor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var \ReflectionParameter[] |
25
|
|
|
*/ |
26
|
|
|
protected $params; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $name |
30
|
|
|
* @param array $args |
31
|
|
|
* @param callable $callback |
32
|
|
|
* @return object |
33
|
|
|
*/ |
34
|
|
|
static function create($name, array $args, callable $callback) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
$class = static::reflectionClass($name); |
37
|
|
|
|
38
|
|
|
if (!$class->constructor()) { |
39
|
|
|
return $class->newInstanceWithoutConstructor(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
if ($args && !is_string(key($args))) { |
|
|
|
|
43
|
|
|
return new $name(...$args); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$matched = []; |
47
|
|
|
$params = $class->params(); |
48
|
|
|
|
49
|
|
|
foreach($params as $param) { |
50
|
|
|
if (isset($args[$param->name])) { |
51
|
|
|
$matched[] = $args[$param->name]; |
52
|
|
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ($param->isOptional()) { |
56
|
|
|
$param->isDefaultValueAvailable() && |
57
|
|
|
$matched[] = $param->getDefaultValue(); |
58
|
|
|
continue; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
if (null !== ($hint = $param->getClass()) && null !== $match = $callback($hint->name)) { |
62
|
|
|
$matched[] = $match; |
63
|
|
|
continue; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (null !== $match = $callback($param->name)) { |
67
|
|
|
$matched[] = $match; |
68
|
|
|
continue; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
Exception::runtime('Missing required parameter $' . $param->name . ' for ' . $name); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return new $name(...($params ? $matched : array_values($args))); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return bool|\ReflectionMethod |
79
|
|
|
*/ |
80
|
|
|
protected function constructor() |
81
|
|
|
{ |
82
|
|
|
return $this->constructor ?? $this->constructor = (parent::getConstructor() ?: false); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param \ReflectionMethod|null $method |
87
|
|
|
* @return array|\ReflectionParameter[] |
88
|
|
|
*/ |
89
|
|
|
protected function constructorParams($method) |
90
|
|
|
{ |
91
|
|
|
return $method ? $method->getParameters() : []; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return \ReflectionParameter[] |
96
|
|
|
*/ |
97
|
|
|
protected function params() |
98
|
|
|
{ |
99
|
|
|
return $this->params ?? $this->params = $this->constructorParams($this->constructor()); |
|
|
|
|
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param $name |
104
|
|
|
* @return self |
105
|
|
|
*/ |
106
|
|
|
static function reflectionClass($name) |
|
|
|
|
107
|
|
|
{ |
108
|
|
|
return static::$class[$name] ?? (static::$class[$name] = new self($name)); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.