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' ) |
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
|
|
|
|
84
|
|
|
return $context; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds the cache object to the context |
90
|
|
|
* |
91
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object including config |
92
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
93
|
|
|
*/ |
94
|
|
|
protected function addCache( \Aimeos\MShop\ContextIface $context ) |
95
|
|
|
{ |
96
|
|
|
$cache = (new \Aimeos\MAdmin\Cache\Manager\Standard( $context ))->getCache(); |
97
|
|
|
|
98
|
|
|
return $context->setCache( $cache ); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Adds the database manager object to the context |
104
|
|
|
* |
105
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
106
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
107
|
|
|
*/ |
108
|
|
|
protected function addDatabaseManager( \Aimeos\MShop\ContextIface $context ) |
109
|
|
|
{ |
110
|
|
|
$dbm = new \Aimeos\Base\DB\Manager\Standard( $context->config()->get( 'resource', [] ), 'DBAL' ); |
|
|
|
|
111
|
|
|
|
112
|
|
|
return $context->setDatabaseManager( $dbm ); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Adds the filesystem manager object to the context |
118
|
|
|
* |
119
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
120
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
121
|
|
|
*/ |
122
|
|
|
protected function addFilesystemManager( \Aimeos\MShop\ContextIface $context ) |
123
|
|
|
{ |
124
|
|
|
$fs = new \Aimeos\Base\Filesystem\Manager\Standard( $context->config()->get( 'resource' ) ); |
|
|
|
|
125
|
|
|
|
126
|
|
|
return $context->setFilesystemManager( $fs ); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Adds the logger object to the context |
132
|
|
|
* |
133
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
134
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
135
|
|
|
*/ |
136
|
|
|
protected function addLogger( \Aimeos\MShop\ContextIface $context ) |
137
|
|
|
{ |
138
|
|
|
$logger = \Aimeos\MAdmin::create( $context, 'log' ); |
139
|
|
|
|
140
|
|
|
return $context->setLogger( $logger ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
|
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Adds the mailer object to the context |
147
|
|
|
* |
148
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
149
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
150
|
|
|
*/ |
151
|
|
|
protected function addMailer( \Aimeos\MShop\ContextIface $context ) |
152
|
|
|
{ |
153
|
|
|
$container = $this->container; |
154
|
|
|
$mail = new \Aimeos\Base\Mail\Symfony( function() use ( $container ) { return $container->get( 'mailer' ); } ); |
|
|
|
|
155
|
|
|
|
156
|
|
|
return $context->setMail( $mail ); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Adds the message queue manager object to the context |
162
|
|
|
* |
163
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
164
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
165
|
|
|
*/ |
166
|
|
|
protected function addMessageQueueManager( \Aimeos\MShop\ContextIface $context ) |
167
|
|
|
{ |
168
|
|
|
$mq = new \Aimeos\Base\MQueue\Manager\Standard( $context->config()->get( 'resource', [] ) ); |
|
|
|
|
169
|
|
|
|
170
|
|
|
return $context->setMessageQueueManager( $mq ); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Adds the nonce value for inline JS to the context |
176
|
|
|
* |
177
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
178
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
179
|
|
|
*/ |
180
|
|
|
protected function addNonce( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
181
|
|
|
{ |
182
|
|
|
return $context->setNonce( base64_encode( random_bytes( 16 ) ) ); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Adds the password hasher object to the context |
188
|
|
|
* |
189
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
190
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
191
|
|
|
*/ |
192
|
|
|
protected function addPassword( \Aimeos\MShop\ContextIface $context ) : \Aimeos\MShop\ContextIface |
193
|
|
|
{ |
194
|
|
|
return $context->setPassword( new \Aimeos\Base\Password\Standard() ); |
|
|
|
|
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Adds the process object to the context |
200
|
|
|
* |
201
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
202
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
203
|
|
|
*/ |
204
|
|
|
protected function addProcess( \Aimeos\MShop\ContextIface $context ) |
205
|
|
|
{ |
206
|
|
|
$config = $context->config(); |
207
|
|
|
$max = $config->get( 'pcntl_max', 4 ); |
208
|
|
|
$prio = $config->get( 'pcntl_priority', 19 ); |
209
|
|
|
|
210
|
|
|
$process = new \Aimeos\Base\Process\Pcntl( $max, $prio ); |
|
|
|
|
211
|
|
|
$process = new \Aimeos\Base\Process\Decorator\Check( $process ); |
|
|
|
|
212
|
|
|
|
213
|
|
|
return $context->setProcess( $process ); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Adds the session object to the context |
219
|
|
|
* |
220
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
221
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
222
|
|
|
*/ |
223
|
|
|
protected function addSession( \Aimeos\MShop\ContextIface $context ) |
224
|
|
|
{ |
225
|
|
|
$requestStack = $this->container->get( 'request_stack' ); |
226
|
|
|
|
227
|
|
|
if( $requestStack->getCurrentRequest() ) { |
228
|
|
|
$context->setSession( new \Aimeos\Base\Session\Symfony( $requestStack->getSession() ) ); |
|
|
|
|
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
return $context; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* Adds the user ID and name if available |
237
|
|
|
* |
238
|
|
|
* @param \Aimeos\MShop\ContextIface $context Context object |
239
|
|
|
* @return \Aimeos\MShop\ContextIface Modified context object |
240
|
|
|
*/ |
241
|
|
|
protected function addUserGroups( \Aimeos\MShop\ContextIface $context ) |
242
|
|
|
{ |
243
|
|
|
$username = ''; |
244
|
|
|
|
245
|
|
|
if( $this->container->has( 'security.token_storage' ) |
246
|
|
|
&& ( $token = $this->container->get( 'security.token_storage' )->getToken() ) !== null |
247
|
|
|
) { |
248
|
|
|
$username = $token->getUser()->getUserIdentifier(); |
249
|
|
|
$userid = $token->getUser()->getId(); |
250
|
|
|
$context->setUserId( $userid ); |
251
|
|
|
$context->setGroupIds( function() use ( $context, $userid ) |
252
|
|
|
{ |
253
|
|
|
$manager = \Aimeos\MShop::create( $context, 'customer' ); |
254
|
|
|
return $manager->get( $userid, array( 'customer/group' ) )->getGroups(); |
255
|
|
|
} ); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
if( $username === '' && $this->container->has( 'request_stack' ) |
259
|
|
|
&& ( $request = $this->container->get( 'request_stack' )->getMainRequest() ) !== null |
260
|
|
|
) { |
261
|
|
|
$username = $request->getClientIp(); |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
return $context->setEditor( $username ); |
265
|
|
|
} |
266
|
|
|
} |
267
|
|
|
|