AliceFixturesBundle
AliceFixturesBundle copied to clipboard
Missing dependencies when creating custom Faker Providers
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?
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 }
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
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.
I created a issue at nelmio/alice for a access to these generators: https://github.com/nelmio/alice/issues/186