Completed
Push — ezp29034_cache_warmup ( 1779c6 )
by
unknown
22:21
created

ConsoleCacheWarmupPass::process()   B

Complexity

Conditions 7
Paths 9

Size

Total Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
nc 9
nop 1
dl 0
loc 27
rs 8.5546
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the ConsoleCacheWarmupPass class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Bundle\EzPublishCoreBundle\DependencyInjection\Compiler;
10
11
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
12
use Symfony\Component\DependencyInjection\ContainerBuilder;
13
use Symfony\Component\DependencyInjection\Reference;
14
15
/**
16
 * Cache related compiler pass.
17
 *
18
 * Ensures class cache warmup is disabled in console mode.
19
 */
20
class ConsoleCacheWarmupPass implements CompilerPassInterface
21
{
22
    public function process(ContainerBuilder $container)
23
    {
24
        if (PHP_SAPI !== 'cli' ||
25
            !$container->hasDefinition('kernel.class_cache.cache_warmer')) {
26
            return;
27
        }
28
29
        $warmers = array();
30
        foreach ($container->findTaggedServiceIds('kernel.cache_warmer') as $id => $attributes) {
31
            if ($id === 'kernel.class_cache.cache_warmer') {
32
                continue;
33
            }
34
35
            $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0;
36
            $warmers[$priority][] = new Reference($id);
37
        }
38
39
        if (empty($warmers)) {
40
            return;
41
        }
42
43
        // sort by priority and flatten
44
        krsort($warmers);
45
        $warmers = \call_user_func_array('array_merge', $warmers);
46
47
        $container->getDefinition('cache_warmer')->replaceArgument(0, $warmers);
48
    }
49
}
50