1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alpixel\Bundle\CMSBundle\Twig\Extension; |
4
|
|
|
|
5
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\Node; |
6
|
|
|
use Alpixel\Bundle\CMSBundle\Entity\TranslatableInterface; |
7
|
|
|
use Alpixel\Bundle\CMSBundle\Helper\BlockHelper; |
8
|
|
|
use Alpixel\Bundle\CMSBundle\Helper\CMSHelper; |
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
10
|
|
|
|
11
|
|
|
class TranslationExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface |
12
|
|
|
{ |
13
|
|
|
protected $blockHelper; |
14
|
|
|
protected $container; |
15
|
|
|
protected $cmsHelper; |
16
|
|
|
|
17
|
|
|
public function __construct(CMSHelper $cmsHelper, BlockHelper $blockHelper, $container) |
18
|
|
|
{ |
19
|
|
|
$this->cmsHelper = $cmsHelper; |
20
|
|
|
$this->container = $container; |
21
|
|
|
$this->blockHelper = $blockHelper; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function getName() |
25
|
|
|
{ |
26
|
|
|
return 'translation'; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function getGlobals() |
30
|
|
|
{ |
31
|
|
|
return [ |
32
|
|
|
'alpixel_cms_languages' => ($this->container->hasParameter('lunetics_locale.allowed_locales') ? $this->container->getParameter('lunetics_locale.allowed_locales') : null), |
33
|
|
|
]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function getFunctions() |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
|
|
new \Twig_SimpleFunction('alpixel_cms_get_translation', [$this, 'cmsHasTranslation']), |
40
|
|
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getFilters() |
44
|
|
|
{ |
45
|
|
|
return [ |
46
|
|
|
new \Twig_SimpleFilter('iso_to_country_name', [$this, 'isoToCountryName']), |
47
|
|
|
new \Twig_SimpleFilter('is_translatable', [$this, 'isTranslatable']), |
48
|
|
|
new \Twig_SimpleFilter('translation_source_property', [$this, 'translationSourceProperty']), |
49
|
|
|
]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function isoToCountryName($iso) |
53
|
|
|
{ |
54
|
|
|
return \Locale::getDisplayLanguage($iso, $this->container->getParameter('default_locale')); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function isTranslatable($object) |
58
|
|
|
{ |
59
|
|
|
return $object instanceof TranslatableInterface; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function cmsHasTranslation(TranslatableInterface $object, $locale) |
63
|
|
|
{ |
64
|
|
|
if ($object instanceof Node) { |
65
|
|
|
return $this->cmsHelper->nodeGetTranslation($object, $locale); |
66
|
|
|
} else { |
67
|
|
|
return $this->blockHelper->blockGetTranslation($object, $locale); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function translationSourceProperty($object, $property) |
72
|
|
|
{ |
73
|
|
View Code Duplication |
if (!is_object($object)) { |
|
|
|
|
74
|
|
|
throw new \InvalidArgumentException('The "$object" parameter must be an object.'); |
75
|
|
|
} elseif (empty($property) || !is_string($property)) { |
76
|
|
|
throw new \InvalidArgumentException('The "$property" parameter must be a non empty string.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if ($this->isTranslatable($object) && $object->getTranslationSource() !== null) { |
80
|
|
|
$object = $object->getTranslationSource(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
84
|
|
|
|
85
|
|
|
return $accessor->getValue($object, $property); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.