DefaultTwigExtensions   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 2
dl 0
loc 48
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A onAdd() 0 12 4
A onRender() 0 3 1
1
<?php
2
3
namespace Jasny\View\Plugin;
4
5
use Jasny\ViewInterface;
6
use Jasny\View\PluginInterface;
7
use Jasny\View\Twig as TwigView;
8
9
/**
10
 * Add the official Twig extension and Jansy Twig extensions.
11
 * 
12
 * @link https://github.com/twigphp/Twig-extensions
13
 * @link https://github.com/jasny/twig-extensions
14
 */
15
class DefaultTwigExtensions implements PluginInterface
16
{
17
    /**
18
     * Extension class names
19
     * 
20
     * @var array
21
     */
22
    public $extensions = [
23
        'Twig_Extensions_Extension_Array',
24
        'Twig_Extensions_Extension_Date',
25
        'Twig_Extensions_Extension_I18n',
26
        'Twig_Extensions_Extension_Intl',
27
        'Twig_Extensions_Extension_Text',
28
        'Jasny\Twig\DateExtension',
29
        'Jasny\Twig\PcreExtension',
30
        'Jasny\Twig\TextExtension',
31
        'Jasny\Twig\ArrayExtension'
32
    ];
33
34
    /**
35
     * Called when the plugin is added to the view.
36
     * 
37
     * @param ViewInterface $view
38
     */
39 2
    public function onAdd(ViewInterface $view)
40
    {
41 2
        if (!$view instanceof TwigView) {
42 1
            throw new \InvalidArgumentException("This plugin only works with a Twig view");
43
        }
44
        
45 1
        foreach ($this->extensions as $class) {
46 1
            if (class_exists($class)) {
47 1
                $view->getTwig()->addExtension(new $class());
48 1
            }
49 1
        }
50 1
    }
51
    
52
    /**
53
     * Called when view renders a template.
54
     * 
55
     * @param ViewInterface $view
56
     * @param string        $template   Template filename
57
     * @param array         $context
58
     */
59 1
    public function onRender(ViewInterface $view, $template, array $context)
60
    {
61 1
    }
62
}
63