如何创建自定义函数/格式化程序 nelmio/alice v.3? [英] How to create custom function/formatter nelmio/alice v.3?

查看:25
本文介绍了如何创建自定义函数/格式化程序 nelmio/alice v.3?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 symfony 4 的新手,并试图为 yml nelmio/alice 编写自己的函数,但是在我运行 bin/console algorithm:fixtures:load 后,我收到了这个错误:<块引用>

在 DeepCopy.php 第 177 行:

类ReflectionClass"不可克隆.

这是我的 fixtures.yml 文件:

App\Entity\Post:后_{1..10}:标题:<customFunction()>

这是我的 AppFixture.php 文件:

loadFile(__DIR__.'/Fixtures.yml',['提供者' =>[$this]])->getObjects();foreach($objectSet as $object) {$manager->persist($object);}$manager->flush();}公共函数 customFunction() {//一些计算返回'是的!我有奖金';}}

解决方案

调查了 nelmio/alice 新版本后,找到了解决方案:

我们应该首先创建一个提供程序,它是一个包含我们新的自定义函数的类.其次,扩展 NativeLoader 类以注册我们的新提供程序.第三,使用我们新的 NativeLoader(这里是 CustomNativeLoader),它允许我们使用新的格式化程序.

这是 CustomFixtureProvider.php 中的提供者:

text($faker->numberBetween(20,100));返回 $title;}}

作为旁注,title 函数会为您的文章或帖子等生成一些虚拟标题,动态长度在 20..100 个字符之间.此函数使用 Faker Alice 内置的格式化程序.

这是 CustomNativeLoader.php:

addProvider(new AliceProvider());$generator->addProvider(new CustomFixtureProvider());$generator->seed($this->getSeed());返回 $generator;}}

这里是 LoadFixture.php:

loadFile(__DIR__ .'/fixtures.yml')->getObjects();foreach($objectSet as $object) {$manager->persist($object);}$manager->flush();}}

最后是使用我们新的 title 格式化程序的示例:

App\Entity\Post:后_{1..10}:标题:<title()>

我知道编写一个小函数的方法很长,但根据我的调查,这是在新版本中扩展格式化程序的标准方法.

I'm new to symfony 4 and tried to write my own function for yml nelmio/alice, but after I ran bin/console doctrine:fixtures:load , I got this error:

In DeepCopy.php line 177:

The class "ReflectionClass" is not cloneable.

Here is my fixtures.yml file:

App\Entity\Post:
post_{1..10}:
    title: <customFunction()>

Here is my AppFixture.php file:

<?php

namespace App\DataFixtures;

use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;
use Nelmio\Alice\Loader\NativeLoader;



class AppFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $loader = new NativeLoader();

        $objectSet = $loader->loadFile(__DIR__.'/Fixtures.yml',
            [
                'providers' => [$this]
            ]
        )->getObjects();

        foreach($objectSet as $object) {
            $manager->persist($object);
        }

        $manager->flush();
    }

    public function customFunction() {

        // Some Calculations

        return 'Yep! I have got my bonus';
    }

}

解决方案

After investigating about nelmio/alice new version, I found the solution:

We should first create a provider which is a class that contains our new custom functions. Second, extend the NativeLoader class to register our new provider. Third, use our new NativeLoader (Here is CustomNativeLoader) which allows us to use our new formatters.

Here is the provider in CustomFixtureProvider.php:

<?php


namespace App\DataFixtures;
use Faker\Factory;

class CustomFixtureProvider
{
    public function title()
    {
        $faker = Factory::create();

        $title = $faker->text($faker->numberBetween(20,100));

        return $title;

    }
}

As a side note, the title function generates some dummy titles for your articles or posts or etc. which have dynamic length between 20..100 character. This function use Faker Alice built-in formatters.

Here is CustomNativeLoader.php:

<?php

namespace App\DataFixtures;

use Nelmio\Alice\Faker\Provider\AliceProvider;
use Nelmio\Alice\Loader\NativeLoader;
use Faker\Factory as FakerGeneratorFactory;
use Faker\Generator as FakerGenerator;

class CustomNativeLoader extends NativeLoader
{
    protected function createFakerGenerator(): FakerGenerator
    {
        $generator = FakerGeneratorFactory::create(parent::LOCALE);
        $generator->addProvider(new AliceProvider());
        $generator->addProvider(new CustomFixtureProvider());
        $generator->seed($this->getSeed());

        return $generator;
    }
}

Here is LoadFixture.php:

<?php
namespace App\DataFixtures;

use App\DataFixtures\CustomNativeLoader;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\Persistence\ObjectManager;

class LoadFixtures extends Fixture
{
    public function load(ObjectManager $manager)
    {
        $loader = new CustomNativeLoader();

        $objectSet = $loader->loadFile(__DIR__ . '/fixtures.yml')->getObjects();
        foreach($objectSet as $object) {
            $manager->persist($object);
        }
        $manager->flush();
    }
}

And finally an example for using our new title formatter:

App\Entity\Post:
    post_{1..10}:
        title: <title()>

I know this is a long way for writing a small function but according to my investigation, it's a standard way for extending the formatters in new version.

这篇关于如何创建自定义函数/格式化程序 nelmio/alice v.3?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