|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Swap Bundle. |
| 5 | + * |
| 6 | + * (c) Florian Voutzinos <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Florianv\SwapBundle\DependencyInjection; |
| 13 | + |
| 14 | +use Symfony\Component\Config\FileLocator; |
| 15 | +use Symfony\Component\DependencyInjection\ContainerBuilder; |
| 16 | +use Symfony\Component\DependencyInjection\Definition; |
| 17 | +use Symfony\Component\DependencyInjection\Extension\Extension; |
| 18 | +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; |
| 19 | +use Symfony\Component\DependencyInjection\Reference; |
| 20 | + |
| 21 | +/** |
| 22 | + * The container extension. |
| 23 | + * |
| 24 | + * @author Florian Voutzinos <[email protected]> |
| 25 | + */ |
| 26 | +class FlorianvSwapExtension extends Extension |
| 27 | +{ |
| 28 | + /** |
| 29 | + * {@inheritdoc} |
| 30 | + */ |
| 31 | + public function load(array $config, ContainerBuilder $container) |
| 32 | + { |
| 33 | + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
| 34 | + $loader->load('services.xml'); |
| 35 | + |
| 36 | + $config = $this->processConfiguration(new Configuration(), $config); |
| 37 | + |
| 38 | + if (isset($config['providers'])) { |
| 39 | + foreach ($config['providers'] as $providerName => $providerConfig) { |
| 40 | + switch ($providerName) { |
| 41 | + case 'yahoo_finance': |
| 42 | + case 'google_finance': |
| 43 | + case 'european_central_bank': |
| 44 | + $this->addProvider($container, $providerName, array( |
| 45 | + new Reference('florianv_swap.client') |
| 46 | + )); |
| 47 | + break; |
| 48 | + |
| 49 | + case 'open_exchange_rates': |
| 50 | + $this->addProvider($container, $providerName, array( |
| 51 | + new Reference('florianv_swap.client'), |
| 52 | + $providerConfig['app_id'], |
| 53 | + $providerConfig['enterprise'] |
| 54 | + )); |
| 55 | + break; |
| 56 | + |
| 57 | + case 'xignite': |
| 58 | + $this->addProvider($container, $providerName, array( |
| 59 | + new Reference('florianv_swap.client'), |
| 60 | + $providerConfig['token'], |
| 61 | + )); |
| 62 | + break; |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Creates the provider definition and add it to the container. |
| 70 | + * |
| 71 | + * @param ContainerBuilder $container |
| 72 | + * @param string $name |
| 73 | + * @param array $arguments |
| 74 | + */ |
| 75 | + private function addProvider(ContainerBuilder $container, $name, $arguments = array()) |
| 76 | + { |
| 77 | + $definition = new Definition('%florianv_swap.provider.'.$name.'.class%', $arguments); |
| 78 | + $definition->setPublic(false); |
| 79 | + $definition->addTag('florianv_swap.provider'); |
| 80 | + |
| 81 | + $container->setDefinition(sprintf('florianv_swap.provider.%s', $name), $definition); |
| 82 | + } |
| 83 | +} |
0 commit comments