/** * @test */ public function The_ServiceDefinition_makes_all_constructor_parameters_available_through_methods() { // given $file = '/my/path'; $line = 42; $IdentifyingType = Type::fromName(__CLASS__); $ImplementingType = Type::fromName(__CLASS__); $FactoryFunction = function () { }; $InitFunction = function () { }; $Target = new ServiceDefinition($file, $line, IdentifyingType::is($IdentifyingType), ImplementingType::is($ImplementingType), FactoryFunction::is($FactoryFunction), InitFunction::is($InitFunction)); // when, then $this->AssertEquals($file, $Target->getDefiningFilePath()); $this->AssertEquals($line, $Target->getDefiningLineNumber()); $this->AssertSame($IdentifyingType, $Target->getIdentifyingType()); $this->AssertSame($ImplementingType, $Target->getImplementingType()); $this->AssertSame($FactoryFunction, $Target->getFactoryFunction()); $this->AssertSame($InitFunction, $Target->getInitFunction()); $this->AssertTrue($Target->hasFactoryFunction()); $this->AssertTrue($Target->hasInitFunction()); }
/** * @param ServiceDefinition $ServiceDefinition * @param mixed $value * @throws InvalidServiceDefinition */ private function ensureValueImplementsIdentifyingType(ServiceDefinition $ServiceDefinition, $value) { $identifyingTypeName = $ServiceDefinition->getIdentifyingType()->getName(); if (is_object($value) && $value instanceof $identifyingTypeName) { return; } $message = 'The factory function returned ' . self::getValueInfo($value, $identifyingTypeName) . '. '; $message .= 'The factory function of a service must return an object that implements the identifying type.'; throw new InvalidServiceDefinition($ServiceDefinition, $message); }
/** * @param ServiceDefinition $ServiceDefinition * @param string $functionType * @param Type $RequiredType * @param string $parameterName */ private static function ensureFactoryFunctionDoesNotRequireTheServiceItself(ServiceDefinition $ServiceDefinition, $functionType, Type $RequiredType, $parameterName) { if ($functionType === 'init') { return; } if ($RequiredType == $ServiceDefinition->getIdentifyingType()) { $message = 'The type hint of the parameter $' . $parameterName . ' is ' . $RequiredType->getName() . '. '; $message .= 'A factory function cannot be injected with the service it is about to build.'; throw new InvalidServiceDefinition($ServiceDefinition, $message); } }
/** * @param ServiceDefinition $ServiceDefinition * @param string $specificMessage */ public function __construct(ServiceDefinition $ServiceDefinition, $specificMessage) { $message = sprintf("Invalid definition of the service %s in %s on line %d: %s", $ServiceDefinition->getIdentifyingType()->getName(), $ServiceDefinition->getDefiningFilePath(), $ServiceDefinition->getDefiningLineNumber(), $specificMessage); parent::__construct($message); }