Issues (165)

src/Base/View.php (6 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 )
0 ignored issues
show
Accessing superuser on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
96
		) {
97
			$helper = new \Aimeos\Base\View\Helper\Access\All( $view );
98
		}
99
		else
100
		{
101
			$helper = new \Aimeos\Base\View\Helper\Access\Standard( $view, function() use ( $context ) {
102
				$manager = \Aimeos\MShop::create( $context, 'group' );
103
				$filter = $manager->filter( true )->add( 'group.id', '==', $context->groups() );
104
				return $manager->search( $filter )->col( 'group.code' )->all();
105
			} );
106
		}
107
108
		$view->addHelper( 'access', $helper );
109
110
		return $view;
111
	}
112
113
114
	/**
115
	 * Adds the "config" helper to the view object
116
	 *
117
	 * @param \Aimeos\Base\View\Iface $view View object
118
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
119
	 * @return \Aimeos\Base\View\Iface Modified view object
120
	 */
121
	protected function addConfig( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config ) : \Aimeos\Base\View\Iface
122
	{
123
		$config = new \Aimeos\Base\Config\Decorator\Protect( clone $config, ['resource/*/baseurl'], ['resource'] );
124
		$helper = new \Aimeos\Base\View\Helper\Config\Standard( $view, $config );
125
		$view->addHelper( 'config', $helper );
126
127
		return $view;
128
	}
129
130
131
	/**
132
	 * Adds the "access" helper to the view object
133
	 *
134
	 * @param \Aimeos\Base\View\Iface $view View object
135
	 * @return \Aimeos\Base\View\Iface Modified view object
136
	 */
137
	protected function addCsrf( \Aimeos\Base\View\Iface $view ) : \Aimeos\Base\View\Iface
138
	{
139
		$helper = new \Aimeos\Base\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
140
		$view->addHelper( 'csrf', $helper );
141
142
		return $view;
143
	}
144
145
146
	/**
147
	 * Adds the "number" helper to the view object
148
	 *
149
	 * @param \Aimeos\Base\View\Iface $view View object
150
	 * @param \Aimeos\Base\Config\Iface $config Configuration object
151
	 * @param string|null $locale Code of the current language or null for no translation
152
	 * @return \Aimeos\Base\View\Iface Modified view object
153
	 */
154
	protected function addNumber( \Aimeos\Base\View\Iface $view, \Aimeos\Base\Config\Iface $config,
155
		string $locale = null ) : \Aimeos\Base\View\Iface
156
	{
157
		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

157
		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...
'shop.num_formatter' of type string is incompatible with the type array expected by parameter $options of config(). ( Ignorable by Annotation )

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

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

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

270
			'site' => env( 'SHOP_MULTISHOP' ) ? config( /** @scrutinizer ignore-type */ 'shop.mshop.locale.site', 'default' ) : '',
Loading history...
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

270
			'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...
271
			'locale' => env( 'SHOP_MULTILOCALE' ) ? app()->getLocale() : '',
0 ignored issues
show
The method getLocale() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

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

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