open-code-modeling/php-code-ast
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
open-code-modeling/php-code-ast 适用场景与选型建议
open-code-modeling/php-code-ast 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.24k 次下载、GitHub Stars 达 7, 最近一次更新时间为 2020 年 08 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 open-code-modeling/php-code-ast 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 open-code-modeling/php-code-ast 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 1.24k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 7
- 点击次数: 15
- 依赖项目数: 4
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-08-07