spatie/laravel-morph-map-generator

自动在您的Laravel应用程序中生成形态映射

1.2.1 2024-02-29 08:09 UTC

This package is auto-updated.

Last update: 2024-08-29 09:14:54 UTC


README

自动在您的Laravel应用程序中生成形态映射

Latest Version on Packagist Tests Psalm Check & fix styling Total Downloads

使用此包,您无需担心忘记将模型添加到应用程序的形态映射中。每个模型都会自动在形态映射中注册自己。您需要做的就是像这样在模型上实现getMorphClass方法

class Post extends Model
{
    public function getMorphClass(): string {
        return 'post';
    }
}

从现在起,Post模型将在您的形态映射中以post表示。

支持我们

我们投入了大量资源来创建最佳开源包。您可以通过购买我们的付费产品之一来支持我们。

我们非常感谢您从家乡寄来明信片,说明您正在使用我们的哪个包。您可以在我们的联系页面上找到我们的地址。我们将所有收到的明信片发布在我们的虚拟明信片墙上。

安装

您可以通过composer安装此包

composer require spatie/laravel-morph-map-generator

您可以使用以下命令发布配置文件

php artisan vendor:publish --provider="Spatie\LaravelMorphMapGenerator\MorphMapGeneratorServiceProvider" --tag="config"

这是发布配置文件的内容

return [
    /*
    * When enabled, morph maps will be automatically generated when the
    * application is booted.
    */

    'autogenerate' => true,

    /**
     * Change the base directory if the models don't reside in the default App namespace.
     *
     * For example, the base directory would become 'src' if:
     * - Application is in src/App
     * - Models are in src/Domain
     */

    'base_directory' => '/',

    /*
    * Within these paths, the package will search for models to be included
    * in the generated morph map.
    */

    'paths' => [
        app_path(),
    ],

    /*
    * Only models that extend from one of the base models defined here will
    * be included in the generated morph map.
    */

    'base_models' => [
        Illuminate\Database\Eloquent\Model::class,
    ],

    /*
    * When generating the morph map, these models will not be included.
    */

    'ignored_models' => [],


    /*
    * Morph maps can be cached, there's a `FilesystemMorphMapCacheDriver` which
    * stores the morph map as a file in a directory or you can also use the
    * Laravel built-in cache by using `LaravelMorphMapCacheDriver`.
    *
    * Both drivers have their own config:
    * - `FilesystemMorphMapCacheDriver` requires a `path` to store the file
    * - `LaravelMorphMapCacheDriver` requires a `key` for storage
    */

    'cache' => [
        'type' => Spatie\LaravelMorphMapGenerator\Cache\FilesystemMorphMapCacheDriver::class,
        'path' => storage_path('app/morph-map-generator'),
    ],
];

使用方法

首先,您必须为要包含在形态映射中的模型实现getMorphClass。我们建议您在应用程序中创建一个新的基模型类,所有模型都从此类扩展。这样,当getMorphClass尚未实现时,您可以抛出异常

use Illuminate\Database\Eloquent\Model;

abstract class BaseModel extends Model
{
    public function getMorphClass()
    {
        throw new Exception('The model should implement `getMorphClass`');
    }
}

当一个模型没有实现getMorphClass时,在构建生成的形态映射时将抛出异常,这使得您可以快速找到没有形态映射条目的模型。

当在morph-map-generator配置文件中启用autogenerate时,应用程序中的形态映射将在每次应用程序启动时动态生成。这对于开发环境来说很棒,因为每次应用程序启动时,形态映射都会重新生成。出于性能原因,您应该通过运行以下命令来缓存动态生成的形态映射

php artisan morph-map:cache

可以通过运行以下命令删除缓存的形态映射

php artisan morph-map:clear

使用自定义解析器

您还可以通过使用自定义解析器来程序化地确定形态类值。例如,您可以使用以下方法自动根据模型表的复数形式推导出值

MorphMapGenerator::resolveUsing(fn ($model) => Str::singular($model->getTable()));

警告!当上述闭包的输出不稳定时,您需要手动更新数据库中所有morhp_type列。使用像表名这样的方法是个好主意,因为它们不会经常改变。

您可以在您的服务提供者的boot方法中设置解析器。

路径之外的模型

一些模型,如默认的Laravel User模型和由包定义的模型,由于此包只搜索app路径中的模型,而不是完整的vendor目录,因此不会被此包发现。您可以通过使用Laravel的默认形态映射功能将它们包含在形态映射中

Relation::enforceMorphMap([
    'post' => 'App\Models\Post',
    'video' => 'App\Models\Video',
]);

测试

composer test

更新日志

请参阅变更日志以获取有关最近更改的更多信息。

贡献

请参阅贡献指南以获取详细信息。

安全漏洞

请查阅我们的安全策略,了解如何报告安全漏洞。

鸣谢

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件