Passed
Push — master ( fb8735...9c9a31 )
by Aimeos
05:27
created

src/Base/View.php (2 issues)

1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2023
6
 */
7
8
namespace Aimeos\Shop\Base;
9
10
11
use Illuminate\Support\Facades\Route;
12
use Illuminate\Support\Facades\Request;
13
use Illuminate\Support\Facades\Response;
14
15
16
/**
17
 * Service providing the view objects
18
 */
19
class View
20
{
21
	/**
22
	 * @var \Illuminate\Contracts\Config\Repository
23
	 */
24
	private $config;
25
26
	/**
27
	 * @var \Aimeos\Shop\Base\I18n
28
	 */
29
	private $i18n;
30
31
	/**
32
	 * @var \Aimeos\Shop\Base\Support
33
	 */
34
	private $support;
35
36
37
	/**
38
	 * Initializes the object
39
	 *
40
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
41
	 * @param \Aimeos\Shop\Base\I18n $i18n I18n object
42
	 * @param \Aimeos\Shop\Base\Support $support Support object
43
	 */
44
	public function __construct( \Illuminate\Contracts\Config\Repository $config,
45
		\Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support )
46
	{
47
		$this->i18n = $i18n;
48
		$this->config = $config;
49
		$this->support = $support;
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,
62
		string $locale = null ) : \Aimeos\Base\View\Iface
63
	{
64
		$engine = new \Aimeos\Base\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) );
65
		$view = new \Aimeos\Base\View\Standard( $templatePaths, array( '.blade.php' => $engine ) );
66
67
		$config = $context->config();
68
		$session = $context->session();
69
70
		$this->addCsrf( $view );
71
		$this->addAccess( $view, $context );
72
		$this->addConfig( $view, $config );
73
		$this->addNumber( $view, $config, $locale );
74
		$this->addParam( $view );
75
		$this->addRequest( $view );
76
		$this->addResponse( $view );
77
		$this->addSession( $view, $session );
78
		$this->addTranslate( $view, $locale );
79
		$this->addUrl( $view );
80
81
		return $view;
82
	}
83
84
85
	/**
86
	 * Adds the "access" helper to the view object
87
	 *
88
	 * @param \Aimeos\Base\View\Iface $view View object
89
	 * @param \Aimeos\MShop\ContextIface $context Context object
90
	 * @return \Aimeos\Base\View\Iface Modified view object
91
	 */
92
	protected function addAccess( \Aimeos\Base\View\Iface $view, \Aimeos\MShop\ContextIface $context ) : \Aimeos\Base\View\Iface
93
	{
94
		if( $this->config->get( 'shop.accessControl', true ) === false
95
			|| ( ( $user = \Illuminate\Support\Facades\Auth::user() ) !== null && $user->superuser )
96
		) {
97
			$helper = new \Aimeos\Base\View\Helper\Access\All( $view );
98
		}
99
		else
100
		{
101
			$support = $this->support;
102
103
			$fcn = function() use ( $support, $context ) {
104
				return $support->getGroups( $context );
105
			};
106
107
			$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, $fcn );
108
		}
109
110
		$view->addHelper( 'access', $helper );
111
112
		return $view;
113
	}
114
115
116
	/**
117
	 * Adds the "config" helper to the view object
118
	 *
119
	 * @param \Aimeos\Base\View\Iface $view View object
120
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
121
	 * @return \Aimeos\Base\View\Iface Modified view object
122
	 */
123
	protected function addConfig( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config ) : \Aimeos\Base\View\Iface
124
	{
125
		$config = new \Aimeos\Base\Config\Decorator\Protect( clone $config, ['resource/*/baseurl'], ['resource'] );
126
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $config );
127
		$view->addHelper( 'config', $helper );
128
129
		return $view;
130
	}
131
132
133
	/**
134
	 * Adds the "access" helper to the view object
135
	 *
136
	 * @param \Aimeos\Base\View\Iface $view View object
137
	 * @return \Aimeos\Base\View\Iface Modified view object
138
	 */
