Completed
Pull Request — 2.x (#212)
by
unknown
16:18
created

TwigTemplateRecorderInjector::initRuntime()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 6
nc 3
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[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 Sonata\CacheBundle\Cache;
13
14
use Sonata\Cache\Invalidation\Recorder;
15
use Twig_Environment;
16
use Twig_Extension;
17
use Twig_Extension_InitRuntimeInterface;
18
19
/**
20
 * @internal
21
 *
22
 * NEXT_MAJOR: remove
23
 */
24
final class TwigTemplateRecorderInjector extends Twig_Extension implements Twig_Extension_InitRuntimeInterface
25
{
26
    /**
27
     * @var Recorder
28
     */
29
    private $recorder;
30
31
    public function __construct(Recorder $recorder)
32
    {
33
        $this->recorder = $recorder;
34
    }
35
36
    /**
37
     * Initializes the runtime environment.
38
     *
39
     * This is where you can load some file that contains filter functions for instance.
40
     *
41
     * @param Twig_Environment $environment The current Twig_Environment instance
42
     */
43
    public function initRuntime(Twig_Environment $environment)
44
    {
45
        $baseTemplateClass = $environment->getBaseTemplateClass();
46
47
        if (empty($baseTemplateClass)) {
48
            return;
49
        }
50
51
        if (method_exists($baseTemplateClass, 'attachRecorder')) {
52
            call_user_func([$baseTemplateClass, 'attachRecorder'], $this->recorder);
53
        }
54
    }
55
}
56