| 1 | <?php |
||
| 23 | class Neo4jExtension extends Extension |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * {@inheritdoc} |
||
| 27 | */ |
||
| 28 | 4 | public function load(array $configs, ContainerBuilder $container) |
|
| 29 | { |
||
| 30 | 4 | $configuration = $this->getConfiguration($configs, $container); |
|
| 31 | 4 | $config = $this->processConfiguration($configuration, $configs); |
|
|
1 ignored issue
–
show
|
|||
| 32 | |||
| 33 | 4 | $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 34 | 4 | $loader->load('services.xml'); |
|
| 35 | |||
| 36 | 4 | $this->handleConnections($config, $container); |
|
| 37 | 4 | $clientServiceIds = $this->handleClients($config, $container); |
|
| 38 | |||
| 39 | 4 | if ($this->validateEntityManagers($config)) { |
|
| 40 | $loader->load('entity_manager.xml'); |
||
| 41 | $this->handleEntityMangers($config, $container, $clientServiceIds); |
||
| 42 | $container->setAlias('neo4j.entity_manager', 'neo4j.entity_manager.default'); |
||
| 43 | } |
||
| 44 | |||
| 45 | // add aliases for the default services |
||
| 46 | 4 | $container->setAlias('neo4j.connection', 'neo4j.connection.default'); |
|
| 47 | 4 | $container->setAlias('neo4j.client', 'neo4j.client.default'); |
|
| 48 | |||
| 49 | // Configure toolbar |
||
| 50 | 4 | if ($this->isConfigEnabled($container, $config['profiling'])) { |
|
| 51 | 2 | $loader->load('data-collector.xml'); |
|
| 52 | } |
||
| 53 | 4 | } |
|
| 54 | |||
| 55 | /** |
||
| 56 | * {@inheritdoc} |
||
| 57 | */ |
||
| 58 | 4 | public function getConfiguration(array $config, ContainerBuilder $container): Configuration |
|
| 62 | |||
| 63 | /** |
||
| 64 | * {@inheritdoc} |
||
| 65 | */ |
||
| 66 | 4 | public function getAlias(): string |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param array $config |
||
| 73 | * @param ContainerBuilder $container |
||
| 74 | * |
||
| 75 | * @return array with service ids |
||
| 76 | */ |
||
| 77 | 4 | private function handleClients(array &$config, ContainerBuilder $container): array |
|
| 108 | |||
| 109 | /** |
||
| 110 | * @param array $config |
||
| 111 | * @param ContainerBuilder $container |
||
| 112 | * @param array $clientServiceIds |
||
| 113 | * |
||
| 114 | * @return array |
||
| 115 | */ |
||
| 116 | private function handleEntityMangers(array &$config, ContainerBuilder $container, array $clientServiceIds): array |
||
| 117 | { |
||
| 118 | $serviceIds = []; |
||
| 119 | foreach ($config['entity_managers'] as $name => $data) { |
||
| 120 | $serviceIds[] = $serviceId = sprintf('neo4j.entity_manager.%s', $name); |
||
| 121 | $clientName = $data['client']; |
||
| 122 | if (empty($clientServiceIds[$clientName])) { |
||
| 123 | throw new InvalidConfigurationException(sprintf( |
||
| 124 | 'EntityManager "%s" is configured to use client named "%s" but there is no such client', |
||
| 125 | $name, |
||
| 126 | $clientName |
||
| 127 | )); |
||
| 128 | } |
||
| 129 | $container |
||
| 130 | ->setDefinition($serviceId, new DefinitionDecorator('neo4j.entity_manager.abstract')) |
||
| 131 | ->setArguments([ |
||
| 132 | $container->getDefinition($clientServiceIds[$clientName]), |
||
| 133 | empty($data['cache_dir']) ? $container->getParameter('kernel.cache_dir').'/neo4j' : $data['cache_dir'], |
||
| 134 | ]); |
||
| 135 | } |
||
| 136 | |||
| 137 | return $serviceIds; |
||
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param array $config |
||
| 142 | * @param ContainerBuilder $container |
||
| 143 | * |
||
| 144 | * @return array with service ids |
||
| 145 | */ |
||
| 146 | 4 | private function handleConnections(array &$config, ContainerBuilder $container): array |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Get URL form config. |
||
| 178 | * |
||
| 179 | * @param array $config |
||
| 180 | * |
||
| 181 | * @return string |
||
| 182 | */ |
||
| 183 | 4 | private function getUrl(array $config): string |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Return the correct default port if not manually set. |
||
| 197 | * |
||
| 198 | * @param array $config |
||
| 199 | * |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | 4 | private function getPort(array $config) |
|
| 210 | |||
| 211 | /** |
||
| 212 | * Make sure the EntityManager is installed if we have configured it. |
||
| 213 | * |
||
| 214 | * @param array &$config |
||
| 215 | * |
||
| 216 | * @return bool true if "graphaware/neo4j-php-ogm" is installed |
||
| 217 | * |
||
| 218 | * @thorws \LogicException if EntityManagers os not installed but they are configured. |
||
| 219 | */ |
||
| 220 | 4 | private function validateEntityManagers(array &$config): bool |
|
| 236 | } |
||
| 237 |
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: