• Модуль: main
  • Путь к файлу: ~/bitrix/modules/main/lib/engine/autowire/binder.php
  • Класс: Bitrix\Main\Engine\AutoWire\Binder
  • Вызов: Binder::getParameterValue
private function getParameterValue(\ReflectionParameter $parameter)
{
	$sourceParameters = $this->getSourcesParametersToMap();

	$reflectionType = $parameter->getType();
	if (
		($reflectionType instanceof \ReflectionUnionType)
		|| ($reflectionType instanceof \ReflectionIntersectionType)
	)
	{
		throw new BinderArgumentException(
			"Currently there is no support for union or intersection types {{$parameter->getName()}}",
			$parameter
		);
	}

	if (($reflectionType instanceof \ReflectionNamedType) && !$reflectionType->isBuiltin())
	{
		foreach ($this->getAutoWiredByClass($parameter) as $autoWireParameter)
		{
			$result = $autoWireParameter->captureData($parameter, $sourceParameters, $this->getAllAutoWiredParameters());
			if (!$result->isSuccess())
			{
				continue;
			}

			$constructedValue = null;
			$constructResult = $this->constructValue($parameter, $autoWireParameter, $result);
			if ($constructResult->isSuccess())
			{
				['value' => $constructedValue] = $constructResult->getData();
			}

			if ($constructedValue === null)
			{
				if ($parameter->isDefaultValueAvailable())
				{
					return $parameter->getDefaultValue();
				}

				throw new BinderArgumentException(
					"Could not construct parameter {{$parameter->getName()}}",
					$parameter,
					$constructResult->getErrors(),
				);
			}

			return $constructedValue;
		}

		if ($parameter->isDefaultValueAvailable())
		{
			return $parameter->getDefaultValue();
		}

		$exceptionMessage = "Could not find value for parameter to build auto wired argument {{$reflectionType->getName()} \${$parameter->getName()}}";
		if (isset($result) && ($result instanceof Result) && $result->getErrorMessages())
		{
			$exceptionMessage = $result->getErrorMessages()[0];
		}

		throw new BinderArgumentException(
			$exceptionMessage,
			$parameter
		);
	}

	$value = $this->findParameterInSourceList($parameter->getName(), $status);
	if ($status === self::STATUS_NOT_FOUND)
	{
		if ($parameter->isDefaultValueAvailable())
		{
			return $parameter->getDefaultValue();
		}

		throw new BinderArgumentException(
			"Could not find value for parameter {{$parameter->getName()}}",
			$parameter
		);
	}
	if ($reflectionType instanceof \ReflectionNamedType)
	{
		$declarationChecker = new TypeDeclarationChecker($reflectionType, $value);
		if (!$declarationChecker->isSatisfied())
		{
			throw new BinderArgumentException(
				"Invalid value {{$value}} to match with parameter {{$parameter->getName()}}. Should be value of type {$reflectionType->getName()}.",
				$parameter
			);
		}
	}

	if ($parameter->isArray())
	{
		$value = (array)$value;
	}

	return $value;
}