-
Notifications
You must be signed in to change notification settings - Fork 363
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
Can't override default formatters #435
Comments
Do you have some code on how you did this in the past? Please also provide Faker and PHP version as the templates suggest |
Probably something similar to how it is done in #386. |
@localheinz , no, #386 is a different problem. I will provide the code in a while. |
<?php
namespace Acme;
use Faker\Generator;
use Faker\Provider\Base;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidInterface;
include_once __DIR__.'/vendor/autoload.php';
class CommonProvider extends Base
{
public function uuid(): UuidInterface
{
return Uuid::uuid4();
}
}
$faker = new Generator();
$faker->addProvider(new CommonProvider($faker));
$uuid = $faker->uuid();
var_dump($uuid); Please try run on faker Output after run in
Output after run in
Problem: to version 1.8 we can manipulate with providers, add custom provider and our added providers added to first, as result, if core provider support uuid, we can replace it with add customer provider with uuid method. I think, better solution for this problem - use only providers for generate data, not check by method existence in Generator. Thank. |
Hello, in an ideal world I agree. However we need to maintain backwards compatibility. @pimjansen do you think changing the order that it checks for method existance is a breaking change? For now, you could try our new extension mechanism. For an example, check https://github.com/fakerphp/dutch It is still experimental, but it is functional. |
Well wel can assume that the latest is always valid right? This is probably happening because the new uuid method is added to the Generator itself. I think the only reason is to overwrite it with the extensions. But you are right they are experimental i would not suggest that. I need to dive in the code tomorrow to see what the best approach is. @bram-pkg could also mean we revoke the methods on the generator for v1 and use the magic instead. For v2 the core extension interface can be used with psr11 |
I think changing the order in which it detects the "Providers" is the best. Putting the provider check above the Generator check. |
If ik correct this is not the problem. The providers and all the legacy stuff is access via a __call method which is always last. |
Previously (to #267) we can change the default logic and formatters in our application by providers. As an example, we override uuid function for correct return UuidInterface (object instead of string). All correct work previously, because our provider have large priority than default provider for uuid. After merge #267, previously you check the method existence in generator, and only if method not exist, check in providers. As result, if method uuid exist in generator class, our provider don't called never.
Any solution for override default generator logic?
Thank, have a nice day!
The text was updated successfully, but these errors were encountered: