|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Twig; |
|
4
|
|
|
|
|
5
|
|
|
use PiedWeb\CMSBundle\Service\PageCanonicalService; |
|
6
|
|
|
use PiedWeb\RenderAttributes\AttributesTrait; |
|
7
|
|
|
use Twig\Extension\AbstractExtension; |
|
8
|
|
|
use Twig\TwigFilter; |
|
9
|
|
|
use Twig\TwigFunction; |
|
10
|
|
|
|
|
11
|
|
|
class AppExtension extends AbstractExtension |
|
12
|
|
|
{ |
|
13
|
|
|
use AttributesTrait; |
|
14
|
|
|
|
|
15
|
|
|
public function __construct(PageCanonicalService $pageCanonical) |
|
16
|
|
|
{ |
|
17
|
|
|
$this->pageCanonical = $pageCanonical; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function getFilters() |
|
21
|
|
|
{ |
|
22
|
|
|
return [ |
|
23
|
|
|
new TwigFilter('html_entity_decode', 'html_entity_decode'), |
|
24
|
|
|
]; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public function getFunctions() |
|
28
|
|
|
{ |
|
29
|
|
|
return [ |
|
30
|
|
|
new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']), |
|
31
|
|
|
new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']), |
|
32
|
|
|
new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], ['is_safe' => ['html']]), |
|
33
|
|
|
]; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public static function renderJavascriptLink($anchor, $path, $attr = []) |
|
37
|
|
|
{ |
|
38
|
|
|
if (0 === strpos($path, 'http://')) { |
|
39
|
|
|
$path = '-'.substr($path, 7); |
|
40
|
|
|
} elseif (0 === strpos($path, 'https://')) { |
|
41
|
|
|
$path = '_'.substr($path, 8); |
|
42
|
|
|
} elseif (0 === strpos($path, 'mailto:')) { |
|
43
|
|
|
$path = '@'.substr($path, 7); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return '<span'.self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]) |
|
47
|
|
|
.' rel=nofollow>'.$anchor.'</span>'; |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|