phpactor / code-builder
此包已被废弃且不再维护。未建议任何替代包。
生成和修改源代码
0.4.3
2021-12-28 20:24 UTC
Requires
- php: ^7.3 || ^8.0
- microsoft/tolerant-php-parser: ~0.1.0
- phpactor/text-document: ~1.2.3
- phpactor/worse-reflection: ~0.4.7
- twig/twig: ^2.4
Requires (Dev)
- ergebnis/composer-normalize: ^2.0
- friendsofphp/php-cs-fixer: ^2.17
- phpactor/test-utils: ~1.1.3
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ~0.12.0
- phpunit/phpunit: ^9.0
- symfony/var-dumper: ^5.0
This package is auto-updated.
Last update: 2022-03-31 19:54:31 UTC
README
此库可用于使用原型 生成 或以幂等 添加 类、方法、属性、use 语句 等到现有源代码。
原型是一个定义结构化代码元素的对象。
使用方法
该库提供了一个源代码原型构建器
$builder = SourceBuilder::create() ->namespace('Animals'); ->use('Measurements\\Height'); ->class('Rabbits') ->extends('Leopridae') ->property('force') ->visibility('private') ->type('int') ->defaultValue(5) ->end() ->method('jump') ->parameters() ->parameter('how') ->default('high') ->type('Height') ->end(); ->end() ->end() ->end(); $sourcePrototype = $builder->build();
上述原型可以用来生成一个新类
$renderer = new TwigRenderer(); $renderer->render($sourcePrototype);
或者可以应用到现有源代码上,假设以下条件
<?php class Rabbits { }
当我们做
$updater = new TolerantUpdater(); $updater->apply($sourcePrototype, Code::fromString(file_get_contents('Rabbits.php')));
然后我们得到
<?php namespace Animals; use Measurements\Height; class Rabbits extends Leopridae { private $force = 5; public function jump(Height $how = 'high') { } }
关于此项目
此库是 phpactor 项目的组成部分。
贡献
此包是开源的,并欢迎贡献!请随意在此仓库中提交拉取请求。
支持
- 在主 Phpactor 仓库中创建一个问题。
- 加入 Slack 上的
#phpactor
频道,链接为 Symfony Devs。