Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing dependencies when creating custom Faker Providers #42

Open
nass600 opened this issue Mar 4, 2015 · 4 comments
Open

Missing dependencies when creating custom Faker Providers #42

nass600 opened this issue Mar 4, 2015 · 4 comments

Comments

@nass600
Copy link
Contributor

nass600 commented Mar 4, 2015

Hi there!

Maybe I am missing something but following README instruction for adding new Faker providers lead me to the following error:

Symfony\Component\Debug\Exception\ContextErrorException]
Catchable Fatal Error: Argument 1 passed to Faker\Provider\Base::__construct() must be an instance of Faker\Generator, none given...

Every Faker Provider needs the dependency @faker.generator however this service is not registered by the bundle so if I add it the way:

services:
    your.faker.provider:
        class: YourProviderClass
        arguments: [@faker.generator]
        tags:
            -  { name: h4cc_alice_fixtures.provider }

I get the non existing dependency error.

Any clue?

@h4cc
Copy link
Owner

h4cc commented Mar 5, 2015

I think i found what you were trying to do:

Did you try to add a Provider from Faker directly? Like:

services:
    your.faker.provider:
        class: Faker\Provider\DateTime
        tags:
            -  { name: h4cc_alice_fixtures.provider }

These providers do not need to be added manually to faker, they are always available anyway: https://github.com/fzaninotto/Faker/blob/master/src/Faker/Factory.php#L9-20

And if your own provider would need a special dependency, it has to configured like so:

services:
    provider.dependency:
        class: ProviderDependency
    provider:
        class: Provider
        arguments: [@provider.dependency]
        tags:
            -  { name: h4cc_alice_fixtures.provider }

@h4cc h4cc closed this as completed Mar 5, 2015
@nass600
Copy link
Contributor Author

nass600 commented Mar 6, 2015

That's not what I tried to do.

I simply create a new Provider:

<?php

namespace AppBundle\DataFixtures\Provider;

use Faker\Provider\Base;

/**
 * Class MathProvider
 */
class Math extends Base
{
    /**
     * Evaluates a math operation
     *
     * @param string $string
     *
     * @return mixed
     */
    public function math($string)
    {
        return "whatever"
    }
}

and register it as a service following the README file (not working for me):

parameters:
    math.provider.class: AppBundle\DataFixtures\Provider\Math

services:
    math.provider:
        class: %math.provider.class%
        tags:
            -  { name: h4cc_alice_fixtures.provider }

But this way, of course, it is working:

parameters:
    math.provider.class: AppBundle\DataFixtures\Provider\Math
    faker.generator.class: Faker\Factory

services:
    faker.generator:
        class: %faker.generator.class%
        factory: [%faker.generator.class%, create]
    math.provider:
        class: %math.provider.class%
        arguments: [@faker.generator]
        tags:
            -  { name: h4cc_alice_fixtures.provider }

Because every Faker provider needs the @faker.generator service as argument.

My question is:
Should not this bundle take care of registering the needed Faker services taking into account that Faker is a dependency of Nelmio/Alice or I have to register as a service the dependencies of my dependencies

If the second one, maybe the documentation should mention that in order to create a Faker provider we need to register the Faker services before but IMO I think the bundle must take care of registering its dependencies services.

Thank you

@h4cc
Copy link
Owner

h4cc commented Mar 6, 2015

Because every Faker provider needs the @faker.generator service as argument.

This is not correct. Any class can be faker provider, it does not need to extend Faker\Provider\Base.
So its not the responsibility of this bundle to provide services regarding Faker.

But in the end it would be helpful and convenient to provide such a service like Faker\Generator, if it would not depend from the given locale Faker\Factory::create($locale = self::DEFAULT_LOCALE). Currently, all the Faker Generators are managed inside Alice, as can be seen here: https://github.com/nelmio/alice/blob/1.x/src/Nelmio/Alice/Loader/Base.php#L461
There is no way to access these Generators from the outside.

@h4cc h4cc reopened this Mar 6, 2015
@h4cc
Copy link
Owner

h4cc commented Mar 6, 2015

I created a issue at nelmio/alice for a access to these generators: nelmio/alice#186

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants