Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
exactparameter.php
1<?php
2
4
7
9{
10 private string $parameterName;
11
12 public function __construct(string $className, string $parameterName, \Closure $constructor)
13 {
14 if (!$this->validateConstructor($constructor))
15 {
16 throw new BinderArgumentException('$constructor closure must have one argument to bind class name.');
17 }
18
19 parent::__construct($className, $constructor);
20 $this->parameterName = $parameterName;
21 }
22
23 protected function validateConstructor(\Closure $constructor)
24 {
25 $reflectionFunction = new \ReflectionFunction($constructor);
26
27 return $reflectionFunction->getNumberOfParameters() >= 1;
28 }
29
30 public function constructValue(\ReflectionParameter $parameter, Result $captureResult, $newThis = null)
31 {
32 return $this->callConstructor(
33 $this->getConstructor(),
34 $captureResult->getData(),
35 $newThis,
36 );
37 }
38
39 public function captureData(\ReflectionParameter $parameter, array $sourceParameters, array $autoWiredParameters = [])
40 {
41 $result = new Result();
42
43 if (!$this->needToMapExternalData())
44 {
45 return $result;
46 }
47
48 $binder = Binder::buildForFunction($this->getConstructor());
49 $binder->setAutoWiredParameters($autoWiredParameters);
50
51 $reflectionClass = $this->buildReflectionClass($parameter);
52 if (!$reflectionClass)
53 {
54 throw new BinderArgumentException("Could not retrieve \\ReflectionClass for {$parameter->getName()}.");
55 }
56
57 array_unshift($sourceParameters, ['className' => $reflectionClass->getName()]);
58 $binder->setSourcesParametersToMap($sourceParameters);
59 try
60 {
61 $capturedParameters = $binder->getArgs();
62 $result->setData($capturedParameters);
63 }
65 {
66 $result->addError(new Error($e->getMessage()));
67 }
68
69 return $result;
70 }
71
72 public function match(\ReflectionParameter $parameter)
73 {
74 return
75 parent::match($parameter) &&
76 $parameter->getName() === $this->getParameterName()
77 ;
78 }
79
83 public function getParameterName()
84 {
85 return $this->parameterName;
86 }
87
88 public function getPriority()
89 {
90 return parent::getPriority() + 1;
91 }
92}
static buildForFunction($callable, $configuration=[])
Definition binder.php:44
match(\ReflectionParameter $parameter)
constructValue(\ReflectionParameter $parameter, Result $captureResult, $newThis=null)
captureData(\ReflectionParameter $parameter, array $sourceParameters, array $autoWiredParameters=[])
__construct(string $className, string $parameterName, \Closure $constructor)
callConstructor(\Closure $constructor, array $params, $newThis=null)
Definition parameter.php:67
buildReflectionClass(\ReflectionParameter $parameter)