popphp/pop-code
Composer 安装命令:
composer require popphp/pop-code
包简介
Pop Code Component for Pop PHP Framework
README 文档
README
Overview
pop-code provides the ability to dynamically generate PHP code on the fly
as well as parse and modify existing PHP code.
pop-code is a component of the Pop PHP Framework.
Install
Install pop-code using Composer.
composer require popphp/pop-code
Or, require it in your composer.json file
"require": {
"popphp/pop-view" : "^5.0.5"
}
Quickstart
Create a simple function
In this example, a function is created and rendered to a string:
use Pop\Code\Generator; $function = new Generator\FunctionGenerator('sayHello'); $function->addArgument('name', 'null', 'string'); $function->setBody("echo 'Hello ' . \$name;"); $function->addReturnType('void'); $function->setDesc('This is the first function'); echo $function;
/** * This is the first function * * @param string|null $name * @return void */ function sayHello(string|null $name = null): void { echo 'Hello ' . $name; }
Create a simple class
In this example, a class is created and saved to a file:
use Pop\Code\Generator; // Create the class object and give it a namespace $class = new Generator\ClassGenerator('MyClass'); $class->setNamespace(new Generator\NamespaceGenerator('MyApp')); // Create a new protected property with a default value $prop = new Generator\PropertyGenerator('foo', 'string', null, 'protected'); // Create a method and give it an argument, body and docblock description $method = new Generator\MethodGenerator('setFoo', 'public'); $method->addArgument('foo', null, 'string') ->setBody('$this->foo = $foo;') ->addReturnType('void') ->setDesc('This is the method to set foo.'); // Add the property and the method to the class code object $class->addProperty($prop); $class->addMethod($method); // Save the class to a file $code = new Generator($class); $code->writeToFile('MuClass.php');
The contents of the file will be:
<?php /** * @namespace */ namespace MyApp; class MyClass { /** * @var string */ protected string|null $foo = null; /** * This is the method to set foo. * * @param string $foo * @return void */ public function setFoo(string $foo): void { $this->foo = $foo; } }
Generate Code
There are a number of individual code generators available to manage the creation and output of various types of code blocks. Code generators are available for the following type of code:
- Classes
- Interfaces
- Traits
- Methods
- Functions
- Constants
- Properties
- Namespaces
- Docblocks
- Bodies (general blocks of code)
Create a file with some functions
use Pop\Code\Generator; $function1 = new Generator\FunctionGenerator('sayHello'); $function1->addArgument('name', null, 'string') ->setBody("echo 'Hello ' . \$name;") ->setDesc('This is the first function') ->addReturnType('void'); $function2 = new Generator\FunctionGenerator('sayGoodbye'); $function2->addArgument('name', null, 'string') ->setBody("echo 'Goodbye ' . \$name;") ->setDesc('This is the second function') ->addReturnType('void'); $code = new Generator(); $code->addCodeObjects([$function1, $function2]); $code->writeToFile('functions.php');
The above code will produce a file called functions.php with the following
code in it:
<?php /** * This is the first function * * @param string $name * @return void */ function sayHello(string $name): void { echo 'Hello ' . $name; } /** * This is the second function * * @param string $name * @return void */ function sayGoodbye(string $name): void { echo 'Goodbye ' . $name; }
Parse Code
This pop-code component also provides the ability to parse existing code, which is useful
to obtain information about the code or to even modify and save new code.
In this example, we use the class that we created above. The reflection object provides you with a code generator object like the one above so that you can add or remove things from the parsed code.
use Pop\Code\Reflection; use Pop\Code\Generator; $class = Reflection::createClass('MyApp\MyClass'); // Create the new method that you want to add to the existing class $method = new Generator\MethodGenerator('hasFoo', 'public'); $method->addArgument('foo', null, 'string') ->setBody('return ($this->foo !== null);') ->setDesc('This is the method to see if foo is set.') ->addReturnType('bool'); // Access the generator and it's code object to add the new method to it $class->addMethod($method); // Echo out the code $code = new Generator($class); $code->writeToFile('MyClass.php');
And the modified class will look like, complete with the new hasFoo() method:
<?php /** * @namespace */ namespace MyApp; class MyClass { /** * @var string */ protected string|null $foo = null; /** * This is the method to set foo. * * @param string $foo * @return void */ public function setFoo(string $foo): void { $this->foo = $foo; } /** * This is the method to see if foo is set. * * @param string $foo * @return bool */ public function hasFoo(string $foo): bool { return ($this->foo !== null); } }
popphp/pop-code 适用场景与选型建议
popphp/pop-code 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 7.82k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2014 年 12 月 07 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「code generation」 「pop」 「pop php」 「code reflection」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 popphp/pop-code 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 popphp/pop-code 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 popphp/pop-code 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Toolset for generating PHP code
Fabricates patterns.
PHP PDF library for generating and importing PDF documents. A component of the Pop PHP Framework
REST PDF Invoices for Magento 2
Common PHP filters for code generation
统计信息
- 总下载量: 7.82k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5
- 点击次数: 6
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2014-12-07