Issues (69)

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

Checks if property accesses are on concrete types.

Bug Major
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, $locale = null )
67
	{
68
		$engine = new \Aimeos\MW\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) );
69
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.blade.php' => $engine ) );
70
71
		$config = $context->getConfig();
72
		$session = $context->getSession();
73
74
		$this->addCsrf( $view );
75
		$this->addAccess( $view, $context );
76
		$this->addConfig( $view, $config );
77
		$this->addNumber( $view, $config, $locale );
78
		$this->addParam( $view );
79
		$this->addRequest( $view );
80
		$this->addResponse( $view );
81
		$this->addSession( $view, $session );
82
		$this->addTranslate( $view, $locale );
83
		$this->addUrl( $view );
84
85
		return $view;
86
	}
87
88
89
	/**
90
	 * Adds the "access" helper to the view object
91
	 *
92
	 * @param \Aimeos\MW\View\Iface $view View object
93
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
94
	 * @return \Aimeos\MW\View\Iface Modified view object
95
	 */
96
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
97
	{
98
		if( $this->config->get( 'shop.accessControl', true ) === false
99
			|| ( ( $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...
100
		) {
101
			$helper = new \Aimeos\MW\View\Helper\Access\All( $view );
102
		}
103
		else
104
		{
105
			$support = $this->support;
106
107
			$fcn = function() use ( $support, $context ) {
108
				return $support->getGroups( $context );
109
			};
110
111
			$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
112
		}
113
114
		$view->addHelper( 'access', $helper );
115
116
		return $view;
117
	}
118
119
120
	/**
121
	 * Adds the "config" helper to the view object
122
	 *
123
	 * @param \Aimeos\MW\View\Iface $view View object
124
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
125
	 * @return \Aimeos\MW\View\Iface Modified view object
126
	 */
127
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
128
	{
129
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, ['admin', 'client', 'resource/fs/baseurl'] );
130
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
131
		$view->addHelper( 'config', $helper );
132
133
		return $view;
134
	}
135
136
137
	/**
138
	 * Adds the "access" helper to the view object
139
	 *
140
	 * @param \Aimeos\MW\View\Iface $view View object
141
	 * @return \Aimeos\MW\View\Iface Modified view object
142
	 */
143
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
144
	{
145
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
146
		$view->addHelper( 'csrf', $helper );
147
148
		return $view;
149
	}
150
151
152
	/**
153
	 * Adds the "number" helper to the view object
154
	 *
155
	 * @param \Aimeos\MW\View\Iface $view View object
156
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
157
	 * @param string|null $locale Code of the current language or null for no translation
158
	 * @return \Aimeos\MW\View\Iface Modified view object
159
	 */
160
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config, $locale )
161
	{
162
		$pattern = $config->get( 'client/html/common/format/pattern' );
163
164
		$helper = new \Aimeos\MW\View\Helper\Number\Locale( $view, $locale, $pattern );
165
		$view->addHelper( 'number', $helper );
166
167
		return $view;
168
	}
169
170
171
	/**
172
	 * Adds the "param" helper to the view object
173
	 *
174
	 * @param \Aimeos\MW\View\Iface $view View object
175
	 * @return \Aimeos\MW\View\Iface Modified view object
176
	 */
177
	protected function addParam( \Aimeos\MW\View\Iface $view )
178
	{
179
		$params = ( Route::current() ? Route::current()->parameters() : array() ) + Request::all();
180
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
181
		$view->addHelper( 'param', $helper );
182
183
		return $view;
184
	}
185
186
187
	/**
188
	 * Adds the "request" helper to the view object
189
	 *
190
	 * @param \Aimeos\MW\View\Iface $view View object
191
	 * @return \Aimeos\MW\View\Iface Modified view object
192
	 */
193
	protected function addRequest( \Aimeos\MW\View\Iface $view )
194
	{
195
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
196
		$view->addHelper( 'request', $helper );
197
198
		return $view;
199
	}
200
201
202
	/**
203
	 * Adds the "response" helper to the view object
204
	 *
205
	 * @param \Aimeos\MW\View\Iface $view View object
206
	 * @return \Aimeos\MW\View\Iface Modified view object
207
	 */
208
	protected function addResponse( \Aimeos\MW\View\Iface $view )
209
	{
210
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
211
		$view->addHelper( 'response', $helper );
212
213
		return $view;
214
	}
215
216
217
	/**
218
	 * Adds the "session" helper to the view object
219
	 *
220
	 * @param \Aimeos\MW\View\Iface $view View object
221
	 * @param \Aimeos\MW\Session\Iface $session Session object
222
	 * @return \Aimeos\MW\View\Iface Modified view object
223
	 */
224
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
225
	{
226
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
227
		$view->addHelper( 'session', $helper );
228
229
		return $view;
230
	}
231
232
233
	/**
234
	 * Adds the "translate" helper to the view object
235
	 *
236
	 * @param \Aimeos\MW\View\Iface $view View object
237
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
238
	 * @return \Aimeos\MW\View\Iface Modified view object
239
	 */
240
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
241
	{
242
		if( $locale !== null )
243
		{
244
			$i18n = $this->i18n->get( array( $locale ) );
245
			$translation = $i18n[$locale];
246
		}
247
		else
248
		{
249
			$translation = new \Aimeos\MW\Translation\None( 'en' );
250
		}
251
252
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
253
		$view->addHelper( 'translate', $helper );
254
255
		return $view;
256
	}
257
258
259
	/**
260
	 * Adds the "url" helper to the view object
261
	 *
262
	 * @param \Aimeos\MW\View\Iface $view View object
263
	 * @return \Aimeos\MW\View\Iface Modified view object
264
	 */
265
	protected function addUrl( \Aimeos\MW\View\Iface $view )
266
	{
267
		$fixed = array();
268
269
		if( Route::current() )
270
		{
271
			if( ( $value = Route::input( 'site' ) ) !== null ) {
272
				$fixed['site'] = $value;
273
			}
274
275
			if( ( $value = Route::input( 'locale' ) ) !== null ) {
276
				$fixed['locale'] = $value;
277
			}
278
279
			if( ( $value = Route::input( 'currency' ) ) !== null ) {
280
				$fixed['currency'] = $value;
281
			}
282
		}
283
284
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app('url'), $fixed );
285
		$view->addHelper( 'url', $helper );
286
287
		return $view;
288
	}
289
}