private function createClass($commande, $description) { $output = $this->_output; $root = ROOT . DS; $app = $root . 'app' . DS . "Application"; $lib = $root . 'library' . DS . "commands.php"; // create command name $name = S::create($commande)->replace(':', ' ')->toTitleCase()->replace(' ', '')->append("Command")->__toString(); // create FQN $fqn = "Application\\Commands\\" . $name; // check avaibality // load commands.php file $code = file_get_contents($lib); $parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP5); $prettyPrinter = new PrettyPrinter\Standard(); $stmts = $parser->parse($code); foreach ($stmts[0]->expr as $express) { $tmp = $express[0]->value->value; if (S::create($tmp)->humanize()->__toString() == S::create($fqn)->humanize()->__toString()) { $output->writeln("This command already exists in commands.php"); die; } } // commands not exists add it to commands.php $nb = count($stmts[0]->expr->items); $ligne = 4 + $nb; $attributes = array("startLine" => $ligne, "endLine" => $ligne, "kind" => 2); $obj = new \PhpParser\Node\Expr\ArrayItem(new \PhpParser\Node\Scalar\String_($fqn, $attributes), null, false, $attributes); array_push($stmts[0]->expr->items, $obj); $code = $prettyPrinter->prettyPrint($stmts); $code = "<?php \r\n" . $code; $output->writeln("Create FQN commande " . $fqn); $path = $app . DS . "Commands" . DS . $name . ".php"; $arg1 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_($commande)); $arg2 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_($description)); $arg3 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_('Start process')); $arg4 = new \PhpParser\Node\Arg(new \PhpParser\Node\Scalar\String_('Finished')); $factory = new BuilderFactory(); $node = $factory->namespace('Application\\Commands')->addStmt($factory->use('Symfony\\Component\\Console\\Command\\Command'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputArgument'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputInterface'))->addStmt($factory->use('Symfony\\Component\\Console\\Input\\InputOption'))->addStmt($factory->use('Symfony\\Component\\Console\\Output\\OutputInterface'))->addStmt($factory->class($name)->extend('Command')->addStmt($factory->method('configure')->makeProtected()->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), "setName", array($arg1)))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), "setDescription", array($arg2))))->addStmt($factory->method('execute')->makeProtected()->addParam($factory->param('input')->setTypeHint('InputInterface'))->addParam($factory->param('output')->setTypeHint('OutputInterface'))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('output'), "writeln", array($arg3)))->addStmt(new Node\Expr\MethodCall(new Node\Expr\Variable('output'), "writeln", array($arg4)))))->getNode(); $stmts = array($node); $prettyPrinter = new PrettyPrinter\Standard(); $php = $prettyPrinter->prettyPrintFile($stmts); file_put_contents($path, $php); $fs = new Filesystem(); // if file exists add command to commands.php if ($fs->exists($path)) { $output->writeln("File saved in " . $path); $output->writeln("Register command to console"); file_put_contents($lib, $code); } else { $output->writeln("File not created"); } }
public function generateNode() { $factory = new BuilderFactory(); $class = $factory->class($this->_interfaceApiDefinition->name)->extend('AbstractInterface'); foreach ($this->_interfaceApiDefinition->methods as $methodApiDefinition) { $docComment = '/**' . "\n" . ' * ' . implode('/', array('', $this->_interfaceApiDefinition->name, $methodApiDefinition->name, 'v' . $methodApiDefinition->version, '')) . "\n" . ' *' . "\n"; $methodParams = array(); $arrayItems = array(); foreach ($methodApiDefinition->parameters as $parameterApiDefinition) { $parameterName = $this->_escapeVariableName($parameterApiDefinition->name); if ($parameterApiDefinition->name !== 'key') { $methodParam = $factory->param($parameterName); if ($parameterApiDefinition->optional !== false) { $methodParam->setDefault(null); } $methodParams[] = $methodParam; $arrayItems[] = new Node\Expr\ArrayItem(new Node\Expr\Variable($parameterName), new Node\Scalar\String_($parameterApiDefinition->name)); $docComment .= ' * @param ' . $parameterApiDefinition->type . ' $' . $parameterName; if (isset($parameterApiDefinition->description) && $parameterApiDefinition->description !== '') { $docComment .= ' ' . $parameterApiDefinition->description; } $docComment .= "\n"; } } $docComment .= ' */'; $method = $factory->method($methodApiDefinition->name . 'V' . $methodApiDefinition->version)->makePublic()->addStmt(new Node\Stmt\Return_(new Node\Expr\MethodCall(new Node\Expr\Variable('this'), '_call', array(new Node\Scalar\MagicConst\Method(), new Node\Scalar\String_($methodApiDefinition->httpmethod), new Node\Expr\Array_($arrayItems))))); foreach ($methodParams as $methodParam) { $method->addParam($methodParam); } $method->setDocComment($docComment); $class->addStmt($method); } $node = $factory->namespace(self::INTERFACE_NAMESPACE)->addStmt($factory->use('Zyberspace\\SteamWebApi\\AbstractInterface'))->addStmt($class)->getNode(); return $node; }
/** * 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)); }
/** * @param $className * @param $factory * * @return Method */ private function createGetValueMethod(BuilderFactory $factory, $className) { return $factory->method('getValue')->makePublic()->makeStatic()->addParam($factory->param('key'))->addParam($factory->param('version'))->addStmts([new Return_(new ArrayDimFetch(new ArrayDimFetch(new StaticPropertyFetch(new Name($className), 'values'), new Variable('version')), new Variable('key')))]); }