1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @license MIT, http://opensource.org/licenses/MIT |
5
|
|
|
* @copyright Aimeos (aimeos.org), 2014-2016 |
6
|
|
|
* @package symfony |
7
|
|
|
* @subpackage Service |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Aimeos\ShopBundle\Service; |
11
|
|
|
|
12
|
|
|
use Symfony\Component\DependencyInjection\Container; |
13
|
|
|
|
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Service providing the context based objects |
17
|
|
|
* |
18
|
|
|
* @author Garret Watkins <[email protected]> |
19
|
|
|
* @package symfony |
20
|
|
|
* @subpackage Service |
21
|
|
|
*/ |
22
|
|
|
class Context |
23
|
|
|
{ |
24
|
|
|
private static $context; |
25
|
|
|
private $container; |
26
|
|
|
private $locale; |
|
|
|
|
27
|
|
|
|
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Initializes the context manager object |
31
|
|
|
* |
32
|
|
|
* @param Container $container Container object to access parameters |
33
|
|
|
*/ |
34
|
|
|
public function __construct( Container $container ) |
35
|
|
|
{ |
36
|
|
|
$this->container = $container; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Returns the current context. |
42
|
|
|
* |
43
|
|
|
* @param boolean $locale True to add locale object to context, false if not |
44
|
|
|
* @param string $type Configuration type ("frontend" or "backend") |
45
|
|
|
* @return \Aimeos\MShop\ContextIface Context object |
46
|
|
|
*/ |
47
|
|
|
public function get( $locale = true, $type = 'frontend' ) : \Aimeos\MShop\ContextIface |
48
|
|
|
{ |
49
|
|
|
$config = $this->container->get( 'aimeos.config' )->get( $type ); |
50
|
|
|
|
51
|
|
|
if( self::$context === null ) |
52
|
|
|
{ |
53
|
|
|
$context = new \Aimeos\MShop\Context(); |
54
|
|
|
$context->setConfig( $config ); |
55
|
|
|
|
56
|
|
|
$this->addDataBaseManager( $context ); |
57
|
|
|
$this->addFilesystemManager( $context ); |
58
|
|
|
$this->addMessageQueueManager( $context ); |
59
|
|
|
$this->addLogger( $context ); |
60
|
|
|
$this->addCache( $context ); |
61
|
|
|
$this->addMailer( $context ); |
62
|
|
|
$this->addNonce( $context ); |
63
|
|
|
$this->addProcess( $context ); |
64
|
|
|
$this->addPassword( $context ); |
65
|
|
|
|
66
|
|
|
self::$context = $context; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$context = self::$context; |
70
|
|
|
$context->setConfig( $config ); |
71
|
|
|
|
72
|
|
|
if( $locale === true ) |
73
|
|
|
{ |
74
|
|
|
$localeItem = $this->container->get( 'aimeos.locale' )->get( $context ); |
75
|
|
|
$context->setI18n( $this->container->get( 'aimeos.i18n' )->get( array( $localeItem->getLanguageId() ) ) ); |
76
|
|
|
$context->setLocale( $localeItem ); |
77
|
|
|
|
78
|
|
|
$config->apply( $localeItem->getSiteItem()->getConfig() ); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$this->addSession( $context ); |
82
|
|
|
$this->addUserGroups( $context ); |
83
|
|
|
$this->addToken( $context ); |
84
|
|
|
|
85
|
|
|
return $context; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Adds the cache object to the context |
91
|
|
|
* |
92
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object including config |
93
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
94
|
|
|
*/ |
95
|
|
|
protected function addCache( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
96
|
|
|
{ |
97
|
|
|
$cache = (new \Aimeos\MAdmin\Cache\Manager\Standard( $context ))->getCache(); |
98
|
|
|
|
99
|
|
|
return $context->setCache( $cache ); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Adds the database manager object to the context |
105
|
|
|
* |
106
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
107
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
108
|
|
|
*/ |
109
|
|
|
protected function addDatabaseManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
110
|
|
|
{ |
111
|
|
|
$dbm = new \Aimeos\Base\DB\Manager\Standard( $context->config()->get( 'resource', [] ), 'DBAL' ); |
112
|
|
|
|
113
|
|
|
return $context->setDatabaseManager( $dbm ); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Adds the filesystem manager object to the context |
119
|
|
|
* |
120
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
121
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
122
|
|
|
*/ |
123
|
|
|
protected function addFilesystemManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
124
|
|
|
{ |
125
|
|
|
$fs = new \Aimeos\Base\Filesystem\Manager\Standard( $context->config()->get( 'resource' ) ); |
126
|
|
|
|
127
|
|
|
return $context->setFilesystemManager( $fs ); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Adds the logger object to the context |
133
|
|
|
* |
134
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
135
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
136
|
|
|
*/ |
137
|
|
|
protected function addLogger( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
138
|
|
|
{ |
139
|
|
|
$logger = \Aimeos\MAdmin::create( $context, 'log' ); |
140
|
|
|
|
141
|
|
|
return $context->setLogger( $logger ); |
|
|
|
|
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
|
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Adds the mailer object to the context |
148
|
|
|
* |
149
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
150
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
151
|
|
|
*/ |
152
|
|
|
protected function addMailer( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
153
|
|
|
{ |
154
|
|
|
$container = $this->container; |
155
|
|
|
$mail = new \Aimeos\Base\Mail\Symfony( function() use ( $container ) { return $container->get( 'mailer' ); } ); |
156
|
|
|
|
157
|
|
|
return $context->setMail( $mail ); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
|
161
|
|
|
/** |
162
|
|
|
* Adds the message queue manager object to the context |
163
|
|
|
* |
164
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
165
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
166
|
|
|
*/ |
167
|
|
|
protected function addMessageQueueManager( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
168
|
|
|
{ |
169
|
|
|
$mq = new \Aimeos\Base\MQueue\Manager\Standard( $context->config()->get( 'resource', [] ) ); |
170
|
|
|
|
171
|
|
|
return $context->setMessageQueueManager( $mq ); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Adds the nonce value for inline JS to the context |
177
|
|
|
* |
178
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
179
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
180
|
|
|
*/ |
181
|
|
|
protected function addNonce( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
182
|
|
|
{ |
183
|
|
|
return $context->setNonce( base64_encode( random_bytes( 16 ) ) ); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Adds the password hasher object to the context |
189
|
|
|
* |
190
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
191
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
192
|
|
|
*/ |
193
|
|
|
protected function addPassword( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
194
|
|
|
{ |
195
|
|
|
return $context->setPassword( new \Aimeos\Base\Password\Standard() ); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
|
199
|
|
|
/** |
200
|
|
|
* Adds the process object to the context |
201
|
|
|
* |
202
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
203
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
204
|
|
|
*/ |
205
|
|
|
protected function addProcess( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
206
|
|
|
{ |
207
|
|
|
$config = $context->config(); |
208
|
|
|
$max = $config->get( 'pcntl_max', 4 ); |
209
|
|
|
$prio = $config->get( 'pcntl_priority', 19 ); |
210
|
|
|
|
211
|
|
|
$process = new \Aimeos\Base\Process\Pcntl( $max, $prio ); |
212
|
|
|
$process = new \Aimeos\Base\Process\Decorator\Check( $process ); |
213
|
|
|
|
214
|
|
|
return $context->setProcess( $process ); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Adds the session object to the context |
220
|
|
|
* |
221
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
222
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
223
|
|
|
*/ |
224
|
|
|
protected function addSession( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
225
|
|
|
{ |
226
|
|
|
$requestStack = $this->container->get( 'request_stack' ); |
227
|
|
|
|
228
|
|
|
if( $requestStack->getCurrentRequest() ) { |
229
|
|
|
$context->setSession( new \Aimeos\Base\Session\Symfony( $requestStack->getSession() ) ); |
230
|
|
|
} else { |
231
|
|
|
$context->setSession( new \Aimeos\Base\Session\None() ); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
return $context; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Adds the session token to the context |
240
|
|
|
* |
241
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
242
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
243
|
|
|
*/ |
244
|
|
|
protected function addToken( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
245
|
|
|
{ |
246
|
|
|
if( ( $token = $context->session()->get( 'token' ) ) === null ) |
247
|
|
|
{ |
248
|
|
|
$requestStack = $this->container->get( 'request_stack' ); |
249
|
|
|
|
250
|
|
|
if( $requestStack->getCurrentRequest() ) { |
251
|
|
|
$context->session()->set( 'token', $token = $requestStack->getSession()->getId() ); |
252
|
|
|
} |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
return $context->setToken( $token ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Adds the user ID and name if available |
261
|
|
|
* |
262
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
263
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
264
|
|
|
*/ |
265
|
|
|
protected function addUserGroups( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
266
|
|
|
{ |
267
|
|
|
$username = ''; |
268
|
|
|
|
269
|
|
|
if( $this->container->has( 'security.token_storage' ) |
270
|
|
|
&& ( $token = $this->container->get( 'security.token_storage' )->getToken() ) !== null |
271
|
|
|
) { |
272
|
|
|
$username = $token->getUser()->getUserIdentifier(); |
273
|
|
|
$userid = $token->getUser()->getId(); |
274
|
|
|
$context->setUserId( $userid ); |
275
|
|
|
$context->setGroupIds( function() use ( $context, $userid ) |
276
|
|
|
{ |
277
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer' ); |
278
|
|
|
return $manager->get( $userid, array( 'customer/group' ) )->getGroups(); |
279
|
|
|
} ); |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
if( $username === '' && $this->container->has( 'request_stack' ) |
283
|
|
|
&& ( $request = $this->container->get( 'request_stack' )->getCurrentRequest() ) !== null |
284
|
|
|
) { |
285
|
|
|
$username = $request->getClientIp(); |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
return $context->setEditor( $username ); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|