PHP code example of maliboot / lombok

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



namespace App\Contract;

interface SwapAnnotationInterface
{
}



declare(strict_types=1);

namespace App\Ast\Generator;

use MaliBoot\Lombok\Annotation\LombokGenerator;
use MaliBoot\Lombok\Ast\AbstractClassVisitor;
use App\Contract\SwapAnnotationInterface;

// 1、此处需要添加\MaliBoot\Lombok\Annotation\LombokGenerator注解
// 2、需要继承 \MaliBoot\Lombok\Ast\AbstractClassVisitor 或者 \MaliBoot\Lombok\Ast\AbstractClassFieldVisitor
// 2.1、\MaliBoot\Lombok\Ast\AbstractClassVisitor 会内置目标类的反射变量,以支持模版相关的变量替换用处
// 2.2、\MaliBoot\Lombok\Ast\AbstractClassFieldVisitor 会内置目标类所有属性的反射变量,以支持模版相关的变量替换用处
#[LombokGenerator]
class SwapGenerator extends AbstractClassVisitor
{
    // 自定义的方法OR属性名称
    protected function getClassMemberName(): string
    {
        return 'swap';
    }

    // 自定义组合注解功能时的接口凭据
    protected function getAnnotationInterface(): string
    {
        return SwapAnnotationInterface::class;
    }

    // 自定义的方法OR属性模板
    // 可自定义变量替换:提供有 AbstractClassVisitor::$reflectionClass 和 AbstractClassFieldVisitor::$reflectionProperty 配合使用
    protected function getClassCodeSnippet(): string
    {
        // 这里返回swap的方法的代码模板。类名称可以自定义,只是为看着整齐
        return <<<'CODE'

class Collection {
    public function swap (int &$a, int &$b): void {
        $tmp = $a;
        $a = $b;
        $b = $tmp;
    }
}
CODE;
    }
}


// app/Foo.php

declare(strict_types=1);

namespace App;

use App\Annotation\Swap;

#[Swap]
class Foo
{
}

$left = 1;
$right = 2;
(new Foo)->swap($left, $right);
var_dump($left, $right); // output: 2, 1