geocoder-php /
BazingaGeocoderBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | /* |
||
| 6 | * This file is part of the BazingaGeocoderBundle package. |
||
| 7 | * For the full copyright and license information, please view the LICENSE |
||
| 8 | * file that was distributed with this source code. |
||
| 9 | * |
||
| 10 | * @license MIT License |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Bazinga\GeocoderBundle\DependencyInjection; |
||
| 14 | |||
| 15 | use Bazinga\GeocoderBundle\DataCollector\GeocoderDataCollector; |
||
| 16 | use Bazinga\GeocoderBundle\DependencyInjection\Compiler\FactoryValidatorPass; |
||
| 17 | use Bazinga\GeocoderBundle\Plugin\FakeIpPlugin; |
||
| 18 | use Bazinga\GeocoderBundle\Plugin\ProfilingPlugin; |
||
| 19 | use Bazinga\GeocoderBundle\ProviderFactory\PluginProviderFactory; |
||
| 20 | use Bazinga\GeocoderBundle\ProviderFactory\ProviderFactoryInterface; |
||
| 21 | use Faker\Generator; |
||
| 22 | use Geocoder\Dumper\Dumper; |
||
| 23 | use Geocoder\Plugin\Plugin\CachePlugin; |
||
| 24 | use Geocoder\Plugin\Plugin\LimitPlugin; |
||
| 25 | use Geocoder\Plugin\Plugin\LocalePlugin; |
||
| 26 | use Geocoder\Plugin\Plugin\LoggerPlugin; |
||
| 27 | use Geocoder\Plugin\PluginProvider; |
||
| 28 | use Geocoder\Provider\Provider; |
||
| 29 | use Symfony\Component\Config\Definition\Processor; |
||
| 30 | use Symfony\Component\Config\FileLocator; |
||
| 31 | use Symfony\Component\DependencyInjection\ContainerBuilder; |
||
| 32 | use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException; |
||
| 33 | use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; |
||
| 34 | use Symfony\Component\DependencyInjection\Reference; |
||
| 35 | use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
||
| 36 | use Symfony\Component\HttpKernel\Kernel; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @author William Durand <[email protected]>. |
||
| 40 | */ |
||
| 41 | class BazingaGeocoderExtension extends Extension |
||
| 42 | { |
||
| 43 | 33 | public function load(array $configs, ContainerBuilder $container) |
|
| 44 | { |
||
| 45 | 33 | $processor = new Processor(); |
|
| 46 | 33 | $configuration = $this->getConfiguration($configs, $container); |
|
| 47 | 33 | $config = $processor->processConfiguration($configuration, $configs); |
|
|
0 ignored issues
–
show
|
|||
| 48 | |||
| 49 | 33 | $loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
|
| 50 | 33 | $loader->load('services.yml'); |
|
| 51 | |||
| 52 | 33 | if (true === $config['profiling']['enabled']) { |
|
| 53 | 3 | $loader->load('profiling.yml'); |
|
| 54 | } |
||
| 55 | |||
| 56 | 33 | if ($config['fake_ip']['enabled']) { |
|
| 57 | $definition = $container->getDefinition(FakeIpPlugin::class); |
||
| 58 | $definition->replaceArgument(0, $config['fake_ip']['local_ip']); |
||
| 59 | $definition->replaceArgument(1, $config['fake_ip']['ip']); |
||
| 60 | $definition->replaceArgument(2, $config['fake_ip']['use_faker']); |
||
| 61 | |||
| 62 | if ($config['fake_ip']['use_faker'] && !class_exists(Generator::class)) { |
||
| 63 | throw new \LogicException('To enable this option, you must install fzaninotto/faker package.'); |
||
| 64 | } |
||
| 65 | } else { |
||
| 66 | 33 | $container->removeDefinition(FakeIpPlugin::class); |
|
| 67 | } |
||
| 68 | |||
| 69 | 33 | $this->loadProviders($container, $config); |
|
| 70 | |||
| 71 | 33 | $container->registerForAutoconfiguration(Dumper::class) |
|
| 72 | 33 | ->addTag('bazinga_geocoder.dumper'); |
|
| 73 | 33 | } |
|
| 74 | |||
| 75 | 33 | private function loadProviders(ContainerBuilder $container, array $config) |
|
| 76 | { |
||
| 77 | 33 | foreach ($config['providers'] as $providerName => $providerConfig) { |
|
| 78 | try { |
||
| 79 | 31 | $factoryService = $container->getDefinition($providerConfig['factory']); |
|
| 80 | 31 | $factoryClass = $factoryService->getClass() ?: $providerConfig['factory']; |
|
| 81 | 31 | if (!$this->implementsProviderFactory($factoryClass)) { |
|
| 82 | throw new \LogicException(sprintf('Provider factory "%s" must implement ProviderFactoryInterface', $providerConfig['factory'])); |
||
| 83 | } |
||
| 84 | // See if any option has a service reference |
||
| 85 | 31 | $providerConfig['options'] = $this->findReferences($providerConfig['options']); |
|
| 86 | 31 | $factoryClass::validate($providerConfig['options'], $providerName); |
|
| 87 | } catch (ServiceNotFoundException $e) { |
||
| 88 | // Assert: We are using a custom factory. If invalid config, it will be caught in FactoryValidatorPass |
||
| 89 | $providerConfig['options'] = $this->findReferences($providerConfig['options']); |
||
| 90 | FactoryValidatorPass::addFactoryServiceId($providerConfig['factory']); |
||
| 91 | } |
||
| 92 | |||
| 93 | 31 | $serviceId = 'bazinga_geocoder.provider.'.$providerName; |
|
| 94 | 31 | $plugins = $this->configureProviderPlugins($container, $providerConfig, $serviceId); |
|
| 95 | |||
| 96 | 31 | $def = $container->register($serviceId, PluginProvider::class) |
|
| 97 | 31 | ->setFactory([PluginProviderFactory::class, 'createPluginProvider']) |
|
| 98 | 31 | ->addArgument($plugins) |
|
| 99 | 31 | ->addArgument(new Reference($providerConfig['factory'])) |
|
| 100 | 31 | ->addArgument($providerConfig['options']); |
|
| 101 | |||
| 102 | 31 | $def->addTag('bazinga_geocoder.provider'); |
|
| 103 | 31 | foreach ($providerConfig['aliases'] as $alias) { |
|
| 104 | $container->setAlias($alias, $serviceId); |
||
| 105 | } |
||
| 106 | |||
| 107 | 31 | if (Kernel::VERSION_ID > 40200) { |
|
| 108 | 31 | $container->registerAliasForArgument($serviceId, Provider::class, "{$providerName}Geocoder"); |
|
| 109 | } |
||
| 110 | } |
||
| 111 | 33 | } |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Configure plugins for a client. |
||
| 115 | */ |
||
| 116 | 31 | public function configureProviderPlugins(ContainerBuilder $container, array $config, string $providerServiceId): array |
|
| 117 | { |
||
| 118 | 31 | $plugins = []; |
|
| 119 | 31 | foreach ($config['plugins'] as $plugin) { |
|
| 120 | 2 | if ($plugin['reference']['enabled']) { |
|
| 121 | 2 | $plugins[] = $plugin['reference']['id']; |
|
| 122 | } |
||
| 123 | } |
||
| 124 | |||
| 125 | 31 | if (isset($config['cache']) || isset($config['cache_lifetime']) || isset($config['cache_precision'])) { |
|
| 126 | 2 | $cacheLifetime = isset($config['cache_lifetime']) ? (int) $config['cache_lifetime'] : null; |
|
| 127 | |||
| 128 | 2 | if (null === $cacheServiceId = $config['cache']) { |
|
| 129 | if (!$container->has('app.cache')) { |
||
| 130 | throw new \LogicException('You need to specify a service for cache.'); |
||
| 131 | } |
||
| 132 | $cacheServiceId = 'app.cache'; |
||
| 133 | } |
||
| 134 | 2 | $plugins[] = $providerServiceId.'.cache'; |
|
| 135 | 2 | $container->register($providerServiceId.'.cache', CachePlugin::class) |
|
| 136 | 2 | ->setPublic(false) |
|
| 137 | 2 | ->setArguments([new Reference($cacheServiceId), $cacheLifetime, $config['cache_precision']]); |
|
| 138 | } |
||
| 139 | |||
| 140 | 31 | View Code Duplication | if (isset($config['limit'])) { |
| 141 | $plugins[] = $providerServiceId.'.limit'; |
||
| 142 | $container->register($providerServiceId.'.limit', LimitPlugin::class) |
||
| 143 | ->setPublic(false) |
||
| 144 | ->setArguments([(int) $config['limit']]); |
||
| 145 | } |
||
| 146 | |||
| 147 | 31 | View Code Duplication | if (isset($config['locale'])) { |
| 148 | $plugins[] = $providerServiceId.'.locale'; |
||
| 149 | $container->register($providerServiceId.'.locale', LocalePlugin::class) |
||
| 150 | ->setPublic(false) |
||
| 151 | ->setArguments([$config['locale']]); |
||
| 152 | } |
||
| 153 | |||
| 154 | 31 | View Code Duplication | if (isset($config['logger'])) { |
| 155 | $plugins[] = $providerServiceId.'.logger'; |
||
| 156 | $container->register($providerServiceId.'.logger', LoggerPlugin::class) |
||
| 157 | ->setPublic(false) |
||
| 158 | ->setArguments([new Reference($config['logger'])]); |
||
| 159 | } |
||
| 160 | |||
| 161 | 31 | if ($container->has(FakeIpPlugin::class)) { |
|
| 162 | $plugins[] = FakeIpPlugin::class; |
||
| 163 | } |
||
| 164 | |||
| 165 | 31 | if ($container->has(GeocoderDataCollector::class)) { |
|
| 166 | 1 | $plugins[] = $providerServiceId.'.profiler'; |
|
| 167 | 1 | $container->register($providerServiceId.'.profiler', ProfilingPlugin::class) |
|
| 168 | 1 | ->setPublic(false) |
|
| 169 | 1 | ->setArguments([substr($providerServiceId, strlen('bazinga_geocoder.provider.'))]) |
|
| 170 | 1 | ->addTag('bazinga_geocoder.profiling_plugin'); |
|
| 171 | } |
||
| 172 | |||
| 173 | return array_map(function (string $id) { |
||
| 174 | 4 | return new Reference($id); |
|
| 175 | 31 | }, $plugins); |
|
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * {@inheritdoc} |
||
| 180 | */ |
||
| 181 | 33 | public function getConfiguration(array $config, ContainerBuilder $container) |
|
| 182 | { |
||
| 183 | 33 | return new Configuration($container->getParameter('kernel.debug')); |
|
| 184 | } |
||
| 185 | |||
| 186 | 31 | private function findReferences(array $options): array |
|
| 187 | { |
||
| 188 | 31 | foreach ($options as $key => $value) { |
|
| 189 | 23 | if (is_array($value)) { |
|
| 190 | 1 | $options[$key] = $this->findReferences($value); |
|
| 191 | 23 | } elseif ('_service' === substr((string) $key, -8) || 0 === strpos((string) $value, '@') || 'service' === $key) { |
|
| 192 | $options[$key] = new Reference(ltrim($value, '@')); |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | 31 | return $options; |
|
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param mixed $factoryClass |
||
| 201 | */ |
||
| 202 | 31 | private function implementsProviderFactory($factoryClass): bool |
|
| 203 | { |
||
| 204 | 31 | if (false === $interfaces = class_implements($factoryClass)) { |
|
| 205 | return false; |
||
| 206 | } |
||
| 207 | |||
| 208 | 31 | return in_array(ProviderFactoryInterface::class, $interfaces, true); |
|
| 209 | } |
||
| 210 | } |
||
| 211 |
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: