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