1717use Symfony \Component \DependencyInjection \Extension \Extension ;
1818use Symfony \Component \DependencyInjection \Loader \XmlFileLoader ;
1919use Symfony \Component \DependencyInjection \Reference ;
20+ use Symfony \Bridge \Doctrine \DependencyInjection \AbstractDoctrineExtension ;
2021
2122/**
2223 * The container extension.
@@ -35,49 +36,73 @@ public function load(array $config, ContainerBuilder $container)
3536
3637 $ config = $ this ->processConfiguration (new Configuration (), $ config );
3738
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- case 'webservicex ' :
45- $ this ->addProvider ($ container , $ providerName , array (
46- new Reference ('florianv_swap.client ' )
47- ));
48- break ;
49-
50- case 'open_exchange_rates ' :
51- $ this ->addProvider ($ container , $ providerName , array (
52- new Reference ('florianv_swap.client ' ),
53- $ providerConfig ['app_id ' ],
54- $ providerConfig ['enterprise ' ]
55- ));
56- break ;
57-
58- case 'xignite ' :
59- $ this ->addProvider ($ container , $ providerName , array (
60- new Reference ('florianv_swap.client ' ),
61- $ providerConfig ['token ' ],
62- ));
63- break ;
64- }
39+ $ container ->setAlias ('florianv_swap.http_adapter ' , $ config ['http_adapter ' ]);
40+
41+ $ this ->loadProviders ($ config ['providers ' ], $ container );
42+
43+ if (isset ($ config ['cache ' ])) {
44+ $ this ->loadCache ($ config ['cache ' ], $ container );
45+ }
46+ }
47+
48+ private function loadProviders (array $ config , ContainerBuilder $ container )
49+ {
50+ foreach ($ config as $ providerName => $ providerConfig ) {
51+ switch ($ providerName ) {
52+ case 'yahoo_finance ' :
53+ case 'google_finance ' :
54+ case 'european_central_bank ' :
55+ case 'national_bank_of_romania ' :
56+ case 'webservicex ' :
57+ $ this ->addProvider ($ container , $ providerName , array (
58+ new Reference ('florianv_swap.http_adapter ' ),
59+ ), $ providerConfig ['priority ' ]);
60+ break ;
61+
62+ case 'open_exchange_rates ' :
63+ $ this ->addProvider ($ container , $ providerName , array (
64+ new Reference ('florianv_swap.http_adapter ' ),
65+ $ providerConfig ['app_id ' ],
66+ $ providerConfig ['enterprise ' ]
67+ ), $ providerConfig ['priority ' ]);
68+ break ;
69+
70+ case 'xignite ' :
71+ $ this ->addProvider ($ container , $ providerName , array (
72+ new Reference ('florianv_swap.http_adapter ' ),
73+ $ providerConfig ['token ' ],
74+ ), $ providerConfig ['priority ' ]);
75+ break ;
6576 }
6677 }
6778 }
6879
80+ private function loadCache (array $ config , ContainerBuilder $ container )
81+ {
82+ $ cacheProvider = new Definition ('%florianv_swap.cache.doctrine. ' .$ config ['doctrine ' ]['type ' ].'.class% ' );
83+ $ cacheProvider ->setPublic (false );
84+
85+ $ cacheDefinition = new Definition ('%florianv_swap.cache.doctrine.class% ' , array (
86+ $ cacheProvider ,
87+ $ config ['ttl ' ]
88+ ));
89+ $ cacheDefinition ->setPublic (false );
90+
91+ $ container ->getDefinition ('florianv_swap.swap ' )->replaceArgument (1 , $ cacheDefinition );
92+ }
93+
6994 /**
7095 * Creates the provider definition and add it to the container.
7196 *
7297 * @param ContainerBuilder $container
7398 * @param string $name
7499 * @param array $arguments
75100 */
76- private function addProvider (ContainerBuilder $ container , $ name , array $ arguments = array ())
101+ private function addProvider (ContainerBuilder $ container , $ name , array $ arguments = array (), $ priority = null )
77102 {
78103 $ definition = new Definition ('%florianv_swap.provider. ' .$ name .'.class% ' , $ arguments );
79104 $ definition ->setPublic (false );
80- $ definition ->addTag ('florianv_swap.provider ' );
105+ $ definition ->addTag ('florianv_swap.provider ' , array ( ' priority ' => $ priority ) );
81106
82107 $ container ->setDefinition (sprintf ('florianv_swap.provider.%s ' , $ name ), $ definition );
83108 }
0 commit comments