bfg/doc

PHP 文档块生成器

1.0.3 2023-05-22 20:52 UTC

This package is auto-updated.

Last update: 2024-09-22 23:43:23 UTC


README

一个用于描述使用魔法方法来保持与类连接的类行为的系统。

这是用来做什么的?

其主要目的是用它来描述你在类的属性中积累的魔法方法或属性。

安装

composer require bfg/doc

工作原理

为了方便描述属性,我在php 8中添加的属性系统中编写了这个逻辑。

内核搜索所有项目文件夹中的文件,除了以下文件夹: {base_path}/public{base_path}/vendor{base_path}/routes{base_path}/resources{base_path}/storage{base_path}/runtimes{base_path}/database

并且忽略所有命名文件夹: node_modulescssjs

搜索具有 *.php 扩展名的文件,并检查文件中是否存在类。

为了避免困难,需要为每个类创建一个单独的文件。

用法

总共,为了生成助手,我创建了2个主要属性

/**
 * @props string $type Doc var type: "@method" 
 * @props array|string|null $var_type Doc type of var
 * @props string|null $name Doc name of var
 * @props string|null $description Doc description of the var
 */
use Bfg\Doc\Attributes\Doc($type, $var_type, $name|null, $description|null);

第一个属性 Bfg\Doc\Attributes\Doc 仅用于指定将扩展类能力的属性。

/**
 * @props string $name Class namespace for mixin helper
 */
use Bfg\Doc\Attributes\DocClassName($name);

第二个属性 Bfg\Doc\Attributes\DocClassName 用于指明将添加这些属性的类名。

如果你正在制作一个可以根据需要扩展其属性的包,这可能很有用。

我还为 Bfg\Doc\Attributes\Doc 属性添加了几个别名

// To generate method helpers
use Bfg\Doc\Attributes\DocMethods($var_type, $name|null, $description|null);

// To generate property helpers
use Bfg\Doc\Attributes\DocProperties($var_type, $name|null, $description|null);

// To generate method helpers from array
use Bfg\Doc\Attributes\DocMethodsFromArray($var_type, $description|null);

// To generate property helpers from array
use Bfg\Doc\Attributes\DocPropertiesFromArray($var_type, $description|null);

宏字符串

  • 全局
    • {name} - 变量名
  • 数组
    • {key} - 数组键
    • {value} - 数组值
  • 变量
    • {value} - 变量值
    • {type} - 变量类型
    • {value_construct} - 值类构造函数属性
  • 类名
    • {class} - 类名
    • {namespace} - 类命名空间

示例

use Bfg\Doc\Attributes\DocMethodsFromArray;
use Bfg\Doc\Attributes\DocPropertiesFromArray;
use Bfg\Doc\Attributes\DocClassName;

/**
 * Class PropsData
 * 
 * Result:
 * @method PropsData|static test() From inputs property for test method
 * @[type] [var_type]       [name] [description]
 * @property-read PropsData $simple_form
 * @[type]       [var_type] [name]
 *
 * And if you want you can add the "DocClassName" and created this class like mixin
 * @mixin \Bfg\Doc\Attributes\PropDataBlocks
 */
class PropsData
{
    /**
     * @var array|string[]
     */
    #[DocMethodsFromArray(['{value}', 'static'], 'From {name} property for {key} method')]
    static array $inputs = [
        'test' => PropsData::class
    ];

    /**
     * @var array|string[]
     */
    #[DocPropertiesFromArray]
    protected array $forms = [
        'simple_form' => PropsData::class
    ];

    /**
     * @var array|string[]
     */
    #[DocPropertiesFromArray, DocClassName('PropDataBlocks')]
    protected array $blocks = [
        'simple_block' => PropsData::class
    ];
}

运行

要启动生成器,只需调用 artisan 命令

php artisan make:docs

composer dump-autoload