rice / ctl
PHP 控制器
v1.1.5
2023-07-26 12:25 UTC
Requires
- ext-json: *
- friendsofphp/php-cs-fixer: ^3.13
- phpunit/phpunit: ^9.5
- symfony/console: ^5.4
README
php 命令行(php 控制器)
安装
composer require rice/ctl
功能点
访问器自动生成注释
以这个 tests\Support\Annotation\Cat.php
文件为例,我们使用了 Accessor
这个 trait
。因此会存在 setxxx()
和 getxxx()
,但是实例化类后调用没有相关函数提示。为了解决这个问题,可以使用 php ctl.php rice:accessor xxx\tests\Support\Annotation\Cat.php
来执行自动生成注释。
只生成受保护的属性的注释,如果属性没有指定类型,则会查看注释中是否有 @var 指定相关类型,如果有则自动获取
生成前:
class Cat { use AutoFillProperties; use Accessor; /** * 眼睛. * * @return $this * * @throws \Exception * * @var string * @Param $class */ protected $eyes; /** * @var S */ protected $speak; }
生成后:
/** * Class Cat. * @method self setEyes(string $value) * @method string getEyes() * @method self setSpeak(S $value) * @method S getSpeak() */ class Cat { use AutoFillProperties; use Accessor; /** * @var string * @Param $class */ protected $eyes; /** * @var S */ protected $speak; }
提示:当属性是对象时,建议不要使用长链式调用
不好
$cat = new \Tests\Entity\Cat(); $cat->getSpeak()->text();
更好
Cat 重写一个方法
public function getSpeakText(): string { return $this->getSpeak()->text(); } $cat->getSpeakText();
这样做的好处是提高内聚性,尽管直接链式调用使用方便,但是当链式的一个环节需要修改名称时,如果多个地方都有使用,那么修改起来就会涉及多个地方。重写方法后,统一使用 Cat
类的 getSpeakText
方法。需要修改时,只需修改 Cat
类即可,降低出错成本。
json 转 class 对象
_class_name
: 类名称 _type
: 类的类型(DTO 或 Entity) _namespace
: 类的命名空间
调用 php generator.php rice:json_to_class xxx\basic\tests\Generate\tsconfig.json xxx\basic\tests\Generate\
第一个参数是输入的 json
文件路径,第二个参数是生成文件所在的目录
{ "_class_name": "Test", "_type": "Entity", "_namespace": "Tests\\Generate", "data": [ { "insights": { "data": [ { "name": "post_impressions", "period": "lifetime", "values": [ { "value": 614 } ], "title": "Lifetime Post Total Impressions", "description": "Lifetime: The number of times your Page's post entered a person's screen. Posts include statuses, photos, links, videos and more. (Total Count)" } ], "paging": { "previous": "xxxxxxxxxxxxxxx", "next": "yyyyyyyyyyyyyyy" } }, "created_time": "2021-10-13T16:11:55+0000", "message": "Very important message" } ], "paging": { "cursors": { "before": "xxxxxxxxxxxxxxx", "after": "yyyyyyyyyyyyyyy" }, "next": "zzzzzzzzzz" } }