-
Notifications
You must be signed in to change notification settings - Fork 0
/
EloquentBundle.php
65 lines (52 loc) · 1.71 KB
/
EloquentBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace Pandawa\Bundle\EloquentBundle;
use Faker\Factory as FakerFactory;
use Faker\Generator as FakerGenerator;
use Illuminate\Database\Eloquent\Model;
use Pandawa\Bundle\DependencyInjectionBundle\Plugin\ImportServicesPlugin;
use Pandawa\Bundle\FoundationBundle\Plugin\ImportConfigurationPlugin;
use Pandawa\Component\Foundation\Bundle\Bundle;
use Pandawa\Contracts\Foundation\HasPluginInterface;
/**
* @author Iqbal Maulana <iq.bluejack@gmail.com>
*/
class EloquentBundle extends Bundle implements HasPluginInterface
{
const REPOSITORY_CONFIG_KEY = 'pandawa.repositories';
const OBSERVER_CONFIG_KEY = 'pandawa.observers';
/**
* The array of resolved Faker instances.
*
* @var array
*/
protected static array $fakers = [];
public function boot(): void
{
Model::setConnectionResolver($this->app['db']);
Model::setEventDispatcher($this->app['events']);
}
public function register(): void
{
Model::clearBootedModels();
$this->registerEloquentFactory();
}
public function plugins(): array
{
return [
new ImportConfigurationPlugin(),
new ImportServicesPlugin(),
];
}
protected function registerEloquentFactory(): void
{
$this->app->singleton(FakerGenerator::class, function ($app, $parameters) {
$locale = $parameters['locale'] ?? $app['config']->get('app.faker_locale', 'en_US');
if (!isset(static::$fakers[$locale])) {
static::$fakers[$locale] = FakerFactory::create($locale);
}
static::$fakers[$locale]->unique(true);
return static::$fakers[$locale];
});
}
}