|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkCoreBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
|
6
|
|
|
|
|
7
|
|
|
class FrameworkExtension extends \Twig_Extension |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var \Symfony\Component\DependencyInjection\ContainerInterface |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $container; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @param ContainerInterface $container |
|
16
|
|
|
*/ |
|
17
|
|
|
public function __construct(ContainerInterface $container) |
|
18
|
|
|
{ |
|
19
|
|
|
$this->container = $container; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Get the registered functions |
|
24
|
|
|
* |
|
25
|
|
|
* @return array |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getFunctions() |
|
28
|
|
|
{ |
|
29
|
|
|
return array( |
|
30
|
|
|
new \Twig_SimpleFunction( |
|
31
|
|
|
'bundleExists', |
|
32
|
|
|
array($this, 'bundleExists') |
|
33
|
|
|
), |
|
34
|
|
|
new \Twig_SimpleFunction( |
|
35
|
|
|
'toTranslation', |
|
36
|
|
|
array($this, 'convertToTranslation') |
|
37
|
|
|
), |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Check if a bundle exists |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $bundle |
|
45
|
|
|
* @return bool |
|
46
|
|
|
*/ |
|
47
|
|
|
public function bundleExists($bundle) |
|
48
|
|
|
{ |
|
49
|
|
|
$bundles = $this->container->getParameter('kernel.bundles'); |
|
50
|
|
|
|
|
51
|
|
|
return array_key_exists($bundle, $bundles); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Convert a given string into a string that will/can be used as a id for |
|
56
|
|
|
* translations |
|
57
|
|
|
* |
|
58
|
|
|
* @param string $stringToConvert |
|
59
|
|
|
* @return string |
|
60
|
|
|
*/ |
|
61
|
|
|
public function convertToTranslation($stringToConvert) |
|
62
|
|
|
{ |
|
63
|
|
|
$stringToConvert = trim($stringToConvert); |
|
64
|
|
|
$stringToConvert = str_replace( |
|
65
|
|
|
array('_', '-', ' ', 'framework', 'Framework'), |
|
66
|
|
|
'.', |
|
67
|
|
|
$stringToConvert |
|
68
|
|
|
); |
|
69
|
|
|
|
|
70
|
|
|
// the first item will mostly be the prefix of the namespace |
|
71
|
|
|
$stringToConvert = preg_replace('/(.*)\.(.*)bundle/U', '$1$2', $stringToConvert); |
|
72
|
|
|
$stringToConvert = str_replace('bundle', '', $stringToConvert); |
|
73
|
|
|
$stringToConvert = str_replace('Bundle', '', $stringToConvert); |
|
74
|
|
|
|
|
75
|
|
|
if (strtolower(mb_substr($stringToConvert, 0, 11)) == 'sumocoders.') { |
|
76
|
|
|
$stringToConvert = substr($stringToConvert, 11); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
// remove numbers if they appear at the end or as single items |
|
80
|
|
|
$stringToConvert = preg_replace('/\d+$/', '', $stringToConvert); |
|
81
|
|
|
$stringToConvert = preg_replace('/\.\d*\./', '.', $stringToConvert); |
|
82
|
|
|
|
|
83
|
|
|
$stringToConvert = preg_replace('/\.+/', '.', $stringToConvert); |
|
84
|
|
|
|
|
85
|
|
|
return trim($stringToConvert, '.'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|