Skip to content

Commit

Permalink
fix: load entity factories
Browse files Browse the repository at this point in the history
  • Loading branch information
sstutz authored and Stefan Stutz committed Nov 27, 2024
1 parent 04915c1 commit 0c724f5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"doctrine/dbal": "^3.0 || ^4.0",
"doctrine/orm": "^3.1",
"doctrine/persistence": "^3.3",
"fakerphp/faker": "^1.23",
"illuminate/auth": "^10.0|^11.0",
"illuminate/console": "^10.0|^11.0",
"illuminate/container": "^10.0|^11.0",
Expand All @@ -45,7 +46,6 @@
"phpstan/phpstan": "^1.9",
"phpstan/phpstan-deprecation-rules": "^1.1",
"phpunit/phpunit": "^11.4",
"fakerphp/faker": "^1.23",
"laravel/framework": "^10.0 || ^11.0",
"orchestra/testbench": "^9.5"
},
Expand Down
23 changes: 23 additions & 0 deletions src/DoctrineServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use Doctrine\ORM\Mapping\ClassMetadataFactory;
use Doctrine\ORM\Proxy\Autoloader;
use Doctrine\Persistence\ManagerRegistry;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Illuminate\Contracts\Container\Container;
use Illuminate\Notifications\ChannelManager;
use Illuminate\Support\ServiceProvider;
Expand All @@ -31,12 +33,14 @@
use LaravelDoctrine\ORM\Exceptions\ExtensionNotFound;
use LaravelDoctrine\ORM\Extensions\ExtensionManager;
use LaravelDoctrine\ORM\Notifications\DoctrineChannel;
use LaravelDoctrine\ORM\Testing\Factory as EntityFactory;
use LaravelDoctrine\ORM\Validation\PresenceVerifierProvider;

use function assert;
use function class_exists;
use function config;
use function config_path;
use function database_path;

class DoctrineServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -68,6 +72,7 @@ public function register(): void
$this->registerExtensions();
$this->registerConsoleCommands();
$this->registerCustomTypes();
$this->registerEntityFactory();
$this->registerProxyAutoloader();

if (! $this->shouldRegisterDoctrinePresenceValidator()) {
Expand Down Expand Up @@ -270,6 +275,24 @@ public function extendNotificationChannel(): void
});
}

/**
* Register the Entity factory instance in the container.
*/
protected function registerEntityFactory(): void
{
$this->app->singleton(FakerGenerator::class, static function ($app) {
return FakerFactory::create($app['config']->get('app.faker_locale', 'en_US'));

Check warning on line 284 in src/DoctrineServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

src/DoctrineServiceProvider.php#L284

Added line #L284 was not covered by tests
});

$this->app->singleton(EntityFactory::class, static function ($app) {
return EntityFactory::construct(
$app->make(FakerGenerator::class),
$app->make('registry'),
database_path('factories'),
);

Check warning on line 292 in src/DoctrineServiceProvider.php

View check run for this annotation

Codecov / codecov/patch

src/DoctrineServiceProvider.php#L288-L292

Added lines #L288 - L292 were not covered by tests
});
}

/**
* Register proxy autoloader
*/
Expand Down

0 comments on commit 0c724f5

Please sign in to comment.