10 private string $className;
11 private \Closure $constructor;
13 private $externalNameConstructor;
15 public function __construct(
string $className, \Closure $constructor, \Closure $externalNameConstructor =
null)
18 ->setClassName($className)
19 ->setConstructor($constructor)
22 $reflectionFunction = new \ReflectionFunction($constructor);
23 if ($reflectionFunction->getNumberOfParameters() > 1)
25 if ($externalNameConstructor ===
null)
27 $externalNameConstructor =
static function(\ReflectionParameter $parameter){
28 return $parameter->getName() .
'Id';
50 if (!$reflectionClass)
55 $paramsToInvoke = array_merge(
56 [$reflectionClass->getName()],
67 protected function callConstructor(\Closure $constructor, array $params, $newThis =
null)
69 if ($newThis && $this->isBindable($constructor))
71 $constructor->bindTo($newThis);
74 return call_user_func_array($constructor, $params);
77 private function isBindable(\Closure $closure): bool
79 $reflectionClosure = new \ReflectionFunction($closure);
80 $isBindable = ($reflectionClosure->getClosureThis() !==
null || $reflectionClosure->getClosureScopeClass() ===
null);
85 public function captureData(\ReflectionParameter $parameter, array $sourceParameters, array $autoWiredParameters = [])
98 $result->addError(
new Error(
"Could not find value for {{$externalName}}"));
113 foreach ($sourceParameters as $source)
115 if (isset($source[$name]))
117 return $source[$name];
120 if (($source instanceof \ArrayAccess) && $source->offsetExists($name))
122 return $source[$name];
125 if (is_array($source) && array_key_exists($name, $source))
127 return $source[$name];
137 $namedType = $parameter->getType();
138 if (!($namedType instanceof \ReflectionNamedType))
142 if ($namedType->isBuiltin())
147 return new \ReflectionClass($namedType->getName());
150 public function match(\ReflectionParameter $parameter)
169 return $this->className;
179 $this->className = $className;
189 return $this->constructor;
199 $this->constructor = $constructor;
209 return $this->externalNameConstructor;
219 $this->externalNameConstructor = $externalNameConstructor;
226 return $this->externalNameConstructor !==
null;
match(\ReflectionParameter $parameter)
callConstructor(\Closure $constructor, array $params, $newThis=null)
constructValue(\ReflectionParameter $parameter, Result $captureResult, $newThis=null)
captureData(\ReflectionParameter $parameter, array $sourceParameters, array $autoWiredParameters=[])
buildReflectionClass(\ReflectionParameter $parameter)
findParameterInSourceList($name, array $sourceParameters, &$status)
setExternalNameConstructor(\Closure $externalNameConstructor)
__construct(string $className, \Closure $constructor, \Closure $externalNameConstructor=null)
getExternalNameConstructor()
setConstructor(\Closure $constructor)