b2pweb /
bdf-prime-bundle
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Bdf\PrimeBundle\DependencyInjection; |
||
| 4 | |||
| 5 | use Bdf\Prime\Cache\DoctrineCacheAdapter; |
||
| 6 | use Bdf\Prime\Configuration as PrimeConfiguration; |
||
| 7 | use Bdf\Prime\Connection\ConnectionRegistry; |
||
| 8 | use Bdf\Prime\Connection\Factory\ConnectionFactory; |
||
| 9 | use Bdf\Prime\Connection\Factory\MasterSlaveConnectionFactory; |
||
| 10 | use Bdf\Prime\Connection\Factory\ShardingConnectionFactory; |
||
| 11 | use Bdf\Prime\Mapper\MapperFactory; |
||
| 12 | use Bdf\Prime\MongoDB\Collection\MongoCollectionLocator; |
||
|
0 ignored issues
–
show
|
|||
| 13 | use Bdf\Prime\MongoDB\Document\DocumentMapperInterface; |
||
|
0 ignored issues
–
show
The type
Bdf\Prime\MongoDB\Document\DocumentMapperInterface was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 14 | use Bdf\Prime\MongoDB\Document\Hydrator\DocumentHydratorFactory; |
||
|
0 ignored issues
–
show
The type
Bdf\Prime\MongoDB\Docume...DocumentHydratorFactory was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||
| 15 | use Bdf\Prime\Types\TypesRegistryInterface; |
||
| 16 | use Doctrine\Common\Cache\Psr6\DoctrineProvider; |
||
| 17 | use Symfony\Component\Cache\Psr16Cache; |
||
| 18 | use Symfony\Component\Config\FileLocator; |
||
| 19 | use Symfony\Component\Config\Loader\FileLoader; |
||
| 20 | use Symfony\Component\DependencyInjection\ChildDefinition; |
||
| 21 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 22 | use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; |
||
| 23 | use Symfony\Component\DependencyInjection\ExpressionLanguage; |
||
| 24 | use Symfony\Component\DependencyInjection\Extension\Extension; |
||
| 25 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
||
| 26 | use Symfony\Component\DependencyInjection\Reference; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * PrimeExtension |
||
| 30 | */ |
||
| 31 | class PrimeExtension extends Extension |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * {@inheritDoc} |
||
| 35 | */ |
||
| 36 | 6 | public function load(array $configs, ContainerBuilder $container) |
|
| 37 | { |
||
| 38 | 6 | $configuration = $this->getConfiguration($configs, $container); |
|
| 39 | 6 | $config = $this->processConfiguration($configuration, $configs); |
|
| 40 | |||
| 41 | 6 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config')); |
|
| 42 | 6 | $loader->load('prime.yaml'); |
|
| 43 | 6 | $loader->load('collector.yaml'); |
|
| 44 | |||
| 45 | 6 | $this->configureConnection($config, $container); |
|
| 46 | 6 | $this->configureMapperCache($config, $container); |
|
| 47 | 6 | $this->configureSerializer($config, $container); |
|
| 48 | |||
| 49 | 6 | if (class_exists(MongoCollectionLocator::class)) { |
|
| 50 | $this->configureMongo($loader, $container, $config); |
||
| 51 | } |
||
| 52 | |||
| 53 | 6 | $container->setParameter('prime.default_connection', $config['default_connection']); |
|
| 54 | 6 | $container->setParameter('prime.migration.connection', $config['migration']['connection']); |
|
| 55 | 6 | $container->setParameter('prime.migration.table', $config['migration']['table']); |
|
| 56 | 6 | $container->setParameter('prime.migration.path', $config['migration']['path']); |
|
| 57 | 6 | $container->setParameter('prime.hydrators', $config['hydrators']); |
|
| 58 | 6 | $container->setParameter('prime.locatorizable', $config['activerecord']); |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @param array $config |
||
| 63 | * @param ContainerBuilder $container |
||
| 64 | */ |
||
| 65 | 6 | public function configureConnection(array $config, ContainerBuilder $container) |
|
| 66 | { |
||
| 67 | 6 | foreach ($config['connections'] as $name => &$options) { |
|
| 68 | 6 | $options = $this->cleanConnectionOptions($options); |
|
| 69 | |||
| 70 | // Overwrite global config by the connection config parameters and create the configuration reference. |
||
| 71 | 6 | $this->createConfiguration($name, $this->mergeConfiguration($config, $options), $container); |
|
| 72 | |||
| 73 | 6 | if (!$container->hasDefinition(MasterSlaveConnectionFactory::class) && $this->hasConnectionOption('read', $options)) { |
|
| 74 | 6 | $container->register(MasterSlaveConnectionFactory::class, MasterSlaveConnectionFactory::class) |
|
| 75 | 6 | ->addTag('bdf_prime.connection_factory', ['priority' => -255]) |
|
| 76 | 6 | ->addArgument(new Reference(ConnectionFactory::class)); |
|
| 77 | } |
||
| 78 | |||
| 79 | 6 | if (!$container->hasDefinition(ShardingConnectionFactory::class) && $this->hasConnectionOption('shards', $options)) { |
|
| 80 | 6 | $container->register(ShardingConnectionFactory::class, ShardingConnectionFactory::class) |
|
| 81 | 6 | ->addTag('bdf_prime.connection_factory', ['priority' => -256]) |
|
| 82 | 6 | ->addArgument(new Reference(ConnectionFactory::class)); |
|
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | 6 | $registry = $container->getDefinition(ConnectionRegistry::class); |
|
| 87 | 6 | $registry->replaceArgument(0, $config['connections']); |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @param string $option |
||
| 92 | * @param array $options |
||
| 93 | * @return bool |
||
| 94 | */ |
||
| 95 | 6 | private function hasConnectionOption(string $option, array $options): bool |
|
| 96 | { |
||
| 97 | 6 | if (isset($options[$option])) { |
|
| 98 | 1 | return true; |
|
| 99 | } |
||
| 100 | |||
| 101 | 6 | if (!isset($options['url'])) { |
|
| 102 | return false; |
||
| 103 | } |
||
| 104 | |||
| 105 | // The option could be in the url. Adding the factory by default. |
||
| 106 | 6 | return true; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $config |
||
| 111 | * @param ContainerBuilder $container |
||
| 112 | */ |
||
| 113 | 6 | public function configureSerializer(array $config, ContainerBuilder $container) |
|
| 114 | { |
||
| 115 | 6 | if (!isset($config['serializer'])) { |
|
| 116 | 6 | return; |
|
| 117 | } |
||
| 118 | |||
| 119 | $prime = $container->findDefinition('prime'); |
||
| 120 | $prime->addMethodCall('setSerializer', [new Reference($config['serializer'])]); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $config |
||
| 125 | * @param ContainerBuilder $container |
||
| 126 | */ |
||
| 127 | 6 | public function configureMapperCache(array $config, ContainerBuilder $container) |
|
| 128 | { |
||
| 129 | 6 | $definition = $container->getDefinition(MapperFactory::class); |
|
| 130 | |||
| 131 | 6 | if (isset($config['cache']['query'])) { |
|
| 132 | 2 | $ref = $this->createResultCacheReference('prime.cache.query', $config['cache']['query'], $container); |
|
| 133 | |||
| 134 | 2 | if ($ref !== null) { |
|
| 135 | 2 | $definition->replaceArgument(2, $ref); |
|
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | 6 | if (isset($config['cache']['metadata'])) { |
|
| 140 | $ref = $this->createCacheReference('prime.cache.metadata', $config['cache']['metadata'], $container); |
||
| 141 | |||
| 142 | if ($ref !== null) { |
||
| 143 | $definition->replaceArgument(1, $ref); |
||
| 144 | } |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param FileLoader $loader |
||
| 150 | * @param ContainerBuilder $container |
||
| 151 | * @param array $config |
||
| 152 | * @return void |
||
| 153 | * @throws \Exception |
||
| 154 | */ |
||
| 155 | public function configureMongo(FileLoader $loader, ContainerBuilder $container, array $config): void |
||
| 156 | { |
||
| 157 | $loader->load('prime_mongo.yaml'); |
||
| 158 | |||
| 159 | $container->registerForAutoconfiguration(DocumentMapperInterface::class) |
||
| 160 | ->setPublic(true) |
||
| 161 | ->setShared(false) |
||
| 162 | ->setAutowired(true) |
||
| 163 | ; |
||
| 164 | |||
| 165 | if (isset($config['cache']['metadata'])) { |
||
| 166 | $definition = $container->findDefinition(DocumentHydratorFactory::class); |
||
| 167 | $ref = $this->createCacheReference('prime.cache.metadata', $config['cache']['metadata'], $container); |
||
| 168 | |||
| 169 | if ($ref !== null) { |
||
| 170 | $definition->replaceArgument(0, $ref); |
||
| 171 | } |
||
| 172 | } |
||
| 173 | } |
||
| 174 | |||
| 175 | /** |
||
| 176 | * @param array $globalConfig |
||
| 177 | * @param array $config |
||
| 178 | * |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | 6 | public function mergeConfiguration(array $globalConfig, array $config): array |
|
| 182 | { |
||
| 183 | return [ |
||
| 184 | 6 | 'types' => array_merge($globalConfig['types'], $config['types']), |
|
| 185 | 6 | 'auto_commit' => $config['auto_commit'] ?? $globalConfig['auto_commit'], |
|
| 186 | 6 | 'logging' => $config['logging'] ?? $globalConfig['logging'], |
|
| 187 | 6 | 'profiling' => $config['profiling'] ?? $globalConfig['profiling'], |
|
| 188 | ]; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Create and declare the configuration definition of the connection |
||
| 193 | * |
||
| 194 | * @param string $name |
||
| 195 | * @param array $config |
||
| 196 | * @param ContainerBuilder $container |
||
| 197 | */ |
||
| 198 | 6 | public function createConfiguration(string $name, array $config, ContainerBuilder $container): void |
|
| 199 | { |
||
| 200 | 6 | $namespace = "prime.{$name}_connection"; |
|
| 201 | |||
| 202 | 6 | $configuration = $container->setDefinition("$namespace.configuration", new ChildDefinition(PrimeConfiguration::class)); |
|
| 203 | 6 | $configuration->setPublic(true); |
|
| 204 | 6 | $configuration->addMethodCall('setTypes', [new Reference("$namespace.types")]); |
|
| 205 | |||
| 206 | 6 | $typeRegistry = $container->setDefinition("$namespace.types", new ChildDefinition(TypesRegistryInterface::class)); |
|
| 207 | 6 | foreach ($config['types'] as $type => $info) { |
|
| 208 | 2 | $typeRegistry->addMethodCall('register', [$info['class'], is_int($type) ? null : $type]); |
|
| 209 | } |
||
| 210 | |||
| 211 | 6 | if (isset($config['auto_commit'])) { |
|
| 212 | 2 | $configuration->addMethodCall('setAutoCommit', [$config['auto_commit']]); |
|
| 213 | } |
||
| 214 | |||
| 215 | 6 | $logger = null; |
|
| 216 | 6 | if ($config['logging']) { |
|
| 217 | 6 | $logger = new Reference('prime.logger'); |
|
| 218 | } |
||
| 219 | |||
| 220 | 6 | if ($config['profiling']) { |
|
| 221 | 6 | $profilingLogger = new Reference('prime.logger.profiling'); |
|
| 222 | |||
| 223 | 6 | if ($logger !== null) { |
|
| 224 | 6 | $chainLogger = $container->findDefinition('prime.logger.chain'); |
|
| 225 | 6 | $chainLogger->replaceArgument(0, [$logger, $profilingLogger]); |
|
| 226 | |||
| 227 | 6 | $logger = new Reference('prime.logger.chain'); |
|
| 228 | } else { |
||
| 229 | $logger = $profilingLogger; |
||
| 230 | } |
||
| 231 | } |
||
| 232 | |||
| 233 | 6 | if ($logger) { |
|
| 234 | 6 | $configuration->addMethodCall('setSQLLogger', [$logger]); |
|
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @param array $config |
||
| 240 | * @param ContainerBuilder $container |
||
| 241 | * |
||
| 242 | * @return null|Reference |
||
| 243 | */ |
||
| 244 | private function createCacheReference(string $namespace, array $config, ContainerBuilder $container) |
||
| 245 | { |
||
| 246 | if (isset($config['service'])) { |
||
| 247 | return new Reference($config['service']); |
||
| 248 | } |
||
| 249 | |||
| 250 | if (isset($config['pool'])) { |
||
| 251 | if (!$container->has($namespace)) { |
||
| 252 | $definition = $container->register($namespace, Psr16Cache::class); |
||
| 253 | $definition->addArgument(new Reference($config['pool'])); |
||
| 254 | } |
||
| 255 | |||
| 256 | return new Reference($namespace); |
||
| 257 | } |
||
| 258 | |||
| 259 | return null; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Create the cache result service |
||
| 264 | * |
||
| 265 | * @param array $config |
||
| 266 | * @param ContainerBuilder $container |
||
| 267 | * |
||
| 268 | * @return null|Reference |
||
| 269 | */ |
||
| 270 | 2 | private function createResultCacheReference(string $namespace, array $config, ContainerBuilder $container) |
|
| 271 | { |
||
| 272 | 2 | if (isset($config['service'])) { |
|
| 273 | 1 | return new Reference($config['service']); |
|
| 274 | } |
||
| 275 | |||
| 276 | 1 | if (isset($config['pool'])) { |
|
| 277 | 1 | if (!$container->has($namespace)) { |
|
| 278 | 1 | $definition = $container->register($namespace.'.doctrine-provider', DoctrineProvider::class); |
|
| 279 | 1 | $definition->setFactory([DoctrineProvider::class, 'wrap']); |
|
| 280 | 1 | $definition->addArgument(new Reference($config['pool'])); |
|
| 281 | |||
| 282 | 1 | $definition = $container->register($namespace, DoctrineCacheAdapter::class); |
|
| 283 | 1 | $definition->addArgument(new Reference($namespace.'.doctrine-provider')); |
|
| 284 | } |
||
| 285 | |||
| 286 | 1 | return new Reference($namespace); |
|
| 287 | } |
||
| 288 | |||
| 289 | return null; |
||
| 290 | } |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Rearrange the key name of the configuration |
||
| 294 | * |
||
| 295 | * @param array $options |
||
| 296 | * |
||
| 297 | * @return array |
||
| 298 | */ |
||
| 299 | 6 | private function cleanConnectionOptions(array $options): array |
|
| 300 | { |
||
| 301 | 6 | if (isset($options['platform_service'])) { |
|
| 302 | $options['platform'] = new Reference($options['platform_service']); |
||
| 303 | unset($options['platform_service']); |
||
| 304 | } |
||
| 305 | |||
| 306 | // unset($options['mapping_types']); |
||
| 307 | |||
| 308 | 6 | if (isset($options['shard_choser'])) { |
|
| 309 | $options['shard_choser'] = new Reference($options['shard_choser']); |
||
| 310 | } |
||
| 311 | |||
| 312 | $parameters = [ |
||
| 313 | 'options' => 'driverOptions', |
||
| 314 | 'driver_class' => 'driverClass', |
||
| 315 | 'wrapper_class' => 'wrapperClass', |
||
| 316 | 'shard_choser' => 'shardChoser', |
||
| 317 | 'distribution_key' => 'distributionKey', |
||
| 318 | 'server_version' => 'serverVersion', |
||
| 319 | 'default_table_options' => 'defaultTableOptions', |
||
| 320 | ]; |
||
| 321 | |||
| 322 | 6 | foreach ($parameters as $old => $new) { |
|
| 323 | 6 | if (isset($options[$old])) { |
|
| 324 | 6 | $options[$new] = $options[$old]; |
|
| 325 | 6 | unset($options[$old]); |
|
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | 6 | if (!empty($options['read']) && !empty($options['shards'])) { |
|
| 330 | throw new InvalidArgumentException('Sharding and master-slave connection cannot be used together'); |
||
| 331 | } |
||
| 332 | |||
| 333 | |||
| 334 | 6 | $parameters = ['read', 'shards', 'driverOptions', 'defaultTableOptions']; |
|
| 335 | |||
| 336 | 6 | foreach ($parameters as $key) { |
|
| 337 | 6 | if (empty($options[$key])) { |
|
| 338 | 6 | unset($options[$key]); |
|
| 339 | } |
||
| 340 | } |
||
| 341 | |||
| 342 | 6 | return $options; |
|
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * {@inheritDoc} |
||
| 347 | */ |
||
| 348 | 6 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 349 | { |
||
| 350 | 6 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 351 | } |
||
| 352 | } |
||
| 353 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths