smurfworks/model-finder

Laravel 包,用于在现有 Laravel 项目中查找模型和构建实用索引。

v1.0.0 2022-11-09 03:54 UTC

This package is auto-updated.

Last update: 2024-09-09 08:04:57 UTC


README

Laravel 包,用于在现有 Laravel 项目中查找模型和构建实用索引。

关键特性

  • 配置发现一个或多个命名空间以扫描 Model 类
  • 忽略特定模型的发现
  • 发现可用的作用域和关系
  • 发现关于属性的详细信息
  • 通过 PHP8 属性将自定义元数据应用于单个作用域、关系和模型

关于

这个实用工具仓库是为了提供在多个项目(开源和专有)之间需要的一些常见功能而创建的,因此开源和发布它以便可以在两种情况下作为依赖项使用。

将模型查找作为依赖项用于开源包的示例:https://www.github.com/SmurfWorks/Sieve

安装

composer require smurfworks/model-finder

Laravel 应该会自动发现服务提供程序并添加别名。由于它是一个单例服务,将缓存结果,因此提供程序将仅在每次请求中扫描模型一次。

用法

/**
 * Get the index (implement laravel caching around this as you need)
 *
 * @var array $index
 */
$index = app('model-finder')->discover();

dd($index);
// Trimmed for verbosity (...)
array:3 [
 "SmurfWorks\ModelFinderTests\SampleModels\User" => array:4 [
    "meta" => array:2 [
      "name" => "User"
      "describe" => "A user record represents a person's access to this system"
    ]
    "attributes" => array:9 [
      "id" => array:4 [
        "type" => "integer"
        "default" => null
        "fillable" => false
        "hidden" => false
      ]
      "name" => array:4 [
        "type" => "string"
        "default" => null
        "fillable" => true
        "hidden" => false
      ]
      "email" => array:4 [
        "type" => "string"
        "default" => null
        "fillable" => true
        "hidden" => false
      ]
      "password" => array:4 [
        "type" => "string"
        "default" => null
        "fillable" => true
        "hidden" => false
      ]

      ...
    ]
    "relations" => array:1 [
      "role" => array:3 [
        "type" => "BelongsTo"
        "model" => "SmurfWorks\ModelFinderTests\SampleModels\User\Role"
        "meta" => array:2 [
          "name" => "User role"
          "describe" => "The user's system role"
        ]
      ]
    ]
    "scopes" => array:2 [
      "activated" => array:1 [
        "meta" => array:2 [
          "name" => "Activated users"
          "describe" => "Activated users have set a password."
        ]
      ]
      "subscribed" => array:1 [
        "meta" => array:2 [
          "name" => "Subscribed"
          "describe" => "Users that are opted in to receive the newsletter."
        ]
      ]
    ]
  ]
  "SmurfWorks\ModelFinderTests\SampleModels\User\Permission" => array:4 [
    "meta" => array:2 [
      "name" => "User permission"
      "describe" => null
    ]

    ...
  ]

  ...
]

测试

测试命名空间包含用于设置一些简单表的架构迁移和一些相应的样本模型。因为模型查找查询的一部分会查询架构以获取表数据,单元测试将运行这些迁移以测试模型发现。

./vendor/bin/phpunit

贡献

欢迎贡献和问题报告,但该项目主要是其他我工作的项目的基石,因此请注意在继续时可能会有大量的任意决策。