Issues (67)

src/Aimeos/Shop/Base/View.php (1 issue)

Labels
Severity
1
<?php
2
3
/**
4
 * @license MIT, http://opensource.org/licenses/MIT
5
 * @copyright Aimeos (aimeos.org), 2015-2016
6
 * @package laravel
7
 * @subpackage Base
8
 */
9
10
namespace Aimeos\Shop\Base;
11
12
13
use Illuminate\Support\Facades\Route;
14
use Illuminate\Support\Facades\Request;
15
use Illuminate\Support\Facades\Response;
16
17
18
/**
19
 * Service providing the view objects
20
 *
21
 * @package laravel
22
 * @subpackage Base
23
 */
24
class View
25
{
26
	/**
27
	 * @var \Illuminate\Contracts\Config\Repository
28
	 */
29
	private $config;
30
31
	/**
32
	 * @var \Aimeos\Shop\Base\I18n
33
	 */
34
	private $i18n;
35
36
	/**
37
	 * @var \Aimeos\Shop\Base\Support
38
	 */
39
	private $support;
40
41
42
	/**
43
	 * Initializes the object
44
	 *
45
	 * @param \Illuminate\Contracts\Config\Repository $config Configuration object
46
	 * @param \Aimeos\Shop\Base\I18n $i18n I18n object
47
	 * @param \Aimeos\Shop\Base\Support $support Support object
48
	 */
49
	public function __construct( \Illuminate\Contracts\Config\Repository $config,
50
		\Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support )
51
	{
52
		$this->i18n = $i18n;
53
		$this->config = $config;
54
		$this->support = $support;
55
	}
56
57
58
	/**
59
	 * Creates the view object for the HTML client.
60
	 *
61
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
62
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
63
	 * @param string|null $locale Code of the current language or null for no translation
64
	 * @return \Aimeos\MW\View\Iface View object
65
	 */
66
	public function create( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths,
67
		string $locale = null ) : \Aimeos\MW\View\Iface
68
	{
69
		$engine = new \Aimeos\MW\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) );
70
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.blade.php' => $engine ) );
71
72
		$config = $context->getConfig();
73
		$session = $context->getSession();
74
75
		$this->addCsrf( $view );
76
		$this->addAccess( $view, $context );
77
		$this->addConfig( $view, $config );
78
		$this->addNumber( $view, $config, $locale );
79
		$this->addParam( $view );
80
		$this->addRequest( $view );
81
		$this->addResponse( $view );
82
		$this->addSession( $view, $session );
83
		$this->addTranslate( $view, $locale );
84
		$this->addUrl( $view );
85
86
		return $view;
87
	}
88
89
90
	/**
91
	 * Adds the "access" helper to the view object
92
	 *
93
	 * @param \Aimeos\MW\View\Iface $view View object
94
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
95
	 * @return \Aimeos\MW\View\Iface Modified view object
96
	 */
97
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MW\View\Iface
98
	{
99
		if( $this->config->get( 'shop.accessControl', true ) === false
100
			|| ( ( $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...
101
		) {
102
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
103
		}
104
		else
105
		{
106
			$support = $this->support;
107
108
			$fcn = function() use ( $support, $context ) {
109
				return $support->getGroups( $context );
110
			};
111
112
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
113
		}
114
115
		$view->addHelper( 'access', $helper );
116
117
		return $view;
118
	}
119
120
121
	/**
122
	 * Adds the "config" helper to the view object
123
	 *
124
	 * @param \Aimeos\MW\View\Iface $view View object
125
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
126
	 * @return \Aimeos\MW\View\Iface Modified view object
127
	 */
128
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config ) : \Aimeos\MW\View\Iface
129
	{
130
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, ['admin', 'client', 'resource/fs/baseurl'] );
131
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
132
		$view->addHelper( 'config', $helper );
133
134
		return $view;
135
	}
136
137
138
	/**
139
	 * Adds the "access" helper to the view object
140
	 *
141
	 * @param \Aimeos\MW\View\Iface $view View object
142
	 * @return \Aimeos\MW\View\Iface Modified view object
143
	 */
144
	protected function addCsrf( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
145
	{
146
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
147
		$view->addHelper( 'csrf', $helper );
148
149
		return $view;
150
	}
151
152
153
	/**
154
	 * Adds the "number" helper to the view object
155
	 *
156
	 * @param \Aimeos\MW\View\Iface $view View object
157
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
158
	 * @param string|null $locale Code of the current language or null for no translation
159
	 * @return \Aimeos\MW\View\Iface Modified view object
160
	 */
161
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config,
162
		string $locale = null ) : \Aimeos\MW\View\Iface
163
	{
164
		if( config( 'shop.num_formatter', 'Locale' ) === 'Locale' )
165
		{
166
			$pattern = $config->get( 'client/html/common/format/pattern' );
167
			$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale, $pattern );
168
		}
169
		else
170
		{
171
			$sep1000 = $config->get( 'client/html/common/format/separator1000', '' );
172
			$decsep = $config->get( 'client/html/common/format/separatorDecimal', '.' );
173
			$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $decsep, $sep1000 );
174
		}
175
176
		return $view->addHelper( 'number', $helper );
177
	}
178
179
180
	/**
181
	 * Adds the "param" 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 addParam( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
187
	{
188
		$params = ( Route::current() ? Route::current()->parameters() : array() ) + Request::all();
189
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
190
		$view->addHelper( 'param', $helper );
191
192
		return $view;
193
	}
194
195
196
	/**
197
	 * Adds the "request" helper to the view object
198
	 *
199
	 * @param \Aimeos\MW\View\Iface $view View object
200
	 * @return \Aimeos\MW\View\Iface Modified view object
201
	 */
202
	protected function addRequest( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
203
	{
204
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
205
		$view->addHelper( 'request', $helper );
206
207
		return $view;
208
	}
209
210
211
	/**
212
	 * Adds the "response" helper to the view object
213
	 *
214
	 * @param \Aimeos\MW\View\Iface $view View object
215
	 * @return \Aimeos\MW\View\Iface Modified view object
216
	 */
217
	protected function addResponse( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
218
	{
219
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
220
		$view->addHelper( 'response', $helper );
221
222
		return $view;
223
	}
224
225
226
	/**
227
	 * Adds the "session" helper to the view object
228
	 *
229
	 * @param \Aimeos\MW\View\Iface $view View object
230
	 * @param \Aimeos\MW\Session\Iface $session Session object
231
	 * @return \Aimeos\MW\View\Iface Modified view object
232
	 */
233
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session ) : \Aimeos\MW\View\Iface
234
	{
235
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
236
		$view->addHelper( 'session', $helper );
237
238
		return $view;
239
	}
240
241
242
	/**
243
	 * Adds the "translate" helper to the view object
244
	 *
245
	 * @param \Aimeos\MW\View\Iface $view View object
246
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
247
	 * @return \Aimeos\MW\View\Iface Modified view object
248
	 */
249
	protected function addTranslate( \Aimeos\MW\View\Iface $view, string $locale = null ) : \Aimeos\MW\View\Iface
250
	{
251
		if( $locale !== null )
252
		{
253
			$i18n = $this->i18n->get( array( $locale ) );
254
			$translation = $i18n[$locale];
255
		}
256
		else
257
		{
258
			$translation = new \Aimeos\MW\Translation\None( 'en' );
259
		}
260
261
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
262
		$view->addHelper( 'translate', $helper );
263
264
		return $view;
265
	}
266
267
268
	/**
269
	 * Adds the "url" helper to the view object
270
	 *
271
	 * @param \Aimeos\MW\View\Iface $view View object
272
	 * @return \Aimeos\MW\View\Iface Modified view object
273
	 */
274
	protected function addUrl( \Aimeos\MW\View\Iface $view ) : \Aimeos\MW\View\Iface
275
	{
276
		$fixed = [
277
			'site' => Request::get( 'site', 'default' ),
278
			'locale' => Request::get( 'locale' ),
279
			'currency' => Request::get( 'currency' )
280
		];
281
282
		if( Route::current() )
283
		{
284
			$fixed['site'] = Route::input( 'site', $fixed['site'] );
285
			$fixed['locale'] = Route::input( 'locale', $fixed['locale'] );
286
			$fixed['currency'] = Route::input( 'currency', $fixed['currency'] );
287
		}
288
289
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app( 'url' ), array_filter( $fixed ) );
290
		$view->addHelper( 'url', $helper );
291
292
		return $view;
293
	}
294
}