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