open-code-modeling/php-code-ast
最新稳定版本:0.12.3
Composer 安装命令:
composer require open-code-modeling/php-code-ast
包简介
Open Code Modeling PHP Code AST
README 文档
README
PHP code generation based on AST.
Installation
$ composer require open-code-modeling/php-code-ast
Usage
See unit tests in
testsfolder for comprehensive examples.
Let's start with a straightforward example of generating a class with the ClassBuilder:
<?php $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7); $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); $ast = $parser->parse(''); $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromScratch('TestClass', 'My\\Awesome\\Service'); $classBuilder ->setFinal(true) ->setExtends('BaseClass') ->setNamespaceImports('Foo\\Bar') ->setImplements('\\Iterator', 'Bar'); $nodeTraverser = new PhpParser\NodeTraverser(); $classBuilder->injectVisitors($nodeTraverser, $parser); print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
Will print the following output:
<?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { }
All this is done via PHP AST and it checks if the AST token is already defined. So it will not override your code.
You can also use the code and PHP AST visitor classes to generate code via the low level API.
To add a method toInt() to the class above you can add this via the ClassMethod visitor.
<?php $method = new OpenCodeModeling\CodeAst\Code\MethodGenerator( 'toInt', [], OpenCodeModeling\CodeAst\Code\MethodGenerator::FLAG_PUBLIC, new OpenCodeModeling\CodeAst\Code\BodyGenerator($this->parser, 'return $this->myValue;') ); $method->setReturnType('int'); $nodeTraverser->addVisitor(new OpenCodeModeling\CodeAst\NodeVisitor\ClassMethod($method)); print_r($printer->prettyPrintFile($nodeTraverser->traverse($ast)));
This will print the following output.
<?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { public function toInt() : int { return $this->myValue; } }
Now, change the body of the toInt() method to something else. You will see that your changes will NOT be overwritten.
Reverse usage
It is also possible to create a factory class from parsed PHP AST. You can create an instance of
OpenCodeModeling\CodeAst\Builder\ClassBuilder by calling OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes().
<?php $parser = (new PhpParser\ParserFactory())->create(PhpParser\ParserFactory::ONLY_PHP7); $printer = new PhpParser\PrettyPrinter\Standard(['shortArraySyntax' => true]); $expected = <<<'EOF' <?php declare (strict_types=1); namespace My\Awesome\Service; use Foo\Bar; final class TestClass extends BaseClass implements \Iterator, Bar { private const PRIV = 'private'; } EOF; $ast = $parser->parse($expected); $classBuilder = OpenCodeModeling\CodeAst\Builder\ClassBuilder::fromNodes(...$ast); $classBuilder->getName(); // TestClass $classBuilder->getExtends(); // BaseClass $classBuilder->isFinal(); // true $classBuilder->isStrict(); // true $classBuilder->isAbstract(); // false
统计信息
- 总下载量: 1.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 1
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-07