|
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\ThemeModule\Twig\Extension; |
|
13
|
|
|
|
|
14
|
|
|
use Psr\Log\LoggerInterface; |
|
15
|
|
|
use Zikula\Common\Translator\TranslatorInterface; |
|
16
|
|
|
use Zikula\ThemeModule\Engine\ParameterBag; |
|
17
|
|
|
|
|
18
|
|
|
class PageVarExtension extends \Twig_Extension |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var TranslatorInterface |
|
22
|
|
|
*/ |
|
23
|
|
|
private $translator; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var ParameterBag |
|
27
|
|
|
*/ |
|
28
|
|
|
private $pageVars; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var LoggerInterface |
|
32
|
|
|
* @deprecated - remove at Core-2.0 |
|
33
|
|
|
*/ |
|
34
|
|
|
private $logger; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var AssetExtension |
|
38
|
|
|
* @deprecated - remove at Core-2.0 |
|
39
|
|
|
*/ |
|
40
|
|
|
private $assetExtension; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* PageVarExtension constructor. |
|
44
|
|
|
* @param TranslatorInterface $translator |
|
45
|
|
|
* @param ParameterBag $pageVars |
|
46
|
|
|
* @param LoggerInterface $logger |
|
47
|
|
|
* @param AssetExtension $assetExtension |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct( |
|
50
|
|
|
TranslatorInterface $translator, |
|
51
|
|
|
ParameterBag $pageVars, |
|
52
|
|
|
LoggerInterface $logger, |
|
53
|
|
|
AssetExtension $assetExtension |
|
54
|
|
|
) { |
|
55
|
|
|
$this->translator = $translator; |
|
56
|
|
|
$this->pageVars = $pageVars; |
|
57
|
|
|
$this->logger = $logger; |
|
|
|
|
|
|
58
|
|
|
$this->assetExtension = $assetExtension; |
|
|
|
|
|
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Returns a list of functions to add to the existing list. |
|
63
|
|
|
* |
|
64
|
|
|
* @return array An array of functions |
|
65
|
|
|
*/ |
|
66
|
|
|
public function getFunctions() |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
69
|
|
|
new \Twig_SimpleFunction('pageAddVar', [$this, 'pageAddVar']), // @deprecated |
|
70
|
|
|
new \Twig_SimpleFunction('pageSetVar', [$this, 'pageSetVar']), |
|
71
|
|
|
new \Twig_SimpleFunction('pageGetVar', [$this, 'pageGetVar']), |
|
72
|
|
|
]; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @deprecated at Core 1.4.1, remove at Core-2.0 |
|
77
|
|
|
* @see use pageSetVar() or pageAddAsset() |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @param string $value |
|
80
|
|
|
*/ |
|
81
|
|
|
public function pageAddVar($name, $value) |
|
82
|
|
|
{ |
|
83
|
|
|
$this->logger->log(\Monolog\Logger::DEBUG, '\Zikula\Bundle\CoreBundle\Twig\Extension\CoreExtension::pageAddVar is deprecated use pageAddAsset() or pageSetVar().'); |
|
|
|
|
|
|
84
|
|
|
if (in_array($name, ['stylesheet', 'javascript', 'header', 'footer'])) { |
|
85
|
|
|
$this->assetExtension->pageAddAsset($name, $value); |
|
|
|
|
|
|
86
|
|
|
} else { |
|
87
|
|
|
$this->pageSetVar($name, $value); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Zikula imposes no restriction on page variable names. |
|
93
|
|
|
* Typical usage is to set `title` `meta.charset` `lang` etc. |
|
94
|
|
|
* array values are set using `.` in the `$name` string (e.g. `meta.charset`) |
|
95
|
|
|
* @param string $name |
|
96
|
|
|
* @param string $value |
|
97
|
|
|
*/ |
|
98
|
|
View Code Duplication |
public function pageSetVar($name, $value) |
|
|
|
|
|
|
99
|
|
|
{ |
|
100
|
|
|
if (empty($name) || empty($value)) { |
|
101
|
|
|
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
$this->pageVars->set($name, $value); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param $name |
|
109
|
|
|
* @param null $default |
|
110
|
|
|
* @return mixed |
|
111
|
|
|
*/ |
|
112
|
|
View Code Duplication |
public function pageGetVar($name, $default = null) |
|
|
|
|
|
|
113
|
|
|
{ |
|
114
|
|
|
if (empty($name)) { |
|
115
|
|
|
throw new \InvalidArgumentException($this->translator->__('Empty argument at') . ':' . __FILE__ . '::' . __LINE__); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return $this->pageVars->get($name, $default); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
This property has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.