定制 glamorous/laravel-data-loader 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

glamorous/laravel-data-loader

最新稳定版本:0.8.2

Composer 安装命令:

composer require glamorous/laravel-data-loader

包简介

Small package to provide tools for data migrations and loading initial data into your database

README 文档

README

Small package to provide tools for data migrations and loading initial data into your database.

Installation

You can install the package via composer:

composer require glamorous/laravel-data-loader

You should publish the config file with:

php artisan vendor:publish --tag="laravel-data-loader-config"

This is the contents of the published config file:

return [
    'loaders' => [
        // EnsureSomeDataIsPresent::class,
    ],
];

Usage

Provide a list of loaders in config/data-loader.php with their class names. Those classes should implement the DataLoader-interface.

Example data loader class

<?php

namespace Database\States;

use App\Models\User;
use Glamorous\DataLoader\Database\DataLoader;

readonly final class EnsureSuperAdminIsPresent implements DataLoader
{
    public function __invoke(): void
    {
        $user = User::query()
            ->where('identifier', '=', $this->getSuperAdminIdentifier())
            ->firstOrNew();

        $user->identifier = $this->getSuperAdminIdentifier();
        $user->email = $this->getEmail();
        $user->password =  $this->getPassword();
        $user->name = 'Super Admin';

        $user->save();
    }

    public function shouldLoad(): bool
    {
        return !User::where('identifier', '=', $this->getSuperAdminIdentifier())->exists();
    }

    protected function getEmail(): string
    {
        return config('custom.superadmin.email');
    }

    protected function getPassword(): string
    {
        return Hash::make(config('custom.superadmin.password'))
    }

    protected function getSuperAdminIdentifier(): string
    {
        return config('custom.superadmin.identifier');
    }
}

Calling the loader

The command can be run after the migrations if you put the following in the boot of a Service Provider:

Event::listen(MigrationsEnded::class, function() {
    Artisan::call(DataLoaderCommand::class);
});

Or you can just call the command in your CI-scripts:

php artisan data-loader:run

It's also possible to run the data-loader only for one specific data-loader class:

php artisan data-loader:run EnsureSuperAdminIsPresent

Options

If necessary you can run the command with the --force flag. This way it would not check if its data needs to be loaded or not. It's required to confirm your choice.

If you want to see which loaders would be executed, without executing them, you can pass the --dry-run option, and it will show you the loaders that would have executed.

Contributing

Package is open for pull requests!

Security Vulnerabilities

Please review our security policy on how to report security vulnerabilities.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

  • 总下载量: 414
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-01-08