/** * @param \EBT\ExtensionBuilder\Domain\Model\ClassObject\ClassObject $classObject * @param bool $skipStatements * @return \PhpParser\Node\Stmt\Class_ */ public function buildClassNode($classObject, $skipStatements = false) { $factory = new \PhpParser\BuilderFactory(); $classNodeBuilder = $factory->class($classObject->getName()); if ($classObject->getParentClassName()) { $classNodeBuilder->extend(self::buildNodeFromName($classObject->getParentClassName())); } $interfaceNames = $classObject->getInterfaceNames(); if (count($interfaceNames) > 0) { call_user_func_array(array($classNodeBuilder, 'implement'), $interfaceNames); } if (!$skipStatements) { $stmts = array(); $properties = array(); $methods = array(); foreach ($classObject->getUseTraitStatement() as $statement) { $stmts[] = $statement; } foreach ($classObject->getMethods() as $method) { $methods[$method->getName()] = $this->buildMethodNode($method); } foreach ($classObject->getProperties() as $property) { $properties[$property->getName()] = $this->buildPropertyNode($property); } $constants = $classObject->getConstants(); if (is_array($constants)) { foreach ($constants as $name => $value) { $stmts[] = self::buildClassConstantNode($name, $value); } } foreach ($properties as $property) { $stmts[] = $property; } foreach ($methods as $method) { $stmts[] = $method; } $classNodeBuilder->addStmts($stmts); } $classNode = $classNodeBuilder->getNode(); $classNode->type = $classObject->getModifiers(); $this->addCommentAttributes($classObject, $classNode); return $classNode; }
/** * @param ControllerSpec $spec * @return \PhpParser\Node[] */ private function doGenerate(ControllerSpec $spec) { $factory = new \PhpParser\BuilderFactory(); $stmts = array(); $stmts[] = $factory->namespace(array($spec->getBundle()->getNamespace(), 'Controller')); $class = $factory->class($spec->getName() . 'Controller'); if ($spec->getBaseClass()) { $stmts[] = $factory->use($spec->getBaseClass()); $class->extend(new Name(array_slice($spec->getBaseClass(), -1, 1))); if ($spec->getOptions()->isStrictOOP()) { $class->makeFinal(); } } foreach ($spec->getActions() as $action) { $class->addStmt($factory->method($action . 'Action')); } $stmts[] = $class; $nodes = array_map(function ($stmt) { return $stmt->getNode(); }, $stmts); return $nodes; }