Issues (67)

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

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\Auth;
14
use Illuminate\Support\Facades\Route;
15
16
17
/**
18
 * Service providing the context objects
19
 *
20
 * @package laravel
21
 * @subpackage Base
22
 */
23
class Context
24
{
25
	/**
26
	 * @var \Aimeos\MShop\Context\Item\Iface
27
	 */
28
	private $context;
29
30
	/**
31
	 * @var \Aimeos\Shop\Base\Config
32
	 */
33
	private $config;
34
35
	/**
36
	 * @var \Aimeos\Shop\Base\I18n
37
	 */
38
	private $i18n;
39
40
	/**
41
	 * @var \Aimeos\Shop\Base\Locale
42
	 */
43
	private $locale;
44
45
	/**
46
	 * @var \Illuminate\Session\Store
47
	 */
48
	private $session;
49
50
51
	/**
52
	 * Initializes the object
53
	 *
54
	 * @param \Illuminate\Session\Store $session Laravel session object
55
	 * @param \Aimeos\Shop\Base\Config $config Configuration object
56
	 * @param \Aimeos\Shop\Base\Locale $locale Locale object
57
	 * @param \Aimeos\Shop\Base\I18n $i18n Internationalisation object
58
	 */
59
	public function __construct( \Illuminate\Session\Store $session, \Aimeos\Shop\Base\Config $config, \Aimeos\Shop\Base\Locale $locale, \Aimeos\Shop\Base\I18n $i18n )
60
	{
61
		$this->session = $session;
62
		$this->config = $config;
63
		$this->locale = $locale;
64
		$this->i18n = $i18n;
65
	}
66
67
68
	/**
69
	 * Returns the current context
70
	 *
71
	 * @param bool $locale True to add locale object to context, false if not (deprecated, use \Aimeos\Shop\Base\Locale)
72
	 * @param string $type Configuration type, i.e. "frontend" or "backend" (deprecated, use \Aimeos\Shop\Base\Config)
73
	 * @return \Aimeos\MShop\Context\Item\Iface Context object
74
	 */
75
	public function get( bool $locale = true, string $type = 'frontend' ) : \Aimeos\MShop\Context\Item\Iface
76
	{
77
		$config = $this->config->get( $type );
78
79
		if( $this->context === null )
80
		{
81
			$context = new \Aimeos\MShop\Context\Item\Standard();
82
			$context->setConfig( $config );
83
84
			$this->addDataBaseManager( $context );
85
			$this->addFilesystemManager( $context );
86
			$this->addMessageQueueManager( $context );
87
			$this->addLogger( $context );
88
			$this->addCache( $context );
89
			$this->addMailer( $context );
90
			$this->addProcess( $context );
91
			$this->addSession( $context );
92
			$this->addUser( $context );
93
			$this->addGroups( $context );
94
95
			$this->context = $context;
96
		}
97
98
		$this->context->setConfig( $config );
99
100
		if( $locale === true )
101
		{
102
			$localeItem = $this->locale->get( $this->context );
103
			$this->context->setLocale( $localeItem );
104
			$this->context->setI18n( $this->i18n->get( array( $localeItem->getLanguageId() ) ) );
105
106
			foreach( $localeItem->getSiteItem()->getConfig() as $key => $value ) {
107
				$config->set( $key, $value );
108
			}
109
		}
110
111
		return $this->context;
112
	}
113
114
115
	/**
116
	 * Adds the cache object to the context
117
	 *
118
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object including config
119
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
120
	 */
121
	protected function addCache( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
122
	{
123
		$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context );
124
125
		return $context->setCache( $cache );
126
	}
127
128
129
	/**
130
	 * Adds the database manager object to the context
131
	 *
132
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
133
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
134
	 */
135
	protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
136
	{
137
		$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() );
138
139
		return $context->setDatabaseManager( $dbm );
140
	}
141
142
143
	/**
144
	 * Adds the filesystem manager object to the context
145
	 *
146
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
147
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
148
	 */
149
	protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
150
	{
151
		$config = $context->getConfig();
152
		$path = storage_path( 'aimeos' );
153
154
		$fs = new \Aimeos\MW\Filesystem\Manager\Laravel( app( 'filesystem' ), $config, $path );
155
156
		return $context->setFilesystemManager( $fs );
157
	}
158
159
160
	/**
161
	 * Adds the logger object to the context
162
	 *
163
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
164
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
165
	 */
166
	protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
167
	{
168
		$logger = \Aimeos\MAdmin::create( $context, 'log' );
169
170
		return $context->setLogger( $logger );
171
	}
172
173
174
175
	/**
176
	 * Adds the mailer object to the context
177
	 *
178
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
179
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
180
	 */
181
	protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
182
	{
183
		$mail = new \Aimeos\MW\Mail\Swift( function() { return app( 'mailer' )->getSwiftMailer(); } );
184
185
		return $context->setMail( $mail );
186
	}
187
188
189
	/**
190
	 * Adds the message queue manager object to the context
191
	 *
192
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
193
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
194
	 */
195
	protected function addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
196
	{
197
		$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() );
198
199
		return $context->setMessageQueueManager( $mq );
200
	}
201
202
203
	/**
204
	 * Adds the process object to the context
205
	 *
206
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
207
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
208
	 */
209
	protected function addProcess( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
210
	{
211
		$config = $context->getConfig();
212
		$max = $config->get( 'pcntl_max', 4 );
213
		$prio = $config->get( 'pcntl_priority', 19 );
214
215
		$process = new \Aimeos\MW\Process\Pcntl( $max, $prio );
216
		$process = new \Aimeos\MW\Process\Decorator\Check( $process );
217
218
		return $context->setProcess( $process );
219
	}
220
221
222
	/**
223
	 * Adds the session object to the context
224
	 *
225
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
226
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
227
	 */
228
	protected function addSession( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
229
	{
230
		$session = new \Aimeos\MW\Session\Laravel5( $this->session );
231
232
		return $context->setSession( $session );
233
	}
234
235
236
	/**
237
	 * Adds the user ID and name if available
238
	 *
239
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
240
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
241
	 */
242
	protected function addUser( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
243
	{
244
		$key = collect( config( 'shop.routes' ) )->where( 'prefix', optional( Route::getCurrentRoute() )->getPrefix() )->keys()->first();
245
		$guard = data_get( config( 'shop.guards' ), $key, Auth::getDefaultDriver() );
246
247
		if( ( $userid = Auth::guard( $guard )->id() ) !== null ) {
248
			$context->setUserId( $userid );
249
		}
250
251
		if( ( $user = Auth::guard( $guard )->user() ) !== null ) {
252
			$context->setEditor( $user->name );
0 ignored issues
show
Accessing name on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
253
		} else {
254
			$context->setEditor( \Request::ip() );
255
		}
256
257
		return $context;
258
	}
259
260
261
	/**
262
	 * Adds the group IDs if available
263
	 *
264
	 * @param \Aimeos\MShop\Context\Item\Iface $context Context object
265
	 * @return \Aimeos\MShop\Context\Item\Iface Modified context object
266
	 */
267
	protected function addGroups( \Aimeos\MShop\Context\Item\Iface $context ) : \Aimeos\MShop\Context\Item\Iface
268
	{
269
		if( ( $userid = Auth::id() ) !== null )
270
		{
271
			$context->setGroupIds( function() use ( $context, $userid )
272
			{
273
				$manager = \Aimeos\MShop::create( $context, 'customer' );
274
				return $manager->get( $userid, array( 'customer/group' ) )->getGroups();
275
			} );
276
		}
277
278
		return $context;
279
	}
280
}
281