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

Enhancement: Allow creating ContainerBuilder with definitions for default extensions #732

Merged
merged 1 commit into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/Faker/Container/ContainerBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,19 @@ public static function defaultExtensions(): array
];
}

public static function withDefaultExtensions(): self
{
$instance = new self();

foreach (self::defaultExtensions() as $id => $definition) {
$instance->add($id, $definition);
}

return $instance;
}
Comment on lines +62 to +71
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method can provide an extension point for people who want to

  • add extensions
  • override default extensions
<?php

declare(strict_types=1);

use Faker\Container;
use Faker\Extension;
use Faker\Generator;

require_once __DIR__ . '/../vendor/autoload.php';

$container = Container\ContainerBuilder::withDefaultExtensions()
    ->add(Extension\FileExtension::class, static function (): Extension\FileExtension {
        return new CustomFileExtension();
    })
    ->build();

$faker = new Generator($container);

This method is similar to the method simplified in #720, but returns the ContainerBuilder instead of the built Container.


public static function getDefault(): ContainerInterface
{
return new Container(self::defaultExtensions());
return self::withDefaultExtensions()->build();
}
Comment on lines 73 to 76
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can probably remove this method - I don't see any use for it (also see changes below).

}
2 changes: 1 addition & 1 deletion src/Faker/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ class Generator

public function __construct(ContainerInterface $container = null)
{
$this->container = $container ?: Container\ContainerBuilder::getDefault();
$this->container = $container ?: Container\ContainerBuilder::withDefaultExtensions()->build();
}

/**
Expand Down
6 changes: 4 additions & 2 deletions test/Faker/Extension/ContainerBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ public function testBuildReturnsContainerWhenContainerBuilderHasCallableAsDefini
self::assertEquals($definition(), $container->get($id));
}

public function testGetDefaultReturnsContainerWithDefaultExtensions(): void
public function testWithDefaultExtensionsReturnsContainerBuilderWithDefaultExtensions(): void
{
$container = ContainerBuilder::getDefault();
$builder = ContainerBuilder::withDefaultExtensions();

$container = $builder->build();

self::assertTrue($container->has(Extension\BarcodeExtension::class));
self::assertTrue($container->has(Extension\BloodExtension::class));
Expand Down