mjkruszewski / plumbok
PHP版的Lombok运行时代码生成器
1.0.1
2018-07-14 07:38 UTC
Requires
- php: ^7.1
- doctrine/annotations: 1.*
- doctrine/inflector: ^1.3
- nikic/php-parser: 4.*
- phpdocumentor/reflection-docblock: ^4.3
- symfony/console: ^4.0
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/php-code-coverage: 6.*
- phpunit/phpunit: ^7.1
- symfony/var-dumper: ^3.2
README
PHP版的Lombok运行时代码生成器。
代码生成是在额外的自动加载器检测到使用了Plumbok注解的类并在预处理步骤中加载新生成的代码时开始的。
安装
使用Composer安装
composer require mjkruszewski/plumbok
使用方法
在开发时在运行时使用自动加载器
注册额外的自动加载器
require 'vendor/autoload.php'; Plumbok\Autoload::register('Plumbok\\Test');
在类中使用注解
namespace Plumbok\Test; /** * @Data * @ToString(property = "email") */ class Person { /** * @var array * @Getter @Setter */ private $names = []; /** * @var string * @Getter @Setter */ private $email; /** * Holds age * @var int * @Getter @Setter */ private $age; /** * @var \DateTime * @Getter @Setter */ private $birthdate; /** * @var int[] * @Getter @Setter */ private $favouriteNumbers = [1, 7, 14, 21, 28]; }
第一次运行后,您的原始代码将略有修改,增加的docblock注解(标签)将以PhpDocumentor风格出现。
namespace Plumbok\Test; /** * @Data * @ToString(property = "email") * @method void __construct(int $age, \DateTime $birthdate) * @method array getNames() * @method void setNames(array $names) * @method string getEmail() * @method void setEmail(string $email) * @method string toString() * @method int getAge() * @method void setAge(int $age) * @method \DateTime getBirthdate() * @method void setBirthdate(\DateTime $birthdate) * @method int[] getFavouriteNumbers() * @method void setFavouriteNumbers(int[] $favouriteNumbers) */ class Person { /** * @var array * @Getter @Setter */ private $names = []; /** * @var string * @Getter @Setter */ private $email; /** * Holds age * @var int * @Getter @Setter */ private $age; /** * @var \DateTime * @Getter @Setter */ private $birthdate; /** * @var int[] * @Getter @Setter */ private $favouriteNumbers = [1, 7, 14, 21, 28]; }
这个预处理步骤允许IDE识别从docblock生成的函数。第二步是包含生成的代码,其外观如下
namespace Plumbok\Test; /** * @Data * @ToString(property = "email") */ class Person { /** * @var array * @Getter @Setter */ private $names = []; /** * Holds age * @var int * @Getter @Setter */ private $age; /** * @var \DateTime * @Getter @Setter */ private $birthdate; /** * @var int[] * @Getter @Setter */ private $favouriteNumbers = [1, 7, 14, 21, 28]; /** * Person constructor. * * @param int $age * @param \DateTime $birthdate */ public function __construct(int $age, \DateTime $birthdate) { $this->age = $age; $this->birthdate = $birthdate; } /** * Retrieves names * * @return array */ public function getNames() : array { return $this->names; } /** * Sets names * * @param array $names * @return void */ public function setNames(array $names) { $this->names = $names; } /** * Retrieves email * * @return string */ public function getEmail() : string { return $this->email; } /** * Sets email * * @param string $email string * @return void */ public function setEmail(string $email) { return $this->email = $email; } /** * Returns string from $email * * @return string */ public function toString() : string { return (string) $this->email; } /** * Retrieves age * * @return int */ public function getAge() : int { return $this->age; } /** * Sets age * * @param int $age * @return void */ public function setAge(int $age) { $this->age = $age; } /** * Retrieves birthdate * * @return \DateTime */ public function getBirthdate() : \DateTime { return $this->birthdate; } /** * Sets birthdate * * @param \DateTime $birthdate * @return void */ public function setBirthdate(\DateTime $birthdate) { $this->birthdate = $birthdate; } /** * Retrieves favouriteNumbers * * @return int[] */ public function getFavouriteNumbers() : array { return $this->favouriteNumbers; } /** * Sets favouriteNumbers * * @param int[] $favouriteNumbers * @return void */ public function setFavouriteNumbers(array $favouriteNumbers) { $this->favouriteNumbers = $favouriteNumbers; } }
使用CLI命令行工具进行测试和生产
使用CLI运行plumbok
可执行文件。
bin/plumbok [src-directory] [cache-directory]
注意!此用法仍需要在引导文件中添加自动加载!
使用CLI运行plumbok
可执行文件并替换源代码。
bin/plumbok [src-directory] --inline
附加选项
--ext
,-e
传递要查找类的文件扩展名,默认php
--no-tags
不会将@method
标签推送到源文件--inline
将替换源代码为生成的代码-v|vv|vvv
增加详细程度--help
,-h
显示帮助
许可证
MIT许可证(MIT)
版权所有(c)2018 Michał Brzuchalski michal.brzuchalski@gmail.com
特此授予任何获得本软件及其相关文档副本(以下简称“软件”)的人免费使用该软件的权利,不受任何限制,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本的权利,并允许软件提供者将软件提供给其他人以执行上述操作,但受以下条件的约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
本软件按“原样”提供,除非另有说明,不提供任何形式的保证,明示或暗示,包括但不限于适销性、适用于特定目的和无侵权性保证。在任何情况下,作者或版权所有者不对任何索赔、损害或其他责任负责,无论该索赔、损害或其他责任是基于合同、侵权或其他方式,无论是否与软件或软件的使用或其他交易有关。