Completed
Push — master ( 74a52d...dd9c53 )
by Aimeos
15:35
created

View::addResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 4
nc 1
nop 1
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\Input;
14
use Illuminate\Support\Facades\Route;
15
use Illuminate\Support\Facades\Request;
16
use Illuminate\Support\Facades\Response;
17
18
19
/**
20
 * Service providing the view objects
21
 *
22
 * @package laravel
23
 * @subpackage Base
24
 */
25
class View
26
{
27
	/**
28
	 * @var \Aimeos\Shop\Base\I18n
29
	 */
30
	private $i18n;
31
32
	/**
33
	 * @var \Aimeos\Shop\Base\Support
34
	 */
35
	private $support;
36
37
38
	/**
39
	 * Initializes the object
40
	 *
41
	 * @param \Aimeos\Shop\Base\I18n $i18n I18n object
42
	 * @param \Aimeos\Shop\Base\Support $support Support object
43
	 */
44
	public function __construct( \Aimeos\Shop\Base\I18n $i18n, \Aimeos\Shop\Base\Support $support )
45
	{
46
		$this->i18n = $i18n;
47
		$this->support = $support;
48
	}
49
50
51
	/**
52
	 * Creates the view object for the HTML client.
53
	 *
54
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
55
	 * @param array $templatePaths List of base path names with relative template paths as key/value pairs
56
	 * @param string|null $locale Code of the current language or null for no translation
57
	 * @return \Aimeos\MW\View\Iface View object
58
	 */
59
	public function create( \Aimeos\MShop\Context\Item\Iface $context, array $templatePaths, $locale = null )
60
	{
61
		$engine = new \Aimeos\MW\View\Engine\Blade( app( 'Illuminate\Contracts\View\Factory' ) );
62
		$view = new \Aimeos\MW\View\Standard( $templatePaths, array( '.blade.php' => $engine ) );
63
64
		$config = $context->getConfig();
65
		$session = $context->getSession();
66
67
		$this->addCsrf( $view );
68
		$this->addAccess( $view, $context );
69
		$this->addConfig( $view, $config );
70
		$this->addNumber( $view, $config );
71
		$this->addParam( $view );
72
		$this->addRequest( $view );
73
		$this->addResponse( $view );
74
		$this->addSession( $view, $session );
75
		$this->addTranslate( $view, $locale );
76
		$this->addUrl( $view );
77
78
		return $view;
79
	}
80
81
82
	/**
83
	 * Adds the "access" helper to the view object
84
	 *
85
	 * @param \Aimeos\MW\View\Iface $view View object
86
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
87
	 * @return \Aimeos\MW\View\Iface Modified view object
88
	 */
89
	protected function addAccess( \Aimeos\MW\View\Iface $view, \Aimeos\MShop\Context\Item\Iface $context )
90
	{
91
		$support = $this->support;
92
93
		$fcn = function() use ( $support, $context ) {
94
			return $support->getGroups( $context );
95
		};
96
97
		$helper = new \Aimeos\MW\View\Helper\Access\Standard( $view, $fcn );
98
		$view->addHelper( 'access', $helper );
99
100
		return $view;
101
	}
102
103
104
	/**
105
	 * Adds the "config" helper to the view object
106
	 *
107
	 * @param \Aimeos\MW\View\Iface $view View object
108
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
109
	 * @return \Aimeos\MW\View\Iface Modified view object
110
	 */
111
	protected function addConfig( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
112
	{
113
		$config = new \Aimeos\MW\Config\Decorator\Protect( clone $config, array( 'admin', 'client' ) );
114
		$helper = new \Aimeos\MW\View\Helper\Config\Standard( $view, $config );
115
		$view->addHelper( 'config', $helper );
116
117
		return $view;
118
	}
119
120
121
	/**
122
	 * Adds the "access" helper to the view object
123
	 *
124
	 * @param \Aimeos\MW\View\Iface $view View object
125
	 * @return \Aimeos\MW\View\Iface Modified view object
126
	 */
127
	protected function addCsrf( \Aimeos\MW\View\Iface $view )
128
	{
129
		$helper = new \Aimeos\MW\View\Helper\Csrf\Standard( $view, '_token', csrf_token() );
130
		$view->addHelper( 'csrf', $helper );
131
132
		return $view;
133
	}
134
135
136
	/**
137
	 * Adds the "number" helper to the view object
138
	 *
139
	 * @param \Aimeos\MW\View\Iface $view View object
140
	 * @param \Aimeos\MW\Config\Iface $config Configuration object
141
	 * @return \Aimeos\MW\View\Iface Modified view object
142
	 */
143
	protected function addNumber( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Config\Iface $config )
144
	{
145
		$sepDec = $config->get( 'client/html/common/format/separatorDecimal', '.' );
146
		$sep1000 = $config->get( 'client/html/common/format/separator1000', ' ' );
147
		$decimals = $config->get( 'client/html/common/format/decimals', 2 );
148
149
		$helper = new \Aimeos\MW\View\Helper\Number\Standard( $view, $sepDec, $sep1000, $decimals );
150
		$view->addHelper( 'number', $helper );
151
152
		return $view;
153
	}
154
155
156
	/**
157
	 * Adds the "param" helper to the view object
158
	 *
159
	 * @param \Aimeos\MW\View\Iface $view View object
160
	 * @return \Aimeos\MW\View\Iface Modified view object
161
	 */
162
	protected function addParam( \Aimeos\MW\View\Iface $view )
163
	{
164
		$params = ( Route::current() ? Route::current()->parameters() : array() ) + Input::all();
165
		$helper = new \Aimeos\MW\View\Helper\Param\Standard( $view, $params );
166
		$view->addHelper( 'param', $helper );
167
168
		return $view;
169
	}
170
171
172
	/**
173
	 * Adds the "request" helper to the view object
174
	 *
175
	 * @param \Aimeos\MW\View\Iface $view View object
176
	 * @return \Aimeos\MW\View\Iface Modified view object
177
	 */
178
	protected function addRequest( \Aimeos\MW\View\Iface $view )
179
	{
180
		$helper = new \Aimeos\MW\View\Helper\Request\Laravel5( $view, Request::instance() );
0 ignored issues
show
Bug introduced by
The method instance() does not exist on Illuminate\Support\Facades\Request. Did you maybe mean clearResolvedInstance()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
181
		$view->addHelper( 'request', $helper );
182
183
		return $view;
184
	}
185
186
187
	/**
188
	 * Adds the "response" 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 addResponse( \Aimeos\MW\View\Iface $view )
194
	{
195
		$helper = new \Aimeos\MW\View\Helper\Response\Laravel5( $view );
196
		$view->addHelper( 'response', $helper );
197
198
		return $view;
199
	}
200
201
202
	/**
203
	 * Adds the "session" helper to the view object
204
	 *
205
	 * @param \Aimeos\MW\View\Iface $view View object
206
	 * @param \Aimeos\MW\Session\Iface $session Session object
207
	 * @return \Aimeos\MW\View\Iface Modified view object
208
	 */
209
	protected function addSession( \Aimeos\MW\View\Iface $view, \Aimeos\MW\Session\Iface $session )
210
	{
211
		$helper = new \Aimeos\MW\View\Helper\Session\Standard( $view, $session );
212
		$view->addHelper( 'session', $helper );
213
214
		return $view;
215
	}
216
217
218
	/**
219
	 * Adds the "translate" helper to the view object
220
	 *
221
	 * @param \Aimeos\MW\View\Iface $view View object
222
	 * @param string|null $locale ISO language code, e.g. "de" or "de_CH"
223
	 * @return \Aimeos\MW\View\Iface Modified view object
224
	 */
225
	protected function addTranslate( \Aimeos\MW\View\Iface $view, $locale )
226
	{
227
		if( $locale !== null )
228
		{
229
			$i18n = $this->i18n->get( array( $locale ) );
230
			$translation = $i18n[$locale];
231
		}
232
		else
233
		{
234
			$translation = new \Aimeos\MW\Translation\None( 'en' );
235
		}
236
237
		$helper = new \Aimeos\MW\View\Helper\Translate\Standard( $view, $translation );
238
		$view->addHelper( 'translate', $helper );
239
240
		return $view;
241
	}
242
243
244
	/**
245
	 * Adds the "url" helper to the view object
246
	 *
247
	 * @param \Aimeos\MW\View\Iface $view View object
248
	 * @return \Aimeos\MW\View\Iface Modified view object
249
	 */
250
	protected function addUrl( \Aimeos\MW\View\Iface $view )
251
	{
252
		$fixed = array();
253
254
		if( Route::current() )
255
		{
256
			if( ( $value = Route::input( 'site' ) ) !== null ) {
257
				$fixed['site'] = $value;
258
			}
259
260
			if( ( $value = Route::input( 'locale' ) ) !== null ) {
261
				$fixed['locale'] = $value;
262
			}
263
264
			if( ( $value = Route::input( 'currency' ) ) !== null ) {
265
				$fixed['currency'] = $value;
266
			}
267
		}
268
269
		$helper = new \Aimeos\MW\View\Helper\Url\Laravel5( $view, app('url'), $fixed );
270
		$view->addHelper( 'url', $helper );
271
272
		return $view;
273
	}
274
}