/** * @return Builder\Class_ * @throws Exception\DomainException */ private function compileClass() { $definitions = $this->definitions; $class = $this->builderFactory->class($this->class)->extend('CompiledContainer'); $mapNodes = array(); foreach ($definitions as $definition) { if ($definition instanceof Definition\AliasDefinition) { $alias = Utils::resolveAlias($this->definitions, $definition, false); $mapNodes[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($alias->getIdentifier()), new Node\Scalar\String_($definition->getKey())); } else { if ($definition instanceof Definition\InterfaceDefinition) { // ignore } else { if (!$definition->isFactory()) { $class->addStmt($property = $this->builderFactory->property($definition->getIdentifier())->makePrivate()->setDocComment('/** * @var ' . $definition->getTypeHint() . ' */')); } $identifier = $definition->getIdentifier(); // Add method $class->addStmt($this->compileDefinition($definition)); // Add map entry $mapNodes[] = new Node\Expr\ArrayItem(new Node\Scalar\String_($identifier), new Node\Scalar\String_($definition->getKey())); } } } $class->addStmt($this->builderFactory->property('map')->makeProtected()->makeStatic()->setDefault(new Node\Expr\Array_($mapNodes))->setDocComment('/** * @var array */')); return $class; }
/** * @return string */ public function getIdentifier() { $name = $this->getName(); $class = $this->getClass(); if ($name) { return Utils::classToIdentifier($name); } else { return Utils::classToIdentifier($class); } }
/** * @inheritdoc */ public function compile() { $definition = $this->definition; $identifier = $definition->getIdentifier(); // Prepare method $method = $this->builderFactory->method($identifier)->makeProtected()->setDocComment('/** * @return ' . $definition->getTypeHint() . ' */'); // Prepare instance check $prop = null; if (!$definition->isFactory()) { $method->addStmt($this->makeSingletonCheck()); $prop = new Node\Expr\PropertyFetch(new Node\Expr\Variable('this'), $identifier); } // Prepare method body $providerDefinition = Utils::resolveAlias($this->definitions, Utils::resolveDefinition($this->definitions, $definition->getProvider())); $fetch = new Node\Expr\MethodCall(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), $providerDefinition->getIdentifier()), 'get'); if (!$definition->isFactory()) { $method->addStmt(new Node\Stmt\Return_(new Node\Expr\Assign(clone $prop, $fetch))); } else { $method->addStmt(new Node\Stmt\Return_($fetch)); } return $method; }
/** * @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; }
/** * @param mixed $value * @return Node\Expr * @throws Exception\DomainException */ protected function compileValue($value) { return Utils::parserNodeFromValue($value); }
private function executeModules() { foreach ($this->modules as $module) { if (is_string($module)) { $module = new $module(); } if (!$module instanceof Module) { throw new Exception\DomainException('Module must implement zdi\\Module, was: ' . Utils::varInfo($module)); } $module->define($this); } }
private function makeParam(Param $param) { if ($param instanceof Param\NamedParam) { return $this->get($param->getName()); } else { if ($param instanceof Param\ClassParam) { if ($param->isOptional() && !$this->has($param->getClass())) { return null; } else { if (is_a($this, $param->getClass())) { return $this; } else { return $this->get($param->getClass()); } } } else { if ($param instanceof Param\ValueParam) { return $param->getValue(); } else { if ($param instanceof Param\UnresolvedParam) { $definition = Utils::resolveGlobalKey($this->definitions, $param->getName()); return $this->get($definition->getKey()); } else { if ($param instanceof Param\InjectionPointParam) { if (count($this->stack) < 2) { return new InjectionPoint(); } else { return $this->stack[count($this->stack) - 2]; } } else { throw new Exception\DomainException("Unsupported param: " . Utils::varInfo($param)); } } } } } }
/** * @param $param * @return Param * @throws Exception\DomainException */ private function convertParam($param) { if (is_string($param)) { return new Param\NamedParam($param); } else { if ($param instanceof Param) { return $param; } else { throw new Exception\DomainException("Invalid parameter: " . Utils::varInfo($param)); } } }
public function testExtractNamespace() { $this->assertSame(array(null, \stdClass::class), Utils::extractNamespace(\stdClass::class)); $this->assertSame(array('Foo', 'Bar'), Utils::extractNamespace('Foo\\Bar')); }