proklung/bitrix-fixture-database-generator-bundle

Bitrix+Symfony 固定数据生成器包

1.0.5 2021-08-08 19:27 UTC

This package is auto-updated.

Last update: 2024-09-09 02:39:17 UTC


README

内部

安装

composer require proklung/bitrix-fixture-database-generator-bundle

包配置

配置示例(文件 local/configs/packages/bitrix-fixture-generator.yaml)

bitrix-fixture-generator:
  # Игнорировать ошибки при создании элементов - сохранение картинок итд.
  ignore_errors: true

  # Пути к фикстурам.
  fixture_path:
    - '/local/classes/BaseFixtures/'

  # Набор команд, генерирующих нужную фикстуру.
  structure_project:
    - migrator:elements spravochnik content --truncate=true --sections=false --count=20
    - migrator:elements common content --truncate=true --sections=true --count=5
    - migrator:elements-hl ExampleHighload --truncate=true --count=5

项目生成

启动项目内容生成。依次执行配置文件中 structure_project 键对应的命令(文件 local/configs/packages/bitrix-fixture-generator.yaml)

php bin/console migrator:structure

信息块内容生成

信息块 common,清除元素,生成子目录,5个元素。

php bin/console migrator:elements common content --truncate=true --sections=true --count=5

信息块 справочник,不清除元素,不生成子目录,5个元素

php bin/console migrator:elements spravochnik content --truncate=false --sections=false --count=5

高负载信息块内容生成

HL-block ExampleHighload。清除元素,5个元素。

php bin/console migrator:elements-hl ExampleHighload --truncate=true --count=5

自定义表内容生成

自定义表。清除元素,5个元素。

php bin/console migrator:seed d_ah_news --truncate=true --count=5

随机用户生成

随机用户。首先删除所有用户,5个用户以电话号码作为登录名。

php bin/console migrator:users --truncate=true --count=5 --phone=true

固定数据

ID: content.common -> 类型信息块 content,代码信息块 common。固定数据文件夹中的文件 content.common.php

数组形式的固定数据示例

use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\EnumGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\ImageGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\LinkElementGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\SentenceGenerator;

return [
//    'PREVIEW_TEXT' => 'bitrix_fixture_generator.preview_text_generator',
//    'DETAIL_TEXT' => 'bitrix_fixture_generator.detail_text_generator',
//    'NAME' => 'bitrix_fixture_generator.name_generator', // Alias сервиса
    'PROPERTY_VALUES' => [
        'STRING' => SentenceGenerator::class, // Сервис, помеченный тэгом fixture_generator.item.
        'FILE' => ImageGenerator::class, // Сервис, помеченный тэгом fixture_generator.item.
        'MULTIPLE_STRING' => 'bitrix_fixture_generator.multiple_string_generator', // Штатный сервис-генератор из бандла.
        'MULTIPLE_FILE' => 'bitrix_fixture_generator.multiple_image_generator',
        'ENUM' => EnumGenerator::class,
        'MULTIPLE_ENUM' => 'bitrix_fixture_generator.multiple_enum_generator',
        'LINK' => LinkElementGenerator::class,
        'MULTIPLE_LINK' => 'bitrix_fixture_generator.multiple_link_generator',
    ]
];

仅指定特殊处理的字段。标准字段已提供默认生成器

元素

[
        'PREVIEW_PICTURE' => 'bitrix_fixture_generator.preview_picture_generator',
        'DETAIL_PICTURE' => 'bitrix_fixture_generator.detail_picture_generator', // Сервис, помеченный тэгом fixture_generator.item.
        'ACTIVE_FROM' => DateGenerator::class,
        'CREATED_BY' => UserIdGenerator::class,
        'MODIFIED_BY' => UserIdGenerator::class,
        'PREVIEW_TEXT' => 'bitrix_fixture_generator.preview_text_generator',
        'PREVIEW_TEXT_TYPE' => 'html',
        'DETAIL_TEXT' => 'bitrix_fixture_generator.detail_text_generator',
        'DETAIL_TEXT_TYPE' => 'html',
        'NAME' => 'bitrix_fixture_generator.name_generator', // Alias сервиса
        'CODE' => CodeGenerator::class,
    ];

