Completed
Push — master ( 09f032...464477 )
by WEBEWEB
01:27
created

RendererTwigExtension   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 64
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getFilters() 0 3 1
A renderIcon() 0 27 5
1
<?php
2
3
/*
4
 * This file is part of the bootstrap-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
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 WBW\Bundle\BootstrapBundle\Twig\Extension;
13
14
use Twig_Environment;
15
use WBW\Bundle\BootstrapBundle\Twig\Extension\Component\GlyphiconTwigExtension;
16
use WBW\Bundle\CoreBundle\Twig\Extension\RendererTwigExtension as BaseTwigExtension;
17
18
/**
19
 * Renderer Twig extension.
20
 *
21
 * @author webeweb <https://github.com/webeweb/>
22
 * @package WBW\Bundle\BootstrapBundle\Twig\Extension
23
 */
24
class RendererTwigExtension extends BaseTwigExtension {
25
26
    /**
27
     * Service name.
28
     *
29
     * @var string
30
     */
31
    const SERVICE_NAME = "webeweb.bootstrap.twig.extension.renderer";
32
33
    /**
34
     * Constructor.
35
     *
36
     * @param Twig_Environment $twigEnvironment The twig environment.
37
     */
38
    public function __construct(Twig_Environment $twigEnvironment) {
39
        parent::__construct($twigEnvironment);
40
    }
41
42
    /**
43
     * Get the Twig filters.
44
     *
45
     * @return Twig_SimpleFilter[] Returns the Twig filters.
46
     */
47
    public function getFilters() {
48
        return [];
49
    }
50
51
    /**
52
     * Render an icon.
53
     *
54
     * @param Twig_Environment $twigEnvironment The twig environment.
55
     * @param string $name The icon name.
56
     * @param string $style The icon style.
57
     * @return string Returns a rendered icon.
58
     */
59
    public static function renderIcon(Twig_Environment $twigEnvironment, $name, $style = null) {
60
61
        // Determines the handler.
62
        $handler = explode(":", $name);
63
        if (1 === count($handler)) {
64
            array_unshift($handler, "g");
65
        }
66
        if (2 !== count($handler)) {
67
            return "";
68
        }
69
70
        // Swith into handler.
71
        switch ($handler[0]) {
72
73
            case "b": // Bootstrap
74
            case "g": // Glyphicon
75
                $output = (new GlyphiconTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
76
                break;
77
78
            default:
79
                $output = parent::renderIcon($twigEnvironment, $name, $style);
80
                break;
81
        }
82
83
        // Return the output.
84
        return $output;
85
    }
86
87
}
88