/** * @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; }
/** * Convert a ClassModel into an array of statements for the php parser * * @param ClassModel $classModel * @return array */ public function transform(ClassModel $classModel) { // create namespace $namespace = $this->factory->namespace($classModel->getNamespace()); // create class $class = $this->factory->class($classModel->getName()); $class->implement($classModel->getInterface()); // add class properties foreach ($classModel->getProperties() as $propertyModel) { $property = $this->factory->property($propertyModel->getName()); $property->setDefault($propertyModel->getDefaultValue()); switch ($propertyModel->getVisibility()) { case PropertyModel::VISIBILITY_PUBLIC: $property->makePublic(); break; case PropertyModel::VISIBILITY_PROTECTED: $property->makeProtected(); break; case PropertyModel::VISIBILITY_PRIVATE: $property->makePrivate(); break; default: throw new UnexpectedValueException('Property visibility must be public, protected, private'); } // add property to class $class->addStmt($property); } // add class methods foreach ($classModel->getMethods() as $methodModel) { $method = $this->factory->method($methodModel->getName()); $method->makePublic(); // add method body $body = '<?php ' . $methodModel->getBody(); $methodStatements = $this->parser->parse($body); if (null !== $methodStatements) { $methodStatements = $this->nodeTraverser->traverse($methodStatements); $method->addStmts($methodStatements); } // add method parameters foreach ($methodModel->getParameters() as $parameterModel) { $parameter = $this->factory->param($parameterModel->getName()); if ($parameterModel->hasTypeHint()) { $parameter->setTypeHint($parameterModel->getTypeHint()); } if ($parameterModel->isOptional()) { $parameter->setDefault($parameterModel->getDefaultValue()); } $method->addParam($parameter); } // add method to class $class->addStmt($method); } // add class to namespace $namespace->addStmt($class); // return an array of statements return [$namespace->getNode()]; }
public function testIntegration() { $factory = new BuilderFactory(); $node = $factory->namespace('Name\\Space')->addStmt($factory->use('Foo\\Bar\\SomeOtherClass'))->addStmt($factory->use('Foo\\Bar')->as('A'))->addStmt($factory->class('SomeClass')->extend('SomeOtherClass')->implement('A\\Few', '\\Interfaces')->makeAbstract()->addStmt($factory->method('firstMethod'))->addStmt($factory->method('someMethod')->makePublic()->makeAbstract()->addParam($factory->param('someParam')->setTypeHint('SomeClass'))->setDocComment('/** * This method does something. * * @param SomeClass And takes a parameter */'))->addStmt($factory->method('anotherMethod')->makeProtected()->addParam($factory->param('someParam')->setDefault('test'))->addStmt(new Expr\Print_(new Expr\Variable('someParam'))))->addStmt($factory->property('someProperty')->makeProtected())->addStmt($factory->property('anotherProperty')->makePrivate()->setDefault(array(1, 2, 3))))->getNode(); $expected = <<<'EOC' <?php namespace Name\Space; use Foo\Bar\SomeOtherClass; use Foo\Bar as A; abstract class SomeClass extends SomeOtherClass implements A\Few, \Interfaces { protected $someProperty; private $anotherProperty = array(1, 2, 3); function firstMethod() { } /** * This method does something. * * @param SomeClass And takes a parameter */ public abstract function someMethod(SomeClass $someParam); protected function anotherMethod($someParam = 'test') { print $someParam; } } EOC; $stmts = array($node); $prettyPrinter = new PrettyPrinter\Standard(); $generated = $prettyPrinter->prettyPrintFile($stmts); $this->assertEquals(str_replace("\r\n", "\n", $expected), str_replace("\r\n", "\n", $generated)); }
/** * Function to put routes in PHP Classes * @return type */ private function createClass() { $fs = new Filesystem(); $directory = $this->getPathPHP(); $path = $this->getPathClass(); if (!$fs->exists($directory)) { $fs->mkdir($directory); } $routes = $this->routes; $factory = new BuilderFactory(); $node = $factory->namespace('Route')->addStmt($factory->class('RouteArray')->addStmt($factory->property('_routes')->makePrivate()->setDefault($routes))->addStmt($factory->method('getRoutes')->makePublic()->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('this->_routes')))))->getNode(); $stmts = array($node); $prettyPrinter = new PrettyPrinter\Standard(); $php = $prettyPrinter->prettyPrintFile($stmts); file_put_contents($path, $php); }
public function sample() { $factory = new BuilderFactory(); $node = $factory->namespace('name\\space')->addStmt($factory->class('Sample')->addStmt($factory->property('string')->makeProtected()->setDocComment('/** * @var string String */'))->addStmt($factory->method('get')->makePublic()->setDocComment('/** * Return string * * @return string String */')->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('this->string'))))->addStmt($factory->method('set')->makePublic()->setDocComment('/** * Set string * * @param string $string String * @return $this */')->addParam(new Node\Param('string'))->addStmt(new Node\Name('$this->string = $string;'))->addStmt(new Node\Stmt\Return_(new Node\Expr\Variable('this')))))->getNode(); $stmts = array($node); $prettyPrinter = new PrettyPrinter\Standard(); $code = $prettyPrinter->prettyPrintFile($stmts); file_put_contents('tmp/origin/PHPParser.php', (string) $code); }
/** * @param Array_ $values * @param BuilderFactory $factory * * @return Property */ private function createValueField(BuilderFactory $factory, Array_ $values) { return $factory->property('values')->makePrivate()->makeStatic()->setDefault($values); }