目录

    [
        'NAME' => 'bitrix_fixture_generator.name_generator', // Alias сервиса
        'CODE' => CodeGenerator::class,
        'PICTURE' => 'bitrix_fixture_generator.preview_picture_generator',
        'DETAIL_PICTURE' => 'bitrix_fixture_generator.detail_picture_generator',
        'DESCRIPTION' => 'bitrix_fixture_generator.preview_text_generator',
        'DESCRIPTION_TYPE' => 'html',
        'MODIFIED_BY' => UserIdGenerator::class,
    ];

通过实现 FixtureInterface 的类来定义固定数据(自动从固定数据目录中检索,配置在包配置中)

use Prokl\BitrixFixtureGeneratorBundle\Services\Annotations\FieldParams;
use Prokl\BitrixFixtureGeneratorBundle\Services\Contracts\FixtureInterface;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\EnumGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\ImageGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\LinkElementGenerator;
use Prokl\BitrixFixtureGeneratorBundle\Services\Generators\SentenceGenerator;

class ContentCommonFixture implements FixtureInterface
{
   /**
     * ID фикстуры (тип инфоблока . код инфоблока).
     *
     * @return string
     */
    public function id() : string
    {
        return 'content.common';
    }

    /**
     * Фикстура
     * @FieldParams(
     *    params={
     *     "PREVIEW_PICTURE"= { "width"=400, "height"=400 },
     *     "PROPERTY_VALUES" = {
     *          "STRING"= { "length"=22 }
     *      }
     *    }
     * )
     */
    public function fixture() : array
    {
        return [
            'PREVIEW_PICTURE' => ImageGenerator::class,
            'PROPERTY_VALUES' => [
                'STRING' => SentenceGenerator::class,
                'FILE' => ImageGenerator::class,
                'MULTIPLE_STRING' => 'bitrix_fixture_generator.multiple_string_generator',
                'MULTIPLE_FILE' => 'bitrix_fixture_generator.multiple_image_generator',
                'ENUM' => EnumGenerator::class,
                'MULTIPLE_ENUM' => 'bitrix_fixture_generator.multiple_enum_generator',
                'LINK' => LinkElementGenerator::class,
                'MULTIPLE_LINK' => 'bitrix_fixture_generator.multiple_link_generator',
                // 'YES' => 1,
            ]
        ];
    }
}

通过注解设置生成器参数。它们作为参数传递给生成器的 generate 方法

负载:

    public function generate(?array $payload = null)
    {
        $width = array_key_exists('width', $payload['params']) ? $payload['params']['width'] : 0;
    }

内容生成器

FixtureGeneratorInterface 接口,继承自 AbstractGenerator。只需实现 abstract public function generate(?array $payload = null) 方法。

标记为 fixture_generator.item

默认

  • ImageIdGenerator
  • UserIdGenerator
  • HtmlGenerator
  • TextGenerator
  • CodeGenerator
  • SortGenerator
  • ImageGenerator
  • DateGenerator
  • LinkElementGenerator
  • RandomLinkElementGenerator
  • MultipleGeneratorDecorator - 装饰器,将任何普通属性生成器转换为多重属性生成器。

如此

  # Множественное поле типа Cтрока.
  bitrix_fixture_generator.multiple_string_generator:
    class: Prokl\BitrixFixtureGeneratorBundle\Services\Generators\MultipleGeneratorDecorator
    arguments:
      - '@Prokl\BitrixFixtureGeneratorBundle\Services\Generators\SentenceGenerator' # Базовый генератор
      - 5 # Количество элементов
    tags:
      - { name: fixture_generator.item }
  • RandomLinkSectionGenerator
  • LinkSectionsGenerator
  • EnumGenerator
  • SentenceGenerator
  • IntGenerator
  • StringGenerator
  • BaseOptionGenerator
  • YesNoGenerator