Completed
Push — new-user-bundle ( a512ca...38b344 )
by Wouter
62:04
created

FrameworkExtension::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
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 22
    public function __construct(ContainerInterface $container)
18
    {
19 22
        $this->container = $container;
20 22
    }
21
22
    /**
23
     * Get the registered functions
24
     *
25
     * @return array
26
     */
27 2
    public function getFunctions()
28
    {
29
        return array(
30 2
            new \Twig_SimpleFunction(
31 2
                'bundleExists',
32 2
                array($this, 'bundleExists')
33
            ),
34 2
            new \Twig_SimpleFunction(
35 2
                'toTranslation',
36 2
                array($this, 'convertToTranslation')
37
            ),
38
        );
39
    }
40
41
    /**
42
     * Check if a bundle exists
43
     *
44
     * @param string $bundle
45
     * @return bool
46
     */
47 10
    public function bundleExists($bundle)
48
    {
49 10
        $bundles = $this->container->getParameter('kernel.bundles');
50
51 10
        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 19
    public function convertToTranslation($stringToConvert)
62
    {
63 19
        $stringToConvert = trim($stringToConvert);
64 19
        $stringToConvert = str_replace(
65 19
            array('_', '-', ' ', 'framework', 'Framework'),
66 19
            '.',
67
            $stringToConvert
68
        );
69
70
        // the first item will mostly be the prefix of the namespace
71 19
        $stringToConvert = preg_replace('/(.*)\.(.*)bundle/U', '$1$2', $stringToConvert);
72 19
        $stringToConvert = str_replace('bundle', '', $stringToConvert);
73 19
        $stringToConvert = str_replace('Bundle', '', $stringToConvert);
74
75 19
        if (strtolower(mb_substr($stringToConvert, 0, 11)) == 'sumocoders.') {
76 2
            $stringToConvert = substr($stringToConvert, 11);
77
        }
78
79
        // remove numbers if they appear at the end or as single items
80 19
        $stringToConvert = preg_replace('/\d+$/', '', $stringToConvert);
81 19
        $stringToConvert = preg_replace('/\.\d*\./', '.', $stringToConvert);
82
83 19
        $stringToConvert = preg_replace('/\.+/', '.', $stringToConvert);
84
85 19
        return trim($stringToConvert, '.');
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 10
    public function getName()
92
    {
93 10
        return 'framework_extension';
94
    }
95
}
96