|
6 | 6 |
|
7 | 7 | use Illuminate\Database\Capsule\Manager; |
8 | 8 | use Nette\DI\CompilerExtension; |
| 9 | +use Nette\Schema\Expect; |
| 10 | +use Nette\Schema\Schema; |
9 | 11 |
|
10 | 12 | class EloquentExtension extends CompilerExtension |
11 | 13 | { |
| 14 | + public function getConfigSchema(): Schema |
| 15 | + { |
| 16 | + /** @var Schema */ |
| 17 | + $schema = Expect::arrayOf( |
| 18 | + Expect::array() |
| 19 | + )->before(fn ($value) => is_array(reset($value)) || null === reset($value) |
| 20 | + ? $value |
| 21 | + : ['default' => $value] |
| 22 | + ); |
| 23 | + |
| 24 | + return $schema; |
| 25 | + } |
| 26 | + |
12 | 27 | public function loadConfiguration() |
13 | 28 | { |
14 | 29 | $container = $this->getContainerBuilder(); |
15 | | - $config = $this->getConfig(); |
16 | 30 |
|
17 | | - $connection = $container->addDefinition($this->prefix('connection')) |
| 31 | + $manager = $container->addDefinition($this->prefix('manager')) |
18 | 32 | ->setFactory(Manager::class) |
19 | | - ->addSetup('addConnection', [$config]) |
20 | 33 | ->addSetup('setAsGlobal') |
21 | 34 | ->addSetup('bootEloquent') |
22 | 35 | ->setAutowired(true); |
23 | 36 |
|
24 | | - if ($container->parameters['debugMode']) { |
25 | | - $panel = $container->addDefinition($this->prefix('panel')) |
| 37 | + $autowired = true; |
| 38 | + foreach ($this->config as $name => $config) { |
| 39 | + $config['autowired'] ??= $autowired; |
| 40 | + |
| 41 | + $manager->addSetup('addConnection', [$config, $name]); |
| 42 | + |
| 43 | + $connection = $container->addDefinition($this->prefix("$name.connection")) |
| 44 | + ->setFactory([$manager, 'connection'], [$name]) |
| 45 | + ->setAutowired($config['autowired']); |
| 46 | + |
| 47 | + $autowired = false; |
| 48 | + |
| 49 | + if (!$container->parameters['debugMode']) { |
| 50 | + continue; |
| 51 | + } |
| 52 | + |
| 53 | + $panel = $container->addDefinition($this->prefix("$name.panel")) |
26 | 54 | ->setFactory(EloquentPanel::class) |
27 | 55 | ->setAutowired(false); |
28 | | - $connection->addSetup([$panel, 'register'], [$connection]); |
| 56 | + |
| 57 | + $connection->addSetup([$panel, 'register'], [$connection, $name]); |
29 | 58 | } |
30 | 59 | } |
31 | 60 | } |
0 commit comments