|
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\Context\Item\Iface 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\Item\Standard(); |
|
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
|
|
|
|
|
63
|
|
|
self::$context = $context; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
$context = self::$context; |
|
67
|
|
|
$context->setConfig( $config ); |
|
68
|
|
|
|
|
69
|
|
|
if( $locale === true ) |
|
70
|
|
|
{ |
|
71
|
|
|
$localeItem = $this->container->get('aimeos_locale')->get( $context ); |
|
72
|
|
|
$context->setI18n( $this->container->get('aimeos_i18n')->get( array( $localeItem->getLanguageId() ) ) ); |
|
73
|
|
|
$context->setLocale( $localeItem ); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$this->addSession( $context ); |
|
77
|
|
|
$this->addUserGroups( $context); |
|
78
|
|
|
|
|
79
|
|
|
return $context; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Adds the cache object to the context |
|
85
|
|
|
* |
|
86
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object including config |
|
87
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Modified context object |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function addCache( \Aimeos\MShop\Context\Item\Iface $context ) |
|
90
|
|
|
{ |
|
91
|
|
|
$cache = new \Aimeos\MAdmin\Cache\Proxy\Standard( $context ); |
|
92
|
|
|
$context->setCache( $cache ); |
|
93
|
|
|
|
|
94
|
|
|
return $context; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Adds the database manager object to the context |
|
100
|
|
|
* |
|
101
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
102
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Modified context object |
|
103
|
|
|
*/ |
|
104
|
|
|
protected function addDatabaseManager( \Aimeos\MShop\Context\Item\Iface $context ) |
|
105
|
|
|
{ |
|
106
|
|
|
$dbm = new \Aimeos\MW\DB\Manager\DBAL( $context->getConfig() ); |
|
107
|
|
|
$context->setDatabaseManager( $dbm ); |
|
108
|
|
|
|
|
109
|
|
|
return $context; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Adds the filesystem manager object to the context |
|
115
|
|
|
* |
|
116
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
117
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Modified context object |
|
118
|
|
|
*/ |
|
119
|
|
|
protected function addFilesystemManager( \Aimeos\MShop\Context\Item\Iface $context ) |
|
120
|
|
|
{ |
|
121
|
|
|
$fs = new \Aimeos\MW\Filesystem\Manager\Standard( $context->getConfig() ); |
|
122
|
|
|
$context->setFilesystemManager( $fs ); |
|
123
|
|
|
|
|
124
|
|
|
return $context; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* Adds the logger object to the context |
|
130
|
|
|
* |
|
131
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
132
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Modified context object |
|
133
|
|
|
*/ |
|
134
|
|
|
protected function addLogger( \Aimeos\MShop\Context\Item\Iface $context ) |
|
135
|
|
|
{ |
|
136
|
|
|
$logger = \Aimeos\MAdmin\Log\Manager\Factory::createManager( $context ); |
|
137
|
|
|
$context->setLogger( $logger ); |
|
138
|
|
|
|
|
139
|
|
|
return $context; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* Adds the mailer object to the context |
|
146
|
|
|
* |
|
147
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
148
|
|
|
* @return \Aimeos\MShop\Context\Item\Iface Modified context object |
|
149
|
|
|
*/ |
|
150
|
|
|
protected function addMailer( \Aimeos\MShop\Context\Item\Iface $context ) |
|
151
|
|
|
{ |
|
152
|
|
|
$container = $this->container; |
|
153
|
|
|
$mail = new \Aimeos\MW\Mail\Swift( function() use ( $container) { return $container->get( 'mailer' ); } ); |
|
154
|
|
|
$context->setMail( $mail ); |
|
155
|
|
|
|
|
156
|
|
|
return $context; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
|
|
160
|
|
|
/** |
|
161
|
|
|
* Adds the message queue manager 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 addMessageQueueManager( \Aimeos\MShop\Context\Item\Iface $context ) |
|
167
|
|
|
{ |
|
168
|
|
|
$mq = new \Aimeos\MW\MQueue\Manager\Standard( $context->getConfig() ); |
|
169
|
|
|
$context->setMessageQueueManager( $mq ); |
|
170
|
|
|
|
|
171
|
|
|
return $context; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Adds the session 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 addSession( \Aimeos\MShop\Context\Item\Iface $context ) |
|
182
|
|
|
{ |
|
183
|
|
|
$session = new \Aimeos\MW\Session\Symfony2( $this->container->get( 'session' ) ); |
|
184
|
|
|
$context->setSession( $session ); |
|
185
|
|
|
|
|
186
|
|
|
return $context; |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Adds the user ID and name if available |
|
192
|
|
|
* |
|
193
|
|
|
* @param \Aimeos\MShop\Context\Item\Iface $context Context object |
|
194
|
|
|
*/ |
|
195
|
|
|
protected function addUserGroups( \Aimeos\MShop\Context\Item\Iface $context ) |
|
196
|
|
|
{ |
|
197
|
|
|
$token = null; |
|
198
|
|
|
$username = ''; |
|
199
|
|
|
|
|
200
|
|
|
if( $this->container->has( 'security.token_storage' ) ) { |
|
201
|
|
|
$token = $this->container->get( 'security.token_storage' )->getToken(); |
|
202
|
|
|
} |
|
203
|
|
|
else if( $this->container->has( 'security.context' ) ) { |
|
204
|
|
|
$token = $this->container->get( 'security.context' )->getToken(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
if( is_object( $token ) ) |
|
208
|
|
|
{ |
|
209
|
|
|
if( method_exists( $token->getUser(), 'getId' ) ) |
|
210
|
|
|
{ |
|
211
|
|
|
$userid = $token->getUser()->getId(); |
|
212
|
|
|
$context->setUserId( $userid ); |
|
213
|
|
|
$context->setGroupIds( function() use ( $context, $userid ) |
|
214
|
|
|
{ |
|
215
|
|
|
$manager = \Aimeos\MShop\Factory::createManager( $context, 'customer' ); |
|
216
|
|
|
return $manager->getItem( $userid, array( 'customer/group' ) )->getGroups(); |
|
|
|
|
|
|
217
|
|
|
} ); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if( is_object( $token->getUser() ) ) { |
|
221
|
|
|
$username = $token->getUser()->getUsername(); |
|
222
|
|
|
} else { |
|
223
|
|
|
$username = $token->getUser(); |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$context->setEditor( $username ); |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.