Completed
Push — master ( b0bffb...a5ac31 )
by Dev
11:29
created

AppExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use Cocur\Slugify\Slugify;
6
use PiedWeb\CMSBundle\Service\PageCanonicalService;
7
use PiedWeb\RenderAttributes\AttributesTrait;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFilter;
10
use Twig\TwigFunction;
11
use Twig_Environment;
12
13
class AppExtension extends AbstractExtension
14
{
15
    use AttributesTrait;
16
17
    public function __construct(PageCanonicalService $pageCanonical)
18
    {
19
        $this->pageCanonical = $pageCanonical;
20
    }
21
22
    public function getFilters()
23
    {
24
        return [
25
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
26
            new TwigFilter(
27
                'punctuation_beautifer',
28
                [AppExtension::class, 'punctuationBeautifer'],
29
                ['is_safe' => ['html']]
30
            ),
31
        ];
32
    }
33
34
    public function getFunctions()
35
    {
36
        return [
37
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
38
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
39
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [
40
                'is_safe' => ['html'],
41
                'needs_environment' => true,
42
            ]),
43
            new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [
44
                'is_safe' => ['html'],
45
                'needs_environment' => true,
46
            ]),
47
            new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [
48
                'is_safe' => ['html'],
49
                'needs_environment' => true,
50
            ]),
51
            new TwigFunction(
52
                'bookmark', // = anchor
53
                [AppExtension::class, 'renderTxtAnchor'],
54
                ['is_safe' => ['html'], 'needs_environment' => true]
55
            ),
56
            new TwigFunction(
57
                'anchor', // = bookmark
58
                [AppExtension::class, 'renderTxtAnchor'],
59
                ['is_safe' => ['html'], 'needs_environment' => true]
60
            ),
61
            new TwigFunction(
62
                'mail', // = bookmark
63
                [AppExtension::class, 'renderMail'],
64
                ['is_safe' => ['html'], 'needs_environment' => true]
65
            ),
66
        ];
67
    }
68
69
    public static function renderTxtAnchor(Twig_Environment $env, $name)
70
    {
71
        $slugify = new Slugify();
72
        $name = $slugify->slugify($name);
73
74
        return $env->render('@PiedWebCMS/component/_txt_anchor.html.twig', ['name' => $name]);
75
    }
76
77
    public static function renderMail(Twig_Environment $env, $mail, $class = '')
78
    {
79
        $mail = str_rot13($mail);
80
81
        return $env->render('@PiedWebCMS/component/_mail.html.twig', ['mail' => $mail, 'class' => $class]);
82
    }
83
84
    public static function renderEncodedMail(Twig_Environment $env, $mail)
85
    {
86
        return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]);
87
    }
88
89
    public static function punctuationBeautifer($text)
90
    {
91
        return str_replace(
92
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
93
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
94
            $text
95
        );
96
    }
97
98
    public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = [])
99
    {
100
        if (0 === strpos($path, 'http://')) {
101
            $path = '-'.substr($path, 7);
102
        } elseif (0 === strpos($path, 'https://')) {
103
            $path = '_'.substr($path, 8);
104
        } elseif (0 === strpos($path, 'mailto:')) {
105
            $path = '@'.substr($path, 7);
106
        }
107
108
        return $env->render('@PiedWebCMS/component/_javascript_link.html.twig', [
109
            'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]),
110
            'anchor' => $anchor,
111
        ]);
112
    }
113
}
114