Skip to content

Commit 55be406

Browse files
committed
Merge pull request #5 from florianv/issue-4
Allow doctrine cache services as types
2 parents cf25b10 + d8f2630 commit 55be406

File tree

5 files changed

+78
-34
lines changed

5 files changed

+78
-34
lines changed

DependencyInjection/Configuration.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ private function getCacheDriverNode($name)
106106
->end()
107107
->isRequired()
108108
->children()
109-
->enumNode('type')
110-
->values(array('apc', 'array', 'xcache', 'wincache', 'zenddata'))
109+
->scalarNode('type')
110+
->info('A cache type or service id')
111111
->defaultValue('array')
112112
->end()
113113
->end()

DependencyInjection/FlorianvSwapExtension.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,12 @@ private function loadProviders(array $config, ContainerBuilder $container)
7979

8080
private function loadCache(array $config, ContainerBuilder $container)
8181
{
82-
$cacheProvider = new Definition('%florianv_swap.cache.doctrine.'.$config['doctrine']['type'].'.class%');
83-
$cacheProvider->setPublic(false);
82+
if (in_array($config['doctrine']['type'], ['apc', 'array', 'xcache', 'wincache', 'zenddata'], true)) {
83+
$cacheProvider = new Definition('%florianv_swap.cache.doctrine.'.$config['doctrine']['type'].'.class%');
84+
$cacheProvider->setPublic(false);
85+
} else {
86+
$cacheProvider = new Reference($config['doctrine']['type']);
87+
}
8488

8589
$cacheDefinition = new Definition('%florianv_swap.cache.doctrine.class%', array(
8690
$cacheProvider,

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
[![Version][version-image]][version-url]
55
[![Downloads][downloads-image]][downloads-url]
66

7-
> Integrates the [Swap](https://github.com/florianv/swap) library with Symfony.
7+
> Integrates the [Swap](https://github.com/florianv/swap) library with Symfony
88
99
## Documentation
1010

Resources/doc/index.md

Lines changed: 42 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,32 @@ In order to add your custom providers implementing `Swap\ProviderInterface`, you
126126
</service>
127127
```
128128

129+
## Providers priority
130+
131+
A priority attribute is available in tags and `config.yml` to sort providers.
132+
133+
```yaml
134+
# app/config/config.yml
135+
florianv_swap:
136+
providers:
137+
yahoo_finance:
138+
priority: 10
139+
google_finance:
140+
priority: 0
141+
```
142+
143+
```xml
144+
<service id="acme_demo.provider.custom" class="Acme\DemoBundle\Provider\Custom">
145+
<tag name="florianv_swap.provider" priority="5" />
146+
</service>
147+
```
148+
149+
In this case, the order of providers will be:
150+
151+
- yahoo_finance
152+
- acme_demo.provider.custom
153+
- google_finance
154+
129155
## Custom HTTP adapter
130156

131157
You can define your own HTTP adapter. It has to inherit from `Ivory\HttpAdapter\HttpAdapterInterface`.
@@ -138,49 +164,38 @@ florianv_swap:
138164

139165
## Cache
140166

141-
Some doctrine cache providers are implemented:
167+
Currently only [Doctrine Cache](https://github.com/doctrine/cache) is supported as cache implementation.
168+
169+
### Lifetime
142170

143-
- apc
144-
- array
145-
- xcache
146-
- wincache
147-
- zenddata
171+
You must specify a lifetime for your cache entries:
148172

149173
```yaml
150174
# app/config/config.yml
151175
florianv_swap:
152176
cache:
153-
doctrine: apc
154-
providers:
155-
yahoo_finance: ~
156-
google_finance: ~
177+
ttl: 3600 # seconds
157178
```
158179

159-
## Priority
180+
### Provider
160181

161-
A priority attribute is available in tags and config.yml to sort providers.
182+
You can use a doctrine service id:
162183

163184
```yaml
164185
# app/config/config.yml
165186
florianv_swap:
166-
providers:
167-
yahoo_finance:
168-
priority: 10
169-
google_finance:
170-
priority: 0
171-
```
172-
173-
```xml
174-
<service id="acme_demo.provider.custom" class="Acme\DemoBundle\Provider\Custom">
175-
<tag name="florianv_swap.provider" priority="5" />
176-
</service>
187+
cache:
188+
doctrine: my_apc_service
177189
```
178190

179-
In this case, the order of providers will be:
191+
or one of the implemented providers (`apc`, `array`, `xcache`, `wincache`, `zenddata`)
180192

181-
- yahoo_finance
182-
- acme_demo.provider.custom
183-
- google_finance
193+
```yaml
194+
# app/config/config.yml
195+
florianv_swap:
196+
cache:
197+
doctrine: apc
198+
```
184199

185200
# Usage
186201

Tests/DependencyInjection/FlorianvSwapExtensionTest.php

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ protected function setUp()
3535
}
3636

3737
/**
38-
* @expectedException Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
38+
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidConfigurationException
3939
*/
4040
public function testNoProvider()
4141
{
@@ -187,7 +187,7 @@ public function testCacheMissTtl()
187187
$this->extension->load($config, $this->container);
188188
}
189189

190-
public function testCache()
190+
public function testDoctrineCacheProvider()
191191
{
192192
$config = array(
193193
'florianv_swap' => array(
@@ -212,6 +212,31 @@ public function testCache()
212212
$this->assertEquals(array($apcDefinition, 3600), $cache->getArguments());
213213
}
214214

215+
public function testDoctrineCacheService()
216+
{
217+
$config = array(
218+
'florianv_swap' => array(
219+
'cache' => array(
220+
'ttl' => 60,
221+
'doctrine' => 'my_service',
222+
),
223+
'providers' => array('yahoo_finance' => null)
224+
)
225+
);
226+
$this->extension->load($config, $this->container);
227+
228+
$swap = $this->container->getDefinition('florianv_swap.swap');
229+
$arguments = $swap->getArguments();
230+
$cache = $arguments[1];
231+
$cacheArguments = $cache->getArguments();
232+
233+
$this->assertEquals($cache->getClass(), '%florianv_swap.cache.doctrine.class%');
234+
$this->assertFalse($cache->isPublic());
235+
236+
$this->assertEquals(new Reference('my_service'), $cacheArguments[0]);
237+
$this->assertEquals(60, $cacheArguments[1]);
238+
}
239+
215240
private function createProvidersConfig(array $providers)
216241
{
217242
return array('florianv_swap' => array('providers' => $providers));

0 commit comments

Comments
 (0)