|
3 | 3 | namespace Bermuda\Cycle; |
4 | 4 |
|
5 | 5 | use Bermuda\Config\ConfigProvider as AbstractProvider; |
| 6 | +use Bermuda\Cycle\Typecast\TypecastHandler; |
| 7 | +use Bermuda\Cycle\Typecast\TypecastHandlerFactory; |
6 | 8 | use Cycle\Database\Config\DatabaseConfig; |
7 | 9 | use Cycle\Database\DatabaseManager; |
8 | 10 | use Cycle\Database\DatabaseProviderInterface; |
|
26 | 28 | use Psr\Log\LoggerInterface; |
27 | 29 | use Psr\Log\NullLogger; |
28 | 30 |
|
| 31 | +use function Bermuda\Config\conf; |
| 32 | + |
29 | 33 | class ConfigProvider extends AbstractProvider |
30 | 34 | { |
31 | | - public const configKey = 'cycle'; |
| 35 | + public const string CONFIG_KEY = 'cycle'; |
32 | 36 | protected function getFactories(): array |
33 | 37 | { |
34 | 38 | return [ |
35 | | - ORMInterface::class => static function (ContainerInterface $container): ORM { |
36 | | - return new ORM($container->get(FactoryInterface::class), $container->get(SchemaInterface::class), |
37 | | - $container->has(CommandGeneratorInterface::class) ? |
38 | | - $container->get(CommandGeneratorInterface::class) : null, |
39 | | - $container->get(HeapInterface::class) |
40 | | - ); |
41 | | - }, |
42 | | - DatabaseProviderInterface::class => static function (ContainerInterface $container): DatabaseManager { |
43 | | - return new DatabaseManager($container->get(DatabaseConfig::class), $container->get(LoggerFactoryInterface::class)); |
44 | | - }, |
45 | | - LoggerFactoryInterface::class => static function (): LoggerFactoryInterface { |
46 | | - return new class implements LoggerFactoryInterface { |
47 | | - public function getLogger(DriverInterface $driver = null): LoggerInterface |
48 | | - { |
49 | | - return new NullLogger; |
50 | | - } |
51 | | - }; |
52 | | - }, |
53 | | - DatabaseConfig::class => static function (ContainerInterface $container): DatabaseConfig { |
54 | | - $config = $container->get(self::containerConfigKey); |
55 | | - if (!isset($config[self::configKey])) { |
56 | | - throw new \RuntimeException('Database configuration expected'); |
57 | | - } |
58 | | - return new DatabaseConfig($config[self::configKey][0]); |
59 | | - }, |
60 | | - HeapInterface::class => static fn(): Heap => new Heap, |
61 | | - FactoryInterface::class => static function (ContainerInterface $container): Factory { |
62 | | - return new Factory($container->get(DatabaseProviderInterface::class), |
63 | | - defaultCollectionFactory: $container->get(CollectionFactoryInterface::class) |
64 | | - ); |
65 | | - }, |
66 | | - CollectionFactoryInterface::class => static function (): DoctrineCollectionFactory { |
67 | | - return new DoctrineCollectionFactory(); |
68 | | - }, |
69 | | - SchemaInterface::class => static function (ContainerInterface $container): Schema { |
70 | | - $config = $container->get(self::containerConfigKey); |
71 | | - if (!isset($config[self::configKey])) { |
72 | | - throw new \RuntimeException('Database configuration expected'); |
73 | | - } |
74 | | - return new Schema($config[self::configKey][1]); |
75 | | - }, |
76 | | - EntityManagerInterface::class => static function (ContainerInterface $container): EntityManager { |
77 | | - return new EntityManager($container->get(ORMInterface::class)); |
78 | | - }, |
| 39 | + ORMInterface::class => [ConfigProvider::class, 'createORM'], |
| 40 | + DatabaseProviderInterface::class => [ConfigProvider::class, 'createDatabaseManager'], |
| 41 | + LoggerFactoryInterface::class => [ConfigProvider::class, 'createLoggerFactory'], |
| 42 | + DatabaseConfig::class => [ConfigProvider::class, 'createDatabaseConfig'], |
| 43 | + HeapInterface::class => [ConfigProvider::class, 'createHeap'], |
| 44 | + FactoryInterface::class => [ConfigProvider::class, 'createFactory'], |
| 45 | + CollectionFactoryInterface::class => [ConfigProvider::class, 'createCollectionFactory'], |
| 46 | + SchemaInterface::class => [ConfigProvider::class, 'createSchema'], |
| 47 | + EntityManagerInterface::class => [ConfigProvider::class, 'createEntityManager'], |
| 48 | + TypecastHandler::class => TypecastHandlerFactory::class |
79 | 49 | ]; |
80 | 50 | } |
| 51 | + |
| 52 | + public static function createORM(ContainerInterface $container): ORM |
| 53 | + { |
| 54 | + return new ORM($container->get(FactoryInterface::class), $container->get(SchemaInterface::class), |
| 55 | + $container->has(CommandGeneratorInterface::class) ? |
| 56 | + $container->get(CommandGeneratorInterface::class) : null, |
| 57 | + $container->get(HeapInterface::class) |
| 58 | + ); |
| 59 | + } |
| 60 | + |
| 61 | + public static function createDatabaseManager(ContainerInterface $container): DatabaseManager |
| 62 | + { |
| 63 | + return new DatabaseManager($container->get(DatabaseConfig::class), $container->get(LoggerFactoryInterface::class)); |
| 64 | + } |
| 65 | + |
| 66 | + public static function createLoggerFactory(ContainerInterface $container): LoggerFactoryInterface |
| 67 | + { |
| 68 | + return new class implements LoggerFactoryInterface { |
| 69 | + public function getLogger(?DriverInterface $driver = null): LoggerInterface |
| 70 | + { |
| 71 | + return new NullLogger; |
| 72 | + } |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + public static function createDatabaseConfig(ContainerInterface $container): DatabaseConfig |
| 77 | + { |
| 78 | + $config = conf($container); |
| 79 | + if (!isset($config[self::CONFIG_KEY])) { |
| 80 | + throw new \RuntimeException('Database configuration expected'); |
| 81 | + } |
| 82 | + return new DatabaseConfig($config[self::CONFIG_KEY][0]->toArray()); |
| 83 | + } |
| 84 | + |
| 85 | + public static function createHeap(ContainerInterface $container): Heap |
| 86 | + { |
| 87 | + return new Heap(); |
| 88 | + } |
| 89 | + |
| 90 | + public static function createFactory(ContainerInterface $container): Factory |
| 91 | + { |
| 92 | + return new Factory($container->get(DatabaseProviderInterface::class), |
| 93 | + defaultCollectionFactory: $container->get(CollectionFactoryInterface::class) |
| 94 | + ); |
| 95 | + } |
| 96 | + |
| 97 | + public static function createCollectionFactory(ContainerInterface $container): CollectionFactoryInterface |
| 98 | + { |
| 99 | + return new DoctrineCollectionFactory(); |
| 100 | + } |
| 101 | + |
| 102 | + public static function createSchema(ContainerInterface $container): Schema |
| 103 | + { |
| 104 | + $config = conf($container); |
| 105 | + if (!isset($config[self::CONFIG_KEY])) { |
| 106 | + throw new \RuntimeException('Database configuration expected'); |
| 107 | + } |
| 108 | + |
| 109 | + return new Schema($config[self::CONFIG_KEY][1]); |
| 110 | + } |
| 111 | + |
| 112 | + public static function createEntityManager(ContainerInterface $container): EntityManager |
| 113 | + { |
| 114 | + return new EntityManager($container->get(ORMInterface::class)); |
| 115 | + } |
81 | 116 | } |
0 commit comments