Completed
Push — master ( 774f40...be3045 )
by WEBEWEB
01:28
created

RendererTwigExtension   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 0
cbo 2
dl 0
loc 55
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 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
     * Render an icon.
44
     *
45
     * @param Twig_Environment $twigEnvironment The twig environment.
46
     * @param string $name The icon name.
47
     * @param string $style The icon style.
48
     * @return string Returns a rendered icon.
49
     */
50
    public static function renderIcon(Twig_Environment $twigEnvironment, $name, $style = null) {
51
52
        // Determines the handler.
53
        $handler = explode(":", $name);
54
        if (1 === count($handler)) {
55
            array_unshift($handler, "g");
56
        }
57
        if (2 !== count($handler)) {
58
            return "";
59
        }
60
61
        // Swith into handler.
62
        switch ($handler[0]) {
63
64
            case "b": // Bootstrap
65
            case "g": // Glyphicon
66
                $output = (new GlyphiconTwigExtension($twigEnvironment))->renderIcon($handler[1], $style);
67
                break;
68
69
            default:
70
                $output = parent::renderIcon($twigEnvironment, $name, $style);
71
                break;
72
        }
73
74
        // Return the output.
75
        return $output;
76
    }
77
78
}
79