1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of BraincraftedBootstrapBundle. |
4
|
|
|
* (c) 2012-2013 by Florian Eckerstorfer |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
|
namespace Braincrafted\Bundle\BootstrapBundle\Twig; |
8
|
|
|
|
9
|
|
|
use Twig_Extension; |
10
|
|
|
use Twig_SimpleFilter; |
11
|
|
|
use Twig_SimpleFunction; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* BootstrapIconExtension |
15
|
|
|
* |
16
|
|
|
* @package BraincraftedBootstrapBundle |
17
|
|
|
* @subpackage Twig |
18
|
|
|
* @author Florian Eckerstorfer <[email protected]> |
19
|
|
|
* @copyright 2012-2013 Florian Eckerstorfer |
20
|
|
|
* @license http://opensource.org/licenses/MIT The MIT License |
21
|
|
|
* @link http://bootstrap.braincrafted.com Bootstrap for Symfony2 |
22
|
|
|
*/ |
23
|
|
|
class BootstrapIconExtension extends Twig_Extension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
private $iconPrefix; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $iconTag; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $iconPrefix |
37
|
|
|
* @param string $iconTag |
38
|
|
|
*/ |
39
|
|
|
public function __construct($iconPrefix, $iconTag = 'span') |
40
|
|
|
{ |
41
|
|
|
$this->iconPrefix = $iconPrefix; |
42
|
|
|
$this->iconTag = $iconTag; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function getFilters() |
49
|
|
|
{ |
50
|
|
|
return array( |
51
|
|
|
new Twig_SimpleFilter( |
52
|
|
|
'parse_icons', |
53
|
|
|
array($this, 'parseIconsFilter'), |
54
|
|
|
array('pre_escape' => 'html', 'is_safe' => array('html')) |
55
|
|
|
) |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* {@inheritDoc} |
61
|
|
|
*/ |
62
|
|
|
public function getFunctions() |
63
|
|
|
{ |
64
|
|
|
return array( |
65
|
|
|
new Twig_SimpleFunction( |
66
|
|
|
'icon', |
67
|
|
|
array($this, 'iconFunction'), |
68
|
|
|
array('pre_escape' => 'html', 'is_safe' => array('html')) |
69
|
|
|
) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Parses the given string and replaces all occurrences of .icon-[name] with the corresponding icon. |
75
|
|
|
* |
76
|
|
|
* @param string $text The text to parse |
77
|
|
|
* |
78
|
|
|
* @return string The HTML code with the icons |
79
|
|
|
*/ |
80
|
|
|
public function parseIconsFilter($text) |
81
|
|
|
{ |
82
|
|
|
$that = $this; |
83
|
|
|
|
84
|
|
|
return preg_replace_callback( |
85
|
|
|
'/\.([a-z]+)-([a-z0-9+-]+)/', |
86
|
|
|
function ($matches) use ($that) { |
87
|
|
|
return $that->iconFunction($matches[2], $matches[1]); |
88
|
|
|
}, |
89
|
|
|
$text |
90
|
|
|
); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Returns the HTML code for the given icon. |
95
|
|
|
* |
96
|
|
|
* @param string $icon The name of the icon |
97
|
|
|
* @param string $iconSet The icon-set name |
98
|
|
|
* |
99
|
|
|
* @return string The HTML code for the icon |
100
|
|
|
*/ |
101
|
|
|
public function iconFunction($icon, $iconSet = 'icon') |
102
|
|
|
{ |
103
|
|
|
if ($iconSet == 'icon') $iconSet = $this->iconPrefix; |
|
|
|
|
104
|
|
|
$icon = str_replace('+', ' '.$iconSet.'-', $icon); |
105
|
|
|
|
106
|
|
|
return sprintf('<%1$s class="%2$s %2$s-%3$s"></%1$s>', $this->iconTag, $iconSet, $icon); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* {@inheritDoc} |
111
|
|
|
*/ |
112
|
|
|
public function getName() |
113
|
|
|
{ |
114
|
|
|
return 'braincrafted_bootstrap_icon'; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|