Issues (34)

src/Service/View.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 * @package symfony
7
 * @subpackage Service
8
 */
9
10
namespace Aimeos\ShopBundle\Service;
11
12
use Symfony\Bundle\SecurityBundle\Security;
13
use Symfony\Component\HttpFoundation\Response;
14
use Symfony\Component\HttpFoundation\RequestStack;
15
use Symfony\Component\DependencyInjection\Container;
16
use Symfony\Component\Security\Csrf\CsrfTokenManagerInterface;
17
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
18
19
20
/**
21
 * Service providing the view objects
22
 *
23
 * @package symfony
24
 * @subpackage Service
25
 */
26
class View
27
{
28
	private $requestStack;
29
	private $tokenManager;
30
	private $container;
31
	private $security;
32
	private $twig;
33
34
35
	/**
36
	 * Initializes the context manager object
37
	 *
38
	 * @param RequestStack $requestStack Current request stack
39
	 * @param Container $container Container object to access parameters
40
	 */
41
	public function __construct( RequestStack $requestStack, Container $container,
42
		Security $security, CsrfTokenManagerInterface $tokenManager,
43
		\Twig\Environment $twig )
44
	{
45
		$this->requestStack = $requestStack;
46
		$this->tokenManager = $tokenManager;
47
		$this->container = $container;
48
		$this->security = $security;
49
		$this->twig = $twig;
50
	}
51
52
53
	/**
54
	 * Creates the view object for the HTML client.
55
	 *
56
	 * @param \Aimeos\MShop\ContextIface $context Context object
57
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
58
	 * @param string|null $locale Code of the current language or null for no translation
59
	 * @return \Aimeos\Base\View\Iface View object
60
	 */
61
	public function create( \Aimeos\MShop\ContextIface $context, array $templatePaths, $locale = null )
62
	{
63
		$engine = new \Aimeos\Base\View\Engine\Twig( $this->twig );
64
		$view = new \Aimeos\Base\View\Standard( $templatePaths, array( '.html.twig' => $engine ) );
65
66
		$config = $context->config();
67
		$session = $context->session();
68
69
		$this->addCsrf( $view );
70
		$this->addAccess( $view, $context );
71
		$this->addConfig( $view, $config );
72
		$this->addNumber( $view, $config, $locale );
73
		$this->addParam( $view );
74
		$this->addRequest( $view );
75
		$this->addResponse( $view );
76
		$this->addSession( $view, $session );
77
		$this->addTranslate( $view, $locale );
78
		$this->addUrl( $view );
79
80
		$this->initTwig( $view, $this->twig );
81
82
		return $view;
83
	}
84
85
86
	/**
87
	 * Adds the "access" helper to the view object
88
	 *
89
	 * @param \Aimeos\Base\View\Iface $view View object
90
	 * @param \Aimeos\MShop\ContextIface $context Context object
91
	 * @return \Aimeos\Base\View\Iface Modified view object
92
	 */
93
	protected function addAccess( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\ContextIface $context )
94
	{
95
		if( ( $user = $this->security->getUser() ) !== null
96
			&& in_array( 'ROLE_SUPER_ADMIN', (array) $user->getRoles() )
97
		) {
98
			$helper = new \Aimeos\Base\View\Helper\Access\All( $view );
99
		}
100
		else
101
		{
102
			$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, function() use ( $context ) {
103
				return $context->groups();
104
			} );
105
		}
106
107
		$view->addHelper( 'access', $helper );
108
109
		return $view;
110
	}
111
112
113
	/**
114
	 * Adds the "config" helper to the view object
115
	 *
116
	 * @param \Aimeos\Base\View\Iface $view View object
117
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
118
	 * @return \Aimeos\Base\View\Iface Modified view object
119
	 */
120
	protected function addConfig( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config )
121
	{
122
		$config = new \Aimeos\Base\Config\Decorator\Protect( clone $config, ['admin', 'client', 'common', 'resource/fs/baseurl'] );
123
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $config );
124
		$view->addHelper( 'config', $helper );
125
126
		return $view;
127
	}
128
129
130
	/**
131
	 * Adds the "access" helper to the view object
132
	 *
133
	 * @param \Aimeos\Base\View\Iface $view View object
134
	 * @return \Aimeos\Base\View\Iface Modified view object
135
	 */
136
	protected function addCsrf( \Aimeos\Base\View\Iface $view )
137
	{
138
		try {
139
			$token = $this->tokenManager->getToken( '_token' )->getValue();
140
		} catch( \Symfony\Component\HttpFoundation\Exception\SessionNotFoundException $e ) {
141
			$token = '';
142
		}
143
144
		$helper = new \Aimeos\Base\View\Helper\Csrf\Standard( $view, '_token', $token );
145
		$view->addHelper( 'csrf', $helper );
146
147
		return $view;
148
	}
149
150
151
	/**
152
	 * Adds the "number" helper to the view object
153
	 *
154
	 * @param \Aimeos\Base\View\Iface $view View object
155
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
156
	 * @param string|null $locale Code of the current language or null for no translation
157
	 * @return \Aimeos\Base\View\Iface Modified view object
158
	 */
159
	protected function addNumber( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config, $locale )
160
	{
161
		$pattern = $config->get( 'client/html/common/format/pattern' );
162
163
		$helper = new \Aimeos\Base\View\Helper\Number\Locale( $view, $locale, $pattern );
164
		$view->addHelper( 'number', $helper );
165
166
		return $view;
167
	}
168
169
170
	/**
171
	 * Adds the "param" helper to the view object
172
	 *
173
	 * @param \Aimeos\Base\View\Iface $view View object
174
	 * @return \Aimeos\Base\View\Iface Modified view object
175
	 */
176
	protected function addParam( \Aimeos\Base\View\Iface $view )
177
	{
178
		$params = array();
179
		$request = $this->requestStack->getCurrentRequest();
180
181
		if( $request !== null ) {
182
			$params = $request->request->all() + $request->query->all() + $request->attributes->get( '_route_params' );
183
		}
184
185
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
186
		$view->addHelper( 'param', $helper );
187
188
		return $view;
189
	}
190
191
192
	/**
193
	 * Adds the "request" helper to the view object
194
	 *
195
	 * @param \Aimeos\Base\View\Iface $view View object
196
	 * @return \Aimeos\Base\View\Iface Modified view object
197
	 */
198
	protected function addRequest( \Aimeos\Base\View\Iface $view )
199
	{
200
		$request = $this->requestStack->getCurrentRequest();
201
202
		if( $request !== null )
203
		{
204
			$helper = new \Aimeos\Base\View\Helper\Request\Symfony( $view, $request );
205
			$view->addHelper( 'request', $helper );
206
		}
207
208
		return $view;
209
	}
210
211
212
	/**
213
	 * Adds the "response" helper to the view object
214
	 *
215
	 * @param \Aimeos\Base\View\Iface $view View object
216
	 * @return \Aimeos\Base\View\Iface Modified view object
217
	 */
218
	protected function addResponse( \Aimeos\Base\View\Iface $view )
219
	{
220
		$helper = new \Aimeos\Base\View\Helper\Response\Symfony( $view );
221
		$view->addHelper( 'response', $helper );
222
223
		return $view;
224
	}
225
226
227
	/**
228
	 * Adds the "session" helper to the view object
229
	 *
230
	 * @param \Aimeos\Base\View\Iface $view View object
231
	 * @param \Aimeos\Base\Session\Iface $session Session object
232
	 * @return \Aimeos\Base\View\Iface Modified view object
233
	 */
234
	protected function addSession( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Session\Iface $session )
235
	{
236
		$helper = new \Aimeos\Base\View\Helper\Session\Standard( $view, $session );
237
		$view->addHelper( 'session', $helper );
238
239
		return $view;
240
	}
241
242
243
	/**
244
	 * Adds the "translate" helper to the view object
245
	 *
246
	 * @param \Aimeos\Base\View\Iface $view View object
247
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
248
	 * @return \Aimeos\Base\View\Iface Modified view object
249
	 */
250
	protected function addTranslate( \Aimeos\Base\View\Iface $view, $locale )
251
	{
252
		if( $locale !== null )
253
		{
254
			$i18n = $this->container->get( 'aimeos.i18n' )->get( array( $locale ) );
255
			$translation = $i18n[$locale];
256
		}
257
		else
258
		{
259
			$translation = new \Aimeos\Base\Translation\None( 'en' );
260
		}
261
262
		$helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $translation );
263
		$view->addHelper( 'translate', $helper );
264
265
		return $view;
266
	}
267
268
269
	/**
270
	 * Adds the "url" helper to the view object
271
	 *
272
	 * @param \Aimeos\Base\View\Iface $view View object
273
	 * @return \Aimeos\Base\View\Iface Modified view object
274
	 */
275
	protected function addUrl( \Aimeos\Base\View\Iface $view )
276
	{
277
		$fixed = [];
278
279
		if( $request = $this->requestStack->getCurrentRequest() )
280
		{
281
			$fixed['site'] = $request->attributes->get( 'site', $request->query->get( 'site' ) );
282
			$fixed['locale'] = $request->attributes->get( 'locale', $request->query->get( 'locale' ) );
283
			$fixed['currency'] = $request->attributes->get( 'currency', $request->query->get( 'currency' ) );
284
		}
285
286
		$helper = new \Aimeos\Base\View\Helper\Url\Symfony( $view, $this->container->get( 'router' ), array_filter( $fixed ) );
0 ignored issues
show
It seems like $this->container->get('router') can also be of type null; however, parameter $router of Aimeos\Base\View\Helper\Url\Symfony::__construct() does only seem to accept Symfony\Component\Routing\RouterInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

286
		$helper = new \Aimeos\Base\View\Helper\Url\Symfony( $view, /** @scrutinizer ignore-type */ $this->container->get( 'router' ), array_filter( $fixed ) );
Loading history...
287
		$view->addHelper( 'url', $helper );
288
289
		return $view;
290
	}
291
292
293
	/**
294
	 * Adds the Aimeos template functions for Twig
295
	 *
296
	 * @param \Aimeos\Base\View\Iface $view View object
297
	 * @param \Twig\Environment $twig Twig environment object
298
	 */
299
	protected function initTwig( \Aimeos\Base\View\Iface $view, \Twig\Environment $twig )
300
	{
301
		$fcn = function( $key, $default = null ) use ( $view ) {
302
			return $view->config( $key, $default );
303
		};
304
		$twig->addFunction( new \Twig\TwigFunction( 'aiconfig', $fcn ) );
305
306
		$fcn = function( $singular, array $values = array(), $domain = 'client' ) use ( $view ) {
307
			return vsprintf( $view->translate( $domain, $singular ), $values );
308
		};
309
		$twig->addFunction( new \Twig\TwigFunction( 'aitrans', $fcn ) );
310
311
		$fcn = function( $singular, $plural, $number, array $values = array(), $domain = 'client' ) use ( $view ) {
312
			return vsprintf( $view->translate( $domain, $singular, $plural, $number ), $values );
313
		};
314
		$twig->addFunction( new \Twig\TwigFunction( 'aitransplural', $fcn ) );
315
	}
316
}
317