leprz/boilerplate-generator

此包可以帮助您生成任何类型的文件模板,包括PHP类

v0.2.0 2020-10-07 05:41 UTC

This package is auto-updated.

Last update: 2024-09-19 04:48:21 UTC


README

生成CRUD、命令、处理程序、端口、适配器、配置文件的文件 - 量身定制。

leprz/boilerplate-generator

example workflow name codecov

简介

此包可以帮助您生成任何类型的文件模板(php、yml等)。包含PHP类、接口、函数、方法和参数的构建器。生成的代码符合PSR-12和PSR-4规范。

安装

composer require leprz/boilerplate-generator --dev

要求

PHP >= 7.4

代码

use Leprz\Boilerplate\PathNode\Folder;
use Leprz\Boilerplate\PathNode\Php\PhpClass;
use Leprz\Boilerplate\PathNode\Php\PhpMethod;
use Leprz\Boilerplate\PathNode\Php\PhpParameter;
use Leprz\Boilerplate\PathNode\Php\PhpType;
use Leprz\Boilerplate\Configuration;
use Leprz\Boilerplate\Generator;

$generator = new Generator(
    new Configuration(
        'AppPrefix',
        'src/'
    )
);

$command = (new Folder('Command'))
    ->addFolder(new Folder('ExampleUseCase'))
    ->addPhpClass(new PhpClass('ExampleCommand'));

$handler = (new Folder('Command'))
    ->addFolder(new Folder('ExampleUseCase'))
    ->addPhpClass(new PhpClass('ExampleHandler'))
    ->addMethod(new PhpMethod('__invoke', 'public', PhpType::void(), [
        new PhpParameter('command', PhpType::object($command))
    ]));

$generator->generate($command);
$generator->generate($handler);

将创建文件 src/Command/ExampleUseCase/ExampleCommand.php

<?php

declare(strict_types=1);

namespace AppPrefix\Command\ExampleUseCase;

/**
 * @package AppPrefix\Command\ExampleUseCase
 */
class ExampleCommand
{
}

将创建文件 src/Command/ExampleUseCase/ExampleHandler.php

<?php

declare(strict_types=1);

namespace AppPrefix\Command\ExampleUseCase;

/**
 * @package AppPrefix\Command\ExampleUseCase
 */
class ExampleHandler
{
    /**
     * @param \AppPrefix\Command\ExampleUseCase\ExampleCommand
     */
    public function __invoke(ExampleCommand $command): void
    {
    }
}

高级示例

use Leprz\Boilerplate\PathNode\BoundedContext;
use Leprz\Boilerplate\PathNode\Layer;
use Leprz\Boilerplate\PathNode\Folder;
use Leprz\Boilerplate\PathNode\Php\PhpClass;
use Leprz\Boilerplate\PathNode\Php\PhpInterface;
use Leprz\Boilerplate\PathNode\Php\PhpMethod;
use Leprz\Boilerplate\PathNode\Php\PhpParameter;
use Leprz\Boilerplate\PathNode\Php\PhpType;

$this->testClass1 = (new Folder('Sample'))
    ->addPhpClass(new PhpClass('TestClass1'));

$this->testInterface1 = (new Folder('Sample'))
    ->addPhpInterface(new PhpInterface('TestInterface1'));

$this->testInterface2 = (new Folder('Sample'))
    ->addPhpInterface(new PhpInterface('TestInterface2'))
    ->addMethod(new PhpMethod('test', 'public', PhpType::string()));

$this->testClass2 = (new BoundedContext('Domain'))
    ->addLayer(new Layer('Application'))
    ->addFolder(new Folder('Command'))
    ->addPhpClass(new PhpClass('TestClass2'))
    ->extends($this->testClass1)
    ->implements($this->testInterface1, $this->testInterface2)
    ->addMethod(new PhpMethod('doSomething', 'public', PhpType::void(), [
        new PhpParameter('testClass1', PhpType::object($this->testClass1)),
        new PhpParameter('test', PhpType::string())
    ]))
    ->addMethod(new PhpMethod('doSomethingElse', 'private', PhpType::object($this->testClass1)));