View::addCsrf()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0
5
 * @copyright Aimeos (aimeos.org), 2016
6
 * @package Slim
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Slim\Base;
11
12
use Psr\Container\ContainerInterface;
13
use Psr\Http\Message\ServerRequestInterface;
14
use Psr\Http\Message\ResponseInterface;
15
16
17
/**
18
 * Service providing the view objects
19
 *
20
 * @package Slim
21
 * @subpackage Base
22
 */
23
class View
24
{
25
	private $container;
26
27
28
	/**
29
	 * Initializes the object
30
	 *
31
	 * @param ContainerInterface $container Dependency container
32
	 */
33
	public function __construct( ContainerInterface $container )
34
	{
35
		$this->container = $container;
36
	}
37
38
39
	/**
40
	 * Creates the view object for the HTML client.
41
	 *
42
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
43
	 * @param ServerRequestInterface $request Request object
44
	 * @param ResponseInterface $response Response object
45
	 * @param array $attributes Associative list of URI parameters
46
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
47
	 * @param string|null $locale Code of the current language or null for no translation
48
	 * @return \Aimeos\MW\View\Iface View object
49
	 */
50
	public function create( \Aimeos\MShop\Context\Item\Iface $context, ServerRequestInterface $request,
51
		ResponseInterface $response, array $attributes, array $templatePaths, string $locale = null ) : \Aimeos\MW\View\Iface
52
	{
53
		$iface = 'Slim\Views\Twig';
54
		$params = $attributes + (array) $request->getParsedBody() + (array) $request->getQueryParams();
55
56
		if( isset( $this->container['view'] ) && $this->container['view'] instanceof $iface )
57
		{
58
			$twig = $this->container['view']->getEnvironment();
59
			$engines = array( '.html.twig' => new \Aimeos\MW\View\Engine\Twig( $twig ) );
60
61
			$view = new \Aimeos\MW\View\Standard( $templatePaths, $engines );
62
			$this->initTwig( $view, $twig );
63
		}
64
		else
65
		{
66
			$view = new \Aimeos\MW\View\Standard( $templatePaths );
67
		}
68
69
		$config = $context->getConfig();
70
		$session = $context->getSession();
71
72
		$this->addAccess( $view );
73
		$this->addConfig( $view, $config );
74
		$this->addCsrf( $view, $request );
75
		$this->addNumber( $view, $config, $locale );
76
		$this->addParam( $view, $params );
77
		$this->addRequest( $view, $request );
78
		$this->addResponse( $view, $response );
79
		$this->addSession( $view, $session );
80
		$this->addTranslate( $view, $locale );
81
		$this->addUrl( $view, $attributes );
82
83
		return $view;
84
	}
85
86
87
	/**
88
	 * Adds the "access" helper to the view object
89
	 *
90
	 * @param \Aimeos\MW\View\Iface $view View object
91
	 * @return \Aimeos\MW\View\Iface Modified view object
92
	 */
93
	protected function addAccess( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
94
	{
95
		$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
96
		$view->addHelper( 'access', $helper );
97
98
		return $view;
99
	}
100
101
102
	/**
103
	 * Adds the "config" helper to the view object
104
	 *
105
	 * @param \Aimeos\MW\View\Iface $view View object
106
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
107
	 * @return \Aimeos\MW\View\Iface Modified view object
108
	 */
109
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config ) : \Aimeos\MW\View\Iface
110
	{
111
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, ['admin', 'client', 'resource/fs/baseurl'] );
112
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
113
		$view->addHelper( 'config', $helper );
114
115
		return $view;
116
	}
117
118
119
	/**
120
	 * Adds the "access" helper to the view object
121
	 *
122
	 * @param \Aimeos\MW\View\Iface $view View object
123
	 * @param ServerRequestInterface $request Request object
124
	 * @return \Aimeos\MW\View\Iface Modified view object
125
	 */
126
	protected function addCsrf( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request ) : \Aimeos\MW\View\Iface
127
	{
128
		$name = $request->getAttribute( 'csrf_name' );
129
		$value = $request->getAttribute( 'csrf_value' );
130
131
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, (string) $name, (string) $value );
132
		$view->addHelper( 'csrf', $helper );
133
134
		return $view;
135
	}
136
137
138
	/**
139
	 * Adds the "number" helper to the view object
140
	 *
141
	 * @param \Aimeos\MW\View\Iface $view View object
142
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
143
	 * @param string|null $locale Code of the current language or null for no translation
144
	 * @return \Aimeos\MW\View\Iface Modified view object
145
	 */
146
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config,
147
		string $locale = null ) : \Aimeos\MW\View\Iface
148
	{
149
		$pattern = $config->get( 'client/html/common/format/pattern' );
150
151
		$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale, $pattern );
152
		$view->addHelper( 'number', $helper );
153
154
		return $view;
155
	}
156
157
158
	/**
159
	 * Adds the "param" helper to the view object
160
	 *
161
	 * @param \Aimeos\MW\View\Iface $view View object
162
	 * @param array $attributes Associative list of request parameters
163
	 * @return \Aimeos\MW\View\Iface Modified view object
164
	 */
165
	protected static function addParam( \Aimeos\MW\View\Iface $view, array $params ) : \Aimeos\MW\View\Iface
166
	{
167
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
168
		$view->addHelper( 'param', $helper );
169
170
		return $view;
171
	}
172
173
174
	/**
175
	 * Adds the "request" helper to the view object
176
	 *
177
	 * @param \Aimeos\MW\View\Iface $view View object
178
	 * @param ServerRequestInterface $request Request object
179
	 * @return \Aimeos\MW\View\Iface Modified view object
180
	 */
181
	protected static function addRequest( \Aimeos\MW\View\Iface $view, ServerRequestInterface $request ) : \Aimeos\MW\View\Iface
182
	{
183
		$helper = new \Aimeos\MW\View\Helper\Request\Slim( $view, $request );
184
		$view->addHelper( 'request', $helper );
185
186
		return $view;
187
	}
188
189
190
	/**
191
	 * Adds the "response" helper to the view object
192
	 *
193
	 * @param \Aimeos\MW\View\Iface $view View object
194
	 * @param ResponseInterface $response Response object
195
	 * @return \Aimeos\MW\View\Iface Modified view object
196
	 */
197
	protected static function addResponse( \Aimeos\MW\View\Iface $view, ResponseInterface $response ) : \Aimeos\MW\View\Iface
198
	{
199
		$helper = new \Aimeos\MW\View\Helper\Response\Slim( $view, $response );
200
		$view->addHelper( 'response', $helper );
201
202
		return $view;
203
	}
204
205
206
	/**
207
	 * Adds the "session" helper to the view object
208
	 *
209
	 * @param \Aimeos\MW\View\Iface $view View object
210
	 * @param \Aimeos\MW\Session\Iface $session Session object
211
	 * @return \Aimeos\MW\View\Iface Modified view object
212
	 */
213
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session ) : \Aimeos\MW\View\Iface
214
	{
215
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
216
		$view->addHelper( 'session', $helper );
217
218
		return $view;
219
	}
220
221
222
	/**
223
	 * Adds the "translate" helper to the view object
224
	 *
225
	 * @param \Aimeos\MW\View\Iface $view View object
226
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
227
	 * @return \Aimeos\MW\View\Iface Modified view object
228
	 */
229
	protected function addTranslate( \Aimeos\MW\View\Iface $view, string $locale = null ) : \Aimeos\MW\View\Iface
230
	{
231
		if( $locale !== null )
232
		{
233
			$i18n = $this->container->get( 'aimeos.i18n' )->get( array( $locale ) );
234
			$translation = $i18n[$locale];
235
		}
236
		else
237
		{
238
			$translation = new \Aimeos\MW\Translation\None( 'en' );
239
		}
240
241
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
242
		$view->addHelper( 'translate', $helper );
243
244
		return $view;
245
	}
246
247
248
	/**
249
	 * Adds the "url" helper to the view object
250
	 *
251
	 * @param \Aimeos\MW\View\Iface $view View object
252
	 * @param array $attributes Associative list of URI parameters
253
	 * @return \Aimeos\MW\View\Iface Modified view object
254
	 */
255
	protected function addUrl( \Aimeos\MW\View\Iface $view, array $attributes ) : \Aimeos\MW\View\Iface
256
	{
257
		$fixed = array();
258
259
		if( isset( $attributes['site'] ) ) {
260
			$fixed['site'] = $attributes['site'];
261
		}
262
263
		if( isset( $attributes['locale'] ) ) {
264
			$fixed['locale'] = $attributes['locale'];
265
		}
266
267
		if( isset( $attributes['currency'] ) ) {
268
			$fixed['currency'] = $attributes['currency'];
269
		}
270
271
		$helper = new \Aimeos\MW\View\Helper\Url\Slim( $view, $this->container->get( 'router' ), $fixed );
272
		$view->addHelper( 'url', $helper );
273
274
		return $view;
275
	}
276
277
278
	/**
279
	 * Adds the Aimeos template functions for Twig
280
	 *
281
	 * @param \Aimeos\MW\View\Iface $view View object
282
	 * @param \Twig_Environment $twig Twig environment object
283
	 */
284
	protected function initTwig( \Aimeos\MW\View\Iface $view, \Twig_Environment $twig )
285
	{
286
		$fcn = function( $key, $default = null ) use ( $view ) {
287
			return $view->config( $key, $default );
288
		};
289
		$twig->addFunction( new \Twig_SimpleFunction( 'aiconfig', $fcn ) );
290
291
		$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) {
292
			return vsprintf( $view->translate( $domain, $singular ), $values );
293
		};
294
		$twig->addFunction( new \Twig_SimpleFunction( 'aitrans', $fcn ) );
295
296
		$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) {
297
			return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values );
298
		};
299
		$twig->addFunction( new \Twig_SimpleFunction( 'aitransplural', $fcn ) );
300
	}
301
}
302