1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSHttpCacheBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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 FOS\HttpCacheBundle\DependencyInjection\Compiler; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException; |
15
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Add tagged provider to the hash generator for user context. |
21
|
|
|
*/ |
22
|
|
|
class HashGeneratorPass implements CompilerPassInterface |
23
|
|
|
{ |
24
|
|
|
const TAG_NAME = 'fos_http_cache.user_context_provider'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* {@inheritdoc} |
28
|
|
|
*/ |
29
|
5 |
|
public function process(ContainerBuilder $container) |
30
|
|
|
{ |
31
|
5 |
|
if (!$container->has('fos_http_cache.user_context.hash_generator')) { |
32
|
1 |
|
return; |
33
|
|
|
} |
34
|
|
|
|
35
|
4 |
|
$definition = $container->getDefinition('fos_http_cache.user_context.hash_generator'); |
36
|
|
|
|
37
|
4 |
|
$prioritisedTags = []; |
38
|
4 |
|
$taggedProviders = $container->findTaggedServiceIds(self::TAG_NAME); |
39
|
|
|
|
40
|
4 |
|
if (!count($taggedProviders)) { |
41
|
1 |
|
throw new InvalidConfigurationException('No user context providers found. Either tag providers or disable fos_http_cache.user_context'); |
42
|
|
|
} |
43
|
|
|
|
44
|
3 |
|
foreach ($taggedProviders as $id => $tags) { |
45
|
3 |
|
foreach ($tags as $tag) { |
46
|
3 |
|
$priority = isset($tag['priority']) ? (int) $tag['priority'] : 0; |
47
|
3 |
|
$prioritisedTags[$priority][] = $id; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
krsort($prioritisedTags, SORT_NUMERIC); |
52
|
3 |
|
$prioritisedProviders = call_user_func_array('array_merge', $prioritisedTags); |
53
|
|
|
|
54
|
3 |
|
$providers = []; |
55
|
3 |
|
foreach ($prioritisedProviders as $id) { |
56
|
3 |
|
$providers[] = new Reference($id); |
57
|
|
|
} |
58
|
|
|
|
59
|
3 |
|
$definition->addArgument($providers); |
60
|
3 |
|
} |
61
|
|
|
} |
62
|
|
|
|