|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Zikula package. |
|
5
|
|
|
* |
|
6
|
|
|
* Copyright Zikula Foundation - http://zikula.org/ |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Zikula\ExtensionsModule\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Zikula\Common\Translator\TranslatorInterface; |
|
15
|
|
|
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface; |
|
16
|
|
|
|
|
17
|
|
|
class ModVarExtension extends \Twig_Extension |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var TranslatorInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $translator; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var VariableApiInterface |
|
26
|
|
|
*/ |
|
27
|
|
|
private $variableApi; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* ExtensionsExtension constructor. |
|
31
|
|
|
* @param TranslatorInterface $translator |
|
32
|
|
|
* @param VariableApiInterface $variableApi |
|
33
|
|
|
*/ |
|
34
|
|
|
public function __construct( |
|
35
|
|
|
TranslatorInterface $translator, |
|
36
|
|
|
VariableApiInterface $variableApi |
|
37
|
|
|
) { |
|
38
|
|
|
$this->translator = $translator; |
|
39
|
|
|
$this->variableApi = $variableApi; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Returns a list of functions to add to the existing list. |
|
44
|
|
|
* |
|
45
|
|
|
* @return array An array of functions |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getFunctions() |
|
48
|
|
|
{ |
|
49
|
|
|
return [ |
|
50
|
|
|
new \Twig_SimpleFunction('getModVar', [$this, 'getModVar']), |
|
51
|
|
|
new \Twig_SimpleFunction('getSystemVar', [$this, 'getSystemVar']), |
|
52
|
|
|
]; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param $module |
|
57
|
|
|
* @param $name |
|
58
|
|
|
* @param null $default |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
View Code Duplication |
public function getModVar($module, $name, $default = null) |
|
|
|
|
|
|
62
|
|
|
{ |
|
63
|
|
|
if (empty($module) || empty($name)) { |
|
64
|
|
|
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return $this->variableApi->get($module, $name, $default); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param $name |
|
72
|
|
|
* @param null $default |
|
73
|
|
|
* @return mixed |
|
74
|
|
|
*/ |
|
75
|
|
View Code Duplication |
public function getSystemVar($name, $default = null) |
|
|
|
|
|
|
76
|
|
|
{ |
|
77
|
|
|
if (empty($name)) { |
|
78
|
|
|
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $this->variableApi->getSystemVar($name, $default); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.