eudiegoborgs/hyperf-cgen

hyperf 的自定义类生成器

v3.1.0 2024-02-09 17:44 UTC

This package is auto-updated.

Last update: 2024-09-09 18:59:06 UTC


README

CGen 是一个自定义类生成器库,旨在优化您的 hyperf 开发体验。

如果您使用此库,可以为您的类创建占位符,并只需一个命令即可创建代码结构。

Latest Stable Version Total Downloads License PHP Version Require

安装

composer require eudiegoborgs/hyperf-cgen

配置

发布配置文件,以便您可以定义自己的规则

php bin/hyperf.php vendor:publish eudiegoborgs/hyperf-cgen

配置文件将出现在以下路径

config/autoload/cgen.php

在此文件中,您可以定义特定类的自定义创建规则

<?php

declare(strict_types=1);

return [
    'generator' => [
        'example_type' => [
            'namespace' => 'CyBorgs\\Hyperf\\CGen\\Custom',
            'stub' => __DIR__ . '/stubs/class.stub',
            'run_previous' => [
                'other_type',
                'interface'
            ]
        ],
        'other_type' => [
            'namespace' => 'CyBorgs\\Hyperf\\CGen\\OtherCustom',
            'stub' => __DIR__ . '/stubs/other_class.stub',
            'prefix' => 'Prefix',
            'suffix' => 'Suffix',
        ],
        'interface' => [
            'namespace' => 'CyBorgs\\Hyperf\\CGen\\Custom\\%CLASS%\\Interfaces',
            'stub' => __DIR__ . '/stubs/interface.stub',
            'suffix' => 'Interface',
        ],
    ],
    'default' => [
        'stub' => 'path/to/real/stub/example_type.stub',
        'namespace' => 'App\\Example\\Default'
    ]
];

创建占位符文件

这是一个其他类型的占位符

// path/to/real/stub/other_type.stub
<?php

declare(strict_types=1);

namespace %NAMESPACE%;

class %CLASS%
{

}

这是一个示例类型接口的占位符

// path/to/real/stub/example_type_interface.stub
<?php

declare(strict_types=1);

namespace %NAMESPACE%;

interface %CLASS%
{

}

这是一个示例类型的占位符

// path/to/real/stub/example_type_interface.stub
<?php

declare(strict_types=1);

namespace %NAMESPACE%;

use %other_type.NAMESPACE%\%other_type.CLASS%;

class %CLASS%
{
    public function __construct(
        private %other_type.CLASS% %other_type.VARIABLE_NAME%
    ) {}
}

用法

要创建一个新类,您需要运行此命令

php bin/hyperf.php cgen:create example_type MyClassName 

运行命令后,此库将为您创建以下结构的文件,使用您的占位符

src/
    - Example/
        - Namespace/
            - MyClassName/
                - Interfaces/
                    - MyClassNameInterface.php
            - MyClassName.php
        - OtherNamespace/
            PrefixMyClassNameSuffix.php

显示 PrefixMyClassNameSuffix.php 文件

<?php

declare(strict_types=1);

namespace CyBorgs\Hyperf\CGen\OtherCustom;

class PrefixMyClassNameSuffix
{
    
}

显示 MyClassNameInterface.php 文件

<?php

declare(strict_types=1);

namespace CyBorgs\Hyperf\CGen\Custom\MyClassName\Interfaces;

class MyClassNameInterface
{
    
}

显示 MyClassName.php 文件

<?php

declare(strict_types=1);

namespace CyBorgs\Hyperf\CGen\Custom;

use CyBorgs\Hyperf\CGen\OtherCustom\PrefixMyClassNameSuffix;

class MyClassName
{
    public function __construct(
        private PrefixMyClassNameSuffix $classTest
    ) {}
}