139
	protected function addCsrf( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
140
	{
141
		$helper = new \Aimeos\Base\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
142
		$view->addHelper( 'csrf', $helper );
143
144
		return $view;
145
	}
146
147
148
	/**
149
	 * Adds the "number" helper to the view object
150
	 *
151
	 * @param \Aimeos\Base\View\Iface $view View object
152
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
153
	 * @param string|null $locale Code of the current language or null for no translation
154
	 * @return \Aimeos\Base\View\Iface Modified view object
155
	 */
156
	protected function addNumber( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config,
157
		string $locale = null ) : \Aimeos\Base\View\Iface
158
	{
159
		if( config( 'shop.num_formatter', 'Locale' ) === 'Locale' )
0 ignored issues
show
The call to config() has too many arguments starting with 'Locale'. ( Ignorable by Annotation )

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

159
		if( /** @scrutinizer ignore-call */ config( 'shop.num_formatter', 'Locale' ) === 'Locale' )

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
160
		{
161
			$pattern = $config->get( 'client/html/common/format/pattern' );
162
			$helper = new \Aimeos\Base\View\Helper\Number\Locale( $view, $locale, $pattern );
163
		}
164
		else
165
		{
166
			$sep1000 = $config->get( 'client/html/common/format/separator1000', '' );
167
			$decsep = $config->get( 'client/html/common/format/separatorDecimal', '.' );
168
			$helper = new \Aimeos\Base\View\Helper\Number\Standard( $view, $decsep, $sep1000 );
169
		}
170
171
		return $view->addHelper( 'number', $helper );
172
	}
173
174
175
	/**
176
	 * Adds the "param" helper to the view object
177
	 *
178
	 * @param \Aimeos\Base\View\Iface $view View object
179
	 * @return \Aimeos\Base\View\Iface Modified view object
180
	 */
181
	protected function addParam( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
182
	{
183
		$params = ( Route::current() ? Route::current()->parameters() : array() ) + Request::all();
184
		$helper = new \Aimeos\Base\View\Helper\Param\Standard( $view, $params );
185
		$view->addHelper( 'param', $helper );
186
187
		return $view;
188
	}
189
190
191
	/**
192
	 * Adds the "request" helper to the view object
193
	 *
194
	 * @param \Aimeos\Base\View\Iface $view View object
195
	 * @return \Aimeos\Base\View\Iface Modified view object
196
	 */
197
	protected function addRequest( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
198
	{
199
		$helper = new \Aimeos\Base\View\Helper\Request\Laravel( $view, Request::instance() );
200
		$view->addHelper( 'request', $helper );
201
202
		return $view;
203
	}
204
205
206
	/**
207
	 * Adds the "response" helper to the view object
208
	 *
209
	 * @param \Aimeos\Base\View\Iface $view View object
210
	 * @return \Aimeos\Base\View\Iface Modified view object
211
	 */
212
	protected function addResponse( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
213
	{
214
		$helper = new \Aimeos\Base\View\Helper\Response\Laravel( $view );
215
		$view->addHelper( 'response', $helper );
216
217
		return $view;
218
	}
219
220
221
	/**
222
	 * Adds the "session" helper to the view object
223
	 *
224
	 * @param \Aimeos\Base\View\Iface $view View object
225
	 * @param \Aimeos\Base\Session\Iface $session Session object
226
	 * @return \Aimeos\Base\View\Iface Modified view object
227
	 */
228
	protected function addSession( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Session\Iface $session ) : \Aimeos\Base\View\Iface
229
	{
230
		$helper = new \Aimeos\Base\View\Helper\Session\Standard( $view, $session );
231
		$view->addHelper( 'session', $helper );
232
233
		return $view;
234
	}
235
236
237
	/**
238
	 * Adds the "translate" helper to the view object
239
	 *
240
	 * @param \Aimeos\Base\View\Iface $view View object
241
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
242
	 * @return \Aimeos\Base\View\Iface Modified view object
243
	 */
244
	protected function addTranslate( \Aimeos\Base\View\Iface $view, string $locale = null ) : \Aimeos\Base\View\Iface
245
	{
246
		if( $locale !== null )
247
		{
248
			$i18n = $this->i18n->get( array( $locale ) );
249
			$translation = $i18n[$locale];
250
		}
251
		else
252
		{
253
			$translation = new \Aimeos\Base\Translation\None( 'en' );
254
		}
255
256
		$helper = new \Aimeos\Base\View\Helper\Translate\Standard( $view, $translation );
257
		$view->addHelper( 'translate', $helper );
258
259
		return $view;
260
	}
261
262
263
	/**
264
	 * Adds the "url" helper to the view object
265
	 *
266
	 * @param \Aimeos\Base\View\Iface $view View object
267
	 * @return \Aimeos\Base\View\Iface Modified view object
268
	 */
269
	protected function addUrl( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
270
	{
271
		$fixed = [
272
			'site' => env( 'SHOP_MULTISHOP' ) ? config( 'shop.mshop.locale.site', 'default' ) : '',
0 ignored issues
show
The call to config() has too many arguments starting with 'default'. ( Ignorable by Annotation )

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

272
			'site' => env( 'SHOP_MULTISHOP' ) ? /** @scrutinizer ignore-call */ config( 'shop.mshop.locale.site', 'default' ) : '',

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
273
			'locale' => env( 'SHOP_MULTILOCALE' ) ? app()->getLocale() : '',
274
			'currency' => ''
275
		];
276
277
		if( Route::current() )
278
		{
279
			$fixed['site'] = Request::route( 'site', $fixed['site'] );
280
			$fixed['locale'] = Request::route( 'locale', $fixed['locale'] );
281
			$fixed['currency'] = Request::route( 'currency', $fixed['currency'] );
282
		}
283
284
		$fixed['site'] = Request::input( 'site', $fixed['site'] );
285
		$fixed['locale'] = Request::input( 'locale', $fixed['locale'] );
286
		$fixed['currency'] = Request::input( 'currency', $fixed['currency'] );
287
288
		$helper = new \Aimeos\Base\View\Helper\Url\Laravel( $view, app( 'url' ), array_filter( $fixed ) );
289
		$view->addHelper( 'url', $helper );
290
291
		return $view;
292
	}
293
}