1. Go to this page and download the library: Download maliboot/lombok library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
maliboot / lombok example snippets
// app/Foo.php
declare(strict_types=1);
namespace App;
class Foo
{
private int $id = 1;
public function getId(): int
{
return $this->id;
}
}
var_dump((new Foo)->getId()); // output: 1
// app/Foo.php
declare(strict_types=1);
namespace App;
use MaliBoot\Lombok\Annotation\Getter;
// 当Getter为类注解时,会为所有的类属性添加Getter方法,无需要一个一个添加
// #[Getter]
class Foo
{
// Getter为类属性注解时
#[Getter]
private int $id;
}
var_dump((new Foo)->getId()); // output: 1
// app/Foo.php
declare(strict_types=1);
namespace App;
use MaliBoot\Lombok\Annotation\Logger;
#[Logger]
class Foo
{
private int $id;
public function testLog(): void
{
$this-logger->error('errLog::' . __FUNCTION__);
}
}
(new Foo)->testLog(); // runtime/hyperf.log记录: [2023-06-21 11:46:31] APP_Foo.ERROR: errLog::testLog [] []
namespace App\Aspect;
use Hyperf\Di\Annotation\Aspect;
use Hyperf\Di\Aop\AbstractAspect;
use Hyperf\Di\Aop\ProceedingJoinPoint;
#[Aspect]
class LombokAspect extends AbstractAspect
{
// 此处注册所有的Lombok注解
public array $annotations = [
\App\Annotation\DTO::class,
];
public function process(ProceedingJoinPoint $proceedingJoinPoint)
{
// Do nothing, just to mark the class should be generated to the proxy classes.
return $proceedingJoinPoint->process();
}
}
// app/Foo.php
declare(strict_types=1);
namespace App;
use MaliBoot\Lombok\Annotation\Getter;
// DTO为类属性注解时
// #[DTO]
class Foo
{
// DTO为类属性注解时
#[DTO]
private int $id;
}
var_dump((new Foo)->getId()); // output: 1