Completed
Push — master ( e3ddba...ecd828 )
by Aimeos
02:57
created

View::initTwig()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 2
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package symfony
7
 * @subpackage Service
8
 */
9
10
namespace Aimeos\ShopBundle\Service;
11
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\RequestStack;
14
use Symfony\Component\DependencyInjection\Container;
15
16
17
/**
18
 * Service providing the view objects
19
 *
20
 * @package symfony
21
 * @subpackage Service
22
 */
23
class View
24
{
25
	private $requestStack;
26
	private $container;
27
28
29
	/**
30
	 * Initializes the context manager object
31
	 *
32
	 * @param RequestStack $requestStack Current request stack
33
	 * @param Container $container Container object to access parameters
34
	 */
35
	public function __construct( RequestStack $requestStack, Container $container )
36
	{
37
		$this->requestStack = $requestStack;
38
		$this->container = $container;
39
	}
40
41
42
	/**
43
	 * Creates the view object for the HTML client.
44
	 *
45
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
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, array $templatePaths, $locale = null )
51
	{
52
		$config = $context->getConfig();
53
		$twig = $this->container->get( 'twig' );
54
		$engine = new \Aimeos\MW\View\Engine\Twig( $twig );
55
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.html.twig' => $engine ) );
56
57
		$this->addCsrf( $view );
58
		$this->addAccess( $view, $context );
59
		$this->addConfig( $view, $config );
60
		$this->addNumber( $view, $config );
61
		$this->addParam( $view );
62
		$this->addRequest( $view );
63
		$this->addResponse( $view );
64
		$this->addTranslate( $view, $locale );
65
		$this->addUrl( $view );
66
67
		$this->initTwig( $view, $twig );
68
69
		return $view;
70
	}
71
72
73
	/**
74
	 * Adds the "access" helper to the view object
75
	 *
76
	 * @param \Aimeos\MW\View\Iface $view View object
77
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
78
	 * @return \Aimeos\MW\View\Iface Modified view object
79
	 */
80
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
81
	{
82
		$container = $this->container;
83
		$token = $this->container->get( 'security.token_storage' )->getToken();
84
85
		if( is_object( $token ) && is_object( $token->getUser() )
86
			&& in_array( 'ROLE_ADMIN', (array) $token->getUser()->getRoles() ) )
87
		{
88
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
89
		}
90
		else
91
		{
92
			$fcn = function() use ( $container, $context ) {
93
				return $container->get( 'aimeos_support' )->getGroups( $context );
94
			};
95
96
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
97
		}
98
99
		$view->addHelper( 'access', $helper );
100
101
		return $view;
102
	}
103
104
105
	/**
106
	 * Adds the "config" helper to the view object
107
	 *
108
	 * @param \Aimeos\MW\View\Iface $view View object
109
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
110
	 * @return \Aimeos\MW\View\Iface Modified view object
111
	 */
112
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
113
	{
114
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
115
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
116
		$view->addHelper( 'config', $helper );
117
118
		return $view;
119
	}
120
121
122
	/**
123
	 * Adds the "access" helper to the view object
124
	 *
125
	 * @param \Aimeos\MW\View\Iface $view View object
126
	 * @return \Aimeos\MW\View\Iface Modified view object
127
	 */
128
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
129
	{
130
		$token = $this->container->get( 'security.csrf.token_manager' )->getToken( '_token' );
131
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', $token->getValue() );
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
	 * @return \Aimeos\MW\View\Iface Modified view object
144
	 */
145
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
146
	{
147
		$sepDec = $config->get( 'client/html/common/format/separatorDecimal', '.' );
148
		$sep1000 = $config->get( 'client/html/common/format/separator1000', ' ' );
149
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
150
151
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
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
	 * @return \Aimeos\MW\View\Iface Modified view object
163
	 */
164
	protected function addParam( \Aimeos\MW\View\Iface $view )
165
	{
166
		$params = array();
167
		$request = $this->requestStack->getMasterRequest();
168
169
		if( $request !== null ) {
170
			$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' );
171
		}
172
173
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
174
		$view->addHelper( 'param', $helper );
175
176
		return $view;
177
	}
178
179
180
	/**
181
	 * Adds the "request" helper to the view object
182
	 *
183
	 * @param \Aimeos\MW\View\Iface $view View object
184
	 * @return \Aimeos\MW\View\Iface Modified view object
185
	 */
186
	protected function addRequest( \Aimeos\MW\View\Iface $view )
187
	{
188
		$request = $this->requestStack->getMasterRequest();
189
190
		if( $request !== null )
191
		{
192
			$helper = new \Aimeos\MW\View\Helper\Request\Symfony2( $view, $request );
193
			$view->addHelper( 'request', $helper );
194
		}
195
196
		return $view;
197
	}
198
199
200
	/**
201
	 * Adds the "response" helper to the view object
202
	 *
203
	 * @param \Aimeos\MW\View\Iface $view View object
204
	 * @return \Aimeos\MW\View\Iface Modified view object
205
	 */
206
	protected function addResponse( \Aimeos\MW\View\Iface $view )
207
	{
208
		$helper = new \Aimeos\MW\View\Helper\Response\Symfony2( $view );
209
		$view->addHelper( 'response', $helper );
210
211
		return $view;
212
	}
213
214
215
	/**
216
	 * Adds the "translate" helper to the view object
217
	 *
218
	 * @param \Aimeos\MW\View\Iface $view View object
219
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
220
	 * @return \Aimeos\MW\View\Iface Modified view object
221
	 */
222
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
223
	{
224
		if( $locale !== null )
225
		{
226
			$i18n = $this->container->get( 'aimeos_i18n' )->get( array( $locale ) );
227
			$translation = $i18n[$locale];
228
		}
229
		else
230
		{
231
			$translation = new \Aimeos\MW\Translation\None( 'en' );
232
		}
233
234
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
235
		$view->addHelper( 'translate', $helper );
236
237
		return $view;
238
	}
239
240
241
	/**
242
	 * Adds the "url" helper to the view object
243
	 *
244
	 * @param \Aimeos\MW\View\Iface $view View object
245
	 * @return \Aimeos\MW\View\Iface Modified view object
246
	 */
247
	protected function addUrl( \Aimeos\MW\View\Iface $view )
248
	{
249
		$fixed = array();
250
		$request = $this->requestStack->getMasterRequest();
251
252
		if( $request !== null )
253
		{
254
			$attr = $request->attributes;
255
256
			if( ( $site = $attr->get( 'site' ) ) !== null ) {
257
				$fixed['site'] = $site;
258
			}
259
260
			if( ( $lang = $attr->get( 'locale' ) ) !== null ) {
261
				$fixed['locale'] = $lang;
262
			}
263
264
			if( ( $currency = $attr->get( 'currency' ) ) !== null ) {
265
				$fixed['currency'] = $currency;
266
			}
267
		}
268
269
		$helper = new \Aimeos\MW\View\Helper\Url\Symfony2( $view, $this->container->get( 'router' ), $fixed );
270
		$view->addHelper( 'url', $helper );
271
272
		return $view;
273
	}
274
275
276
	/**
277
	 * Adds the Aimeos template functions for Twig
278
	 *
279
	 * @param \Twig_Environment $env Twig environment object
0 ignored issues
show
Bug introduced by
There is no parameter named $env. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
280
	 * @param \Aimeos\MW\View\Iface $view View object
281
	 */
282
	protected function initTwig( \Aimeos\MW\View\Iface $view, \Twig_Environment $twig )
283
	{
284
		$fcn = function( $key, $default = null ) use ( $view ) {
285
			return $view->config( $key, $default );
286
		};
287
		$twig->addFunction( new \Twig_SimpleFunction( 'aiconfig', $fcn ) );
288
289
		$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) {
290
			return vsprintf( $view->translate( $domain, $singular ), $values );
291
		};
292
		$twig->addFunction( new \Twig_SimpleFunction( 'aitrans', $fcn ) );
293
294
		$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) {
295
			return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values );
296
		};
297
		$twig->addFunction( new \Twig_SimpleFunction( 'aitransplural', $fcn ) );
298
	}
299
}
300