|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the login-cidadao project or it's bundles. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Guilherme Donato <guilhermednt on github> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LoginCidadao\CoreBundle\Twig\Extension; |
|
12
|
|
|
|
|
13
|
|
|
use LoginCidadao\CoreBundle\Event\LoginCidadaoCoreEvents; |
|
14
|
|
|
use LoginCidadao\CoreBundle\Event\TranslateScopeEvent; |
|
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
16
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
17
|
|
|
|
|
18
|
|
|
class ScopesExtension extends \Twig_Extension |
|
19
|
|
|
{ |
|
20
|
|
|
/** @var EventDispatcherInterface */ |
|
21
|
|
|
private $dispatcher; |
|
22
|
|
|
|
|
23
|
|
|
/** @var TranslatorInterface */ |
|
24
|
|
|
private $translator; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* ScopesExtension constructor. |
|
28
|
|
|
* @param EventDispatcherInterface $dispatcher |
|
29
|
|
|
* @param TranslatorInterface $translator |
|
30
|
|
|
*/ |
|
31
|
|
|
public function __construct(EventDispatcherInterface $dispatcher, TranslatorInterface $translator) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->dispatcher = $dispatcher; |
|
34
|
|
|
$this->translator = $translator; |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
public function getFilters() |
|
38
|
|
|
{ |
|
39
|
|
|
return [ |
|
40
|
|
|
new \Twig_SimpleFilter('scope', [$this, 'scopeFilter']), |
|
41
|
|
|
new \Twig_SimpleFilter('scope_has_description', [$this, 'scopeHasDescription']), |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function scopeFilter($scopes, $allowBlank = false) |
|
46
|
|
|
{ |
|
47
|
|
|
$translator = $this->translateScope(); |
|
48
|
|
|
if (is_array($scopes)) { |
|
49
|
|
|
$result = []; |
|
50
|
|
|
foreach ($scopes as $scope) { |
|
51
|
|
|
$result[$scope] = $translator($scope); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $allowBlank ? $result : array_filter($result); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return $translator($scopes); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function scopeHasDescription($scope) |
|
61
|
|
|
{ |
|
62
|
|
|
$id = "scope_details.{$scope}"; |
|
63
|
|
|
$translation = $this->translator->trans($id); |
|
64
|
|
|
|
|
65
|
|
|
return $translation !== $id; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
private function translateScope() |
|
69
|
|
|
{ |
|
70
|
|
|
return function ($scope) { |
|
71
|
|
|
$event = new TranslateScopeEvent($scope); |
|
72
|
|
|
$this->dispatcher->dispatch(LoginCidadaoCoreEvents::TRANSLATE_SCOPE, $event); |
|
73
|
|
|
if ($event->isTranslated()) { |
|
74
|
|
|
return $event->getTranslation(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$id = "scope.{$scope}"; |
|
78
|
|
|
$translation = $this->translator->trans($id); |
|
79
|
|
|
if ($translation !== $id) { |
|
80
|
|
|
return $translation; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
return $scope; |
|
84
|
|
|
}; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|