/** * @param Param $param * @param InjectionPoint $ip * @return Node\Expr * @throws Exception\DomainException */ protected function compileParam(Param $param, InjectionPoint $ip) { if ($param instanceof Param\ClassParam) { $key = $param->getClass(); // Just return this if it's asking for a container if ($key == Container::class) { return new Node\Expr\Variable('this'); } // Get definition $definition = Utils::resolveAliasKey($this->definitions, $key, $param->isOptional()); if ($definition) { return $this->compileParamInjectionPoint($definition, $ip); } else { return new Node\Expr\ConstFetch(new Node\Name('null')); } } else { if ($param instanceof Param\NamedParam) { $definition = Utils::resolveAliasKey($this->definitions, $param->getName(), true); if ($definition) { return $this->compileParamInjectionPoint($definition, $ip); } else { return new Node\Expr\MethodCall(new Node\Expr\Variable('this'), 'get', array(new Node\Arg(new Node\Scalar\String_($param->getName())))); } } else { if ($param instanceof Param\ValueParam) { return new Node\Arg($this->compileValue($param->getValue())); } else { if ($param instanceof Param\UnresolvedParam) { $definition = Utils::resolveGlobalKey($this->definitions, $param->getName(), true); if (!$definition) { throw new Exception\OutOfBoundsException("Undefined identifier: " . $param->getName() . ' for definition: ' . $this->definition->getKey()); } return $this->compileParamInjectionPoint($definition, $ip); } else { if ($param instanceof Param\InjectionPointParam) { return new Node\Expr\ConstFetch(new Node\Name('WHOOPSIES')); } else { throw new Exception\DomainException('Unsupported parameter: ' . Utils::varInfo($param) . ' for definition: ' . $this->definition->getKey()); } } } } } }
/** * @inheritdoc */ public function get($key) { // Already made if (isset($this->values[$key])) { return $this->values[$key]; } // Lookup definition $definition = Utils::resolveAliasKey($this->definitions, $key); // Make injection point $ip = new InjectionPoint(); $ip->class = $definition->getClass(); array_push($this->stack, $ip); try { // Build the parameters if ($definition instanceof Definition\DataDefinition) { $object = $this->makeDefault($definition, $ip); } else { if ($definition instanceof Definition\ClosureDefinition) { $object = $this->makeClosure($definition, $ip); } else { if ($definition instanceof Definition\ClassDefinition) { $object = $this->makeProvider($definition, $ip); } else { throw new Exception\DomainException('Unsupported definition: ' . Utils::varInfo($definition)); } } } // Check for injection interfaces if (is_object($object)) { $this->injectInterfaces($object, $ip); } if (!$definition->isFactory()) { $this->values[$key] = $object; } return $object; } finally { array_pop($this->stack); } }
/** * @param Definition[] $definitions * @param string $key * @param boolean $isOptional * @return Definition */ public static function resolveGlobalKey(array $definitions, $key, $isOptional = false) { $definition = Utils::resolveAliasKey($definitions, $key, true); if ($definition && $definition->isGlobal()) { return $definition; } else { if (!$isOptional) { throw new Exception\OutOfBoundsException("Undefined identifier: " . $key); } } return null; }