kunstmaan/fixtures-bundle

使用此包,您可以通过yaml文件轻松地创建固定数据

安装次数:43,692

依赖项: 1

建议者: 0

安全: 0

星级: 1

关注者: 9

分支: 1

开放问题: 1

类型:symfony-bundle

7.1.1 2024-03-31 20:24 UTC

README

您仍将使用默认的doctrine固定数据方式,但不是从Doctrine\Common\DataFixtures\AbstractFixture类扩展,而是从Kunstmaan\FixturesBundle\Loader\FixtureLoader类扩展。

这将是您的固定类的外观

<?php

namespace Acme\SomeBundle\DataFixtures\ORM\DefaultSiteGenerator;

use Doctrine\Common\DataFixtures\OrderedFixtureInterface;
use Kunstmaan\FixturesBundle\Loader\FixtureLoader;

class YamlFixtures extends FixtureLoader implements OrderedFixtureInterface
{
    protected function getFixtures()
    {
        return [
            __DIR__ . '/media.yml',
            __DIR__ . '/default.yml',
        ];
    }

    /**
     * Get the order of this fixture
     *
     * @return integer
     */
    public function getOrder()
    {
        return 50;
    }
}

Yaml吗?

是的!创建固定数据从未如此简单。它受到了Nelmio/Alice包的启发。我们为什么不使用那个包呢?那是因为Nelmio/Alice非常适合“简单”实体,但我们的CMS在创建页面和页面部分时会发生很多操作。因此,我们希望从头开始而不是在其他包中修改。

那么这些yaml文件是什么样的呢?

\Kunstmaan\MediaBundle\Entity\Media:
    image_homepage-header:
        folder: image
        originalFilename: <getMediaPath()>homepage-header.jpg

    image_{some.svg, this.svg, that.svg, yes.svg, no.svg}:
        folder: image
        originalFilename: <getMediaPath()><current()>


\Acme\SomeBundle\Entity\Pages\HomePage:
    homepage:
        title: <word()>
        parameters:
            page_internal_name: homepage
            set_online: true
            hidden_from_nav: false
            creator: <adminUser()>
        translations:
            nl:
                title: Home
            fr:
                title: Home

\Acme\SomeBundle\Entity\Pages\ContentPage:
    content{1..10}:
        title: <word()>
        parameters:
            parent: @homepage
            page_internal_name: content<current()>
            set_online: true
            hidden_from_nav: false
            creator: <adminUser()>
        translations:
            nl: []
            fr: []

\Acme\SomeBundle\Entity\Contact:
    contact_{ironman, blackwidow, thor, hulk, captainamerica, hawkeye}:
        firstName: <firstName()>
        lastName: <lastName()>
        email: <email()>
        function: <word()>
        mobile: <phoneNumber()>
        phone: <phoneNumber()>

\Acme\SomeBundle\Entity\PageParts\HeaderPagePart:
    header_pp_{1..10}:
        title: <word()>
        niv: 1
        parameters:
            page: @content<current()>
            context: header
        translations:
            nl: []
            fr: []

\Acme\SomeBundle\Entity\PageParts\ContactPagePart:
    contact_pp_{1..5}:
        contacts: 
            - @contact_ironman
            - @contact_blackwidow
            - @contact_thor
        parameters:
            page: @content<current()>
            context: main
        translations:
            nl: []
            fr: []

    contact_pp_{6..10}:
        contacts: 
            - @contact_hulk
            - @contact_captainamerica
            - @contact_hawkeye
        parameters:
            page: @content<current()>
            context: main
        translations:
            nl: []
            fr: []

提供者

提供者是用于向固定数据返回数据的类。例如,如果您使用<current()>方法,则将调用Spec提供者。所以如果您想添加一些功能以轻松返回页面创建者的值,您可以添加一个包含该方法的提供者。

public function adminUser()
{
    return 'admin';
}

默认情况下,您可以将这些类型的函数添加到您的固定类中,因为它会自动作为提供者添加。

此外,您还有Spec提供者、NodeTranslation提供者和Faker提供者。您可以通过将函数getProviders()添加到您的固定类并返回包含您的提供者的数组来添加自己的提供者,或者您可以通过添加kunstmaan_fixtures.provider标记来标记您的提供者。

解析器

解析器用于将yaml数据转换为实际数据。因此,类似于@content<current()>的内容将通过不同的解析器转换为对象。默认情况下,您有Method和Reference解析器用于属性数据,以及Listed和Range解析器用于规范。如果您想添加自己的解析器,只需通过标记它们为kunstmaan_fixtures.parser.propertykunstmaan_fixtures.parser.spec即可。

填充器

填充器正好与名称相符。填充器将在解析所有yaml数据后填充实体。如果您想添加自己的填充器,只需通过添加kunstmaan_fixtures.populator标记即可。

构建器

使用构建器,您可以在创建实体的过程中操纵行为。这可以在三个阶段发生:预构建、后构建和后刷新构建。在这些阶段中,您可以操纵实体或添加更多实体,例如我们可以在PageBuilder中做的那样。如果您想添加自己的构建器,只需通过标记它们为kunstmaan_fixtures.builder即可。