offline/oc-seeder-plugin 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

offline/oc-seeder-plugin

Composer 安装命令:

composer require offline/oc-seeder-plugin

包简介

Laravel Seeder integration for October CMS.

README 文档

README

Laravel Seeder integration for October CMS.

This plugin integrates Laravel's Factory and Database Seeder features with October CMS.

Installation

  1. Install the plugin using Composer.
composer require offline/oc-seeder-plugin
  1. Setup the random file helpers.
php artisan seeder:init

Defining factories

To define a new Factory for your plugin, create a YourModelFactory.php in the plugin's factories folder and define your factories as you would in Laravel:

<?php
// plugins/yourvendor/yourplugin/factories/YourModelFactory.php
namespace YourVendor\YourPlugin\Factories;

class YourModelFactory extends \OFFLINE\Seeder\Classes\Factory
{
    /**
     * Default model attributes.
     */
    public function definition()
    {
        return [
            'name' => fake()->name,
            'number' => fake()->numberBetween(0, 6),
        ];
    }

    /**
     * Define states: Override attributes that differ from the default definition.
     * Use it like this: YourModel::factory()->withHigherNumber()->make();
     */
    public function withHigherNumber()
    {
        return $this->states(function(array $attributes) {
            return [
                'number' => fake()->numberBetween(100, 900);
            ];
        });
    }
}

Next, add the OFFLINE\Seeder\Traits\HasSeederFactory trait to your model:

<?php
// plugins/yourvendor/yourplugin/models/YourModel.php
namespace YourVendor\YourPlugin\Models;

class YourModel extends Model
{
    use \OFFLINE\Seeder\Traits\HasSeederFactory; // add this
}

Defining seeders

Add a registerSeeder method to your Plugin.php in which you seed your plugin's models:

public function registerSeeder()
{
    \YourVendor\YourPlugin\Models\YourModel::factory()->count(50)->create();
}

Tailor integration

Starting from version 2.1, this plugin can be used to seed Tailor data.

Requirements

To seed Tailor data using this plugin, it is required that all Tailor blueprint handles are in the SomeSection\SomeEntity format, like Blog\Post.

Defining factories

For a Tailor entity like Blog\Post, create the file app/factories/blog/PostFactory.php and define your factory in it.

<?php

namespace App\Factories\Blog;

class PostFactory extends \OFFLINE\Seeder\Classes\Factory
{
    public function definition()
    {
        return [
            'title' => fake()->sentence,
            // ...
            'is_enabled' => true,
        ];
    }
}

Registering seeders

In the app/Provider.php, add a static registerSeeder method and define your seeders like so:

<?php

namespace App;

use OFFLINE\Seeder\Classes\Factory;

class Provider extends \System\Classes\AppBase
{
    // ...

    /**
     * @param $seed \Closure(string $handle, \Closure(Factory $factory) $callback): void
     */
    public static function registerSeeder(\Closure $seed)
    {
        // Blog\Post = blueprint handle
        $seed('Blog\Post', function(Factory $factory) {
            $factory->count(10)->create();
        });

        $seed('Blog\Category', function(Factory $factory) {
            // ...
        });

        $seed('Blog\Author', function(Factory $factory) {
            // ...
        });
    }

Seeding specific blueprints

The --plugins flag can be used to seed specific entities:

php artisan offline:seeder --plugins=Blog\\Post

Migrate from 2.x

October 3.3 introduced its own plugin:seed Artisan command. To resolve this conflict, the Artisan command of this plugin was renamed to offline:seeder.

No special migration work is required, you can just use the new Artisan command.

Migrate from 1.x

To migrate old seeders from Version 1.0 of this plugin, make the following changes:

  1. Move all factories from the factories.php to their own Factory classes in the factories directory.
  2. Add the OFFLINE\Seeder\Traits\HasSeederFactory trait to all models
  3. Change your registerSeeder method:
// Old
factory(YourModel::class)->make();
factory(YourModel::class)->states('special')->make();
// New
YourModel::factory()->make();
YourModel::factory()->special()->make();

Running seeders

Simply run php artisan offline:seeder to run the seeders of all plugins. The seeder of each plugin will be only run once.

To run a seeder for a already seeded plugin, use the --fresh option. Be aware that this will rollback and reinstall all plugins with a registered seeder completely, so any plugin data will be lost.

You can use the --plugins option to run only specified seeders. Simply provide a comma-separated list of plugin names.

php artisan offline:seeder --plugins=Vendor.PluginA,Vendor.PluginB --fresh

Included factories

This plugin includes factories for the following models:

\System\Models\File::class

\System\Models\File::factory()->make() returns a File model with a random image. You can use it in any seeder to attach a file to a created model:

// Create a model
$myModel = \YourVendor\YourPlugin\Models\YourModel::factory()->create();

// Attach an image
$image = \System\Models\File::factory()->make();
$myModel->image()->save($image);

There are size states available: tiny returns a 90x90 image, hd returns a 1920x1080 image and huge returns a 6000x4000 image. Only one side of the image will match the given dimension (it is uncropped by default).

$tiny = \System\Models\File::factory()->tiny()->make();
$hd = \System\Models\File::factory()->hd()->make();
$huge = \System\Models\File::factory()->huge()->make();

If you need something other than an image, you can use the file, pdf, mp3 or xlsx states:

$randomType = \System\Models\File::factory()->file()->make();
$pdf = \System\Models\File::factory()->pdf()->make();
$mp3 = \System\Models\File::factory()->mp3()->make();
$xlsx = \System\Models\File::factory()->xlsx()->make();

\Backend\Models\User::class

\Backend\Models\User::factory()->make() returns a Backend User model. You can use the superuser state to generate a superuser.

// Build a simple backend user.
\Backend\Models\User::factory()->make();

// Build a superuser backend user.
\Backend\Models\User::factory()->superuser()->make();

\RainLab\User\Models\User::class

\RainLab\User\Models\User::factory()->make() returns a RainLab User model.

Twig helper functions

You can use the random_image() helper function to get a random image directly in your markup.

<img src="{{ random_image().thumb(400, 800) }}" alt=""/>
<img src="{{ random_image('tiny').thumb(400, 800) }}" alt=""/>
<img src="{{ random_image('hd').thumb(400, 800) }}" alt=""/>
<img src="{{ random_image('huge').thumb(400, 800) }}" alt=""/>

If you need a valid file download, you can use the random_file() function:

<a href="{{ random_file('xlsx').path }}" download>Download the spreadsheet!</a>
<a href="{{ random_file('pdf').path }}" download>Download the PDF!</a>

{# or make some noise #}
<audio controls src="{{ random_file('mp3').path }}"></audio>

Attribution

All images used in this plugin are provided by unsplash.com.

Credits

This plugin is heavily inspired by Inetis' oc-testing-plugin.

offline/oc-seeder-plugin 适用场景与选型建议

offline/oc-seeder-plugin 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 186 次下载、GitHub Stars 达 12, 最近一次更新时间为 2021 年 04 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 offline/oc-seeder-plugin 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 offline/oc-seeder-plugin 我们能提供哪些服务?
定制开发 / 二次开发

基于 offline/oc-seeder-plugin 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 4
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-04-06