1 | <?php |
||
2 | |||
3 | /** |
||
4 | * @license LGPLv3, http://opensource.org/licenses/LGPL-3.0 |
||
5 | * @copyright Metaways Infosystems GmbH, 2012 |
||
6 | * @copyright Aimeos (aimeos.org), 2015-2025 |
||
7 | * @package Client |
||
8 | * @subpackage Html |
||
9 | */ |
||
10 | |||
11 | |||
12 | namespace Aimeos\Client\Html; |
||
13 | |||
14 | |||
15 | /** |
||
16 | * Common abstract class for all HTML client classes. |
||
17 | * |
||
18 | * @package Client |
||
19 | * @subpackage Html |
||
20 | */ |
||
21 | abstract class Base |
||
22 | implements \Aimeos\Client\Html\Iface, \Aimeos\Macro\Iface |
||
23 | { |
||
24 | use \Aimeos\Macro\Macroable; |
||
25 | |||
26 | |||
27 | private \Aimeos\MShop\ContextIface $context; |
||
28 | private ?\Aimeos\Base\View\Iface $view = null; |
||
29 | private ?\Aimeos\Client\Html\Iface $object = null; |
||
30 | private ?\Aimeos\Base\View\Iface $cachedView = null; |
||
31 | private ?array $subclients = null; |
||
32 | private array $cache = []; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * Initializes the class instance. |
||
37 | * |
||
38 | * @param \Aimeos\MShop\ContextIface $context Context object |
||
39 | */ |
||
40 | public function __construct( \Aimeos\MShop\ContextIface $context ) |
||
41 | { |
||
42 | $this->context = $context; |
||
43 | } |
||
44 | |||
45 | |||
46 | /** |
||
47 | * Returns the HTML code for insertion into the body. |
||
48 | * |
||
49 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
50 | * @return string HTML code |
||
51 | */ |
||
52 | public function body( string $uid = '' ) : string |
||
53 | { |
||
54 | $html = ''; |
||
55 | $type = $this->clientType(); |
||
56 | |||
57 | $parts = explode( '/', $type ); |
||
58 | $list = array_merge( $parts, ['body'] ); |
||
59 | |||
60 | $template = join( '/', array_splice( $list, 0, 2, [] ) ) . '/' . join( '-', $list ); |
||
61 | |||
62 | // populate view only once |
||
63 | if( count( $parts ) === 2 ) { |
||
64 | $view = $this->cachedView = $this->cachedView ?? $this->object()->data( $this->view() ); |
||
65 | } else { |
||
66 | $view = $this->view(); |
||
67 | } |
||
68 | |||
69 | foreach( $this->getSubClients() as $subclient ) { |
||
70 | $html .= $subclient->setView( $view )->body( $uid ); |
||
71 | } |
||
72 | |||
73 | return $view->set( 'body', $html ) |
||
74 | ->render( $view->config( "client/html/{$type}/template-body", $template ) ); |
||
75 | } |
||
76 | |||
77 | |||
78 | /** |
||
79 | * Adds the data to the view object required by the templates |
||
80 | * |
||
81 | * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output |
||
82 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
83 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
84 | * @return \Aimeos\Base\View\Iface The view object with the data required by the templates |
||
85 | */ |
||
86 | public function data( \Aimeos\Base\View\Iface $view, array &$tags = [], ?string &$expire = null ) : \Aimeos\Base\View\Iface |
||
87 | { |
||
88 | foreach( $this->getSubClients() as $name => $subclient ) { |
||
89 | $view = $subclient->data( $view, $tags, $expire ); |
||
90 | } |
||
91 | |||
92 | return $view; |
||
93 | } |
||
94 | |||
95 | |||
96 | /** |
||
97 | * Returns the sub-client given by its name. |
||
98 | * |
||
99 | * @param string $type Name of the client type |
||
100 | * @param string|null $name Name of the sub-client (Default if null) |
||
101 | * @return \Aimeos\Client\Html\Iface Sub-client object |
||
102 | */ |
||
103 | public function getSubClient( string $type, ?string $name = null ) : \Aimeos\Client\Html\Iface |
||
104 | { |
||
105 | return $this->createSubClient( $this->clientType() . '/' . $type, $name ); |
||
106 | } |
||
107 | |||
108 | |||
109 | /** |
||
110 | * Returns the HTML string for insertion into the header. |
||
111 | * |
||
112 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
113 | * @return string|null String including HTML tags for the header on error |
||
114 | */ |
||
115 | public function header( string $uid = '' ) : ?string |
||
116 | { |
||
117 | $type = $this->clientType(); |
||
118 | $view = $this->cachedView = $this->cachedView ?? $this->object()->data( $this->view() ); |
||
119 | |||
120 | return $view->render( $view->config( "client/html/{$type}/template-header", $type . '/header' ) ); |
||
121 | } |
||
122 | |||
123 | |||
124 | /** |
||
125 | * Processes the input, e.g. store given values. |
||
126 | * |
||
127 | * A view must be available and this method doesn't generate any output |
||
128 | * besides setting view variables. |
||
129 | */ |
||
130 | public function init() |
||
131 | { |
||
132 | $view = $this->view(); |
||
133 | |||
134 | foreach( $this->getSubClients() as $subclient ) { |
||
135 | $subclient->setView( $view )->init(); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | |||
140 | /** |
||
141 | * Modifies the cached content to replace content based on sessions or cookies. |
||
142 | * |
||
143 | * @param string $content Cached content |
||
144 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
145 | * @return string Modified content |
||
146 | */ |
||
147 | public function modify( string $content, string $uid ) : string |
||
148 | { |
||
149 | $view = $this->view(); |
||
150 | |||
151 | foreach( $this->getSubClients() as $subclient ) |
||
152 | { |
||
153 | $subclient->setView( $view ); |
||
154 | $content = $subclient->modify( $content, $uid ); |
||
155 | } |
||
156 | |||
157 | return $content; |
||
158 | } |
||
159 | |||
160 | |||
161 | /** |
||
162 | * Returns the PSR-7 response object for the request |
||
163 | * |
||
164 | * @return \Psr\Http\Message\ResponseInterface Response object |
||
165 | */ |
||
166 | public function response() : \Psr\Http\Message\ResponseInterface |
||
167 | { |
||
168 | return $this->view()->response(); |
||
169 | } |
||
170 | |||
171 | |||
172 | /** |
||
173 | * Injects the reference of the outmost client object or decorator |
||
174 | * |
||
175 | * @param \Aimeos\Client\Html\Iface $object Reference to the outmost client or decorator |
||
176 | * @return \Aimeos\Client\Html\Iface Client object for chaining method calls |
||
177 | */ |
||
178 | public function setObject( \Aimeos\Client\Html\Iface $object ) : \Aimeos\Client\Html\Iface |
||
179 | { |
||
180 | $this->object = $object; |
||
181 | return $this; |
||
182 | } |
||
183 | |||
184 | |||
185 | /** |
||
186 | * Sets the view object that will generate the HTML output. |
||
187 | * |
||
188 | * @param \Aimeos\Base\View\Iface $view The view object which generates the HTML output |
||
189 | * @return \Aimeos\Client\Html\Iface Reference to this object for fluent calls |
||
190 | */ |
||
191 | public function setView( \Aimeos\Base\View\Iface $view ) : \Aimeos\Client\Html\Iface |
||
192 | { |
||
193 | $this->view = $view; |
||
194 | return $this; |
||
195 | } |
||
196 | |||
197 | |||
198 | /** |
||
199 | * Returns the outmost decorator of the decorator stack |
||
200 | * |
||
201 | * @return \Aimeos\Client\Html\Iface Outmost decorator object |
||
202 | */ |
||
203 | protected function object() : \Aimeos\Client\Html\Iface |
||
204 | { |
||
205 | if( $this->object !== null ) { |
||
206 | return $this->object; |
||
207 | } |
||
208 | |||
209 | return $this; |
||
210 | } |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Returns the view object that will generate the HTML output. |
||
215 | * |
||
216 | * @return \Aimeos\Base\View\Iface $view The view object which generates the HTML output |
||
217 | */ |
||
218 | protected function view() : \Aimeos\Base\View\Iface |
||
219 | { |
||
220 | if( !isset( $this->view ) ) { |
||
221 | throw new \Aimeos\Client\Html\Exception( sprintf( 'No view available' ) ); |
||
222 | } |
||
223 | |||
224 | return $this->view; |
||
225 | } |
||
226 | |||
227 | |||
228 | /** |
||
229 | * Adds the decorators to the client object |
||
230 | * |
||
231 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
232 | * @param array $decorators List of decorator name that should be wrapped around the client |
||
233 | * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Client\Html\Catalog\Decorator\" |
||
234 | * @return \Aimeos\Client\Html\Iface Client object |
||
235 | * @throws \LogicException If class can't be instantiated |
||
236 | */ |
||
237 | protected function addDecorators( \Aimeos\Client\Html\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Client\Html\Iface |
||
238 | { |
||
239 | $interface = \Aimeos\Client\Html\Common\Decorator\Iface::class; |
||
240 | |||
241 | foreach( $decorators as $name ) |
||
242 | { |
||
243 | if( ctype_alnum( $name ) === false ) |
||
244 | { |
||
245 | $classname = is_string( $name ) ? $classprefix . $name : '<not a string>'; |
||
246 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) ); |
||
247 | } |
||
248 | |||
249 | $classname = $classprefix . $name; |
||
250 | $client = \Aimeos\Utils::create( $classname, [$client, $this->context], $interface ); |
||
251 | } |
||
252 | |||
253 | return $client; |
||
254 | } |
||
255 | |||
256 | |||
257 | /** |
||
258 | * Adds the decorators to the client object |
||
259 | * |
||
260 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
261 | * @param string $path Client string in lower case, e.g. "catalog/detail/basic" |
||
262 | * @return \Aimeos\Client\Html\Iface Client object |
||
263 | */ |
||
264 | protected function addClientDecorators( \Aimeos\Client\Html\Iface $client, string $path ) : \Aimeos\Client\Html\Iface |
||
265 | { |
||
266 | if( !is_string( $path ) || $path === '' ) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
267 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid domain "%1$s"', $path ) ); |
||
268 | } |
||
269 | |||
270 | $localClass = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
||
271 | $config = $this->context->config(); |
||
272 | |||
273 | $classprefix = '\\Aimeos\\Client\\Html\\Common\\Decorator\\'; |
||
274 | $decorators = $config->get( 'client/html/' . $path . '/decorators/global', [] ); |
||
275 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
276 | |||
277 | $classprefix = '\\Aimeos\\Client\\Html\\' . $localClass . '\\Decorator\\'; |
||
278 | $decorators = $config->get( 'client/html/' . $path . '/decorators/local', [] ); |
||
279 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
280 | |||
281 | return $client; |
||
282 | } |
||
283 | |||
284 | |||
285 | /** |
||
286 | * Adds the cache tags to the given list and sets a new expiration date if necessary based on the given item. |
||
287 | * |
||
288 | * @param \Aimeos\MShop\Common\Item\Iface|iterable $items Item or list of items, maybe with associated list items |
||
289 | * @param string|null &$expire Expiration date that will be overwritten if an earlier date is found |
||
290 | * @param array &$tags List of tags the new tags will be added to |
||
291 | * @param array $custom List of custom tags which are added too |
||
292 | */ |
||
293 | protected function addMetaItems( $items, ?string &$expire, array &$tags, array $custom = [] ) |
||
294 | { |
||
295 | /** client/html/common/cache/tag-all |
||
296 | * Adds tags for all items used in a cache entry |
||
297 | * |
||
298 | * Each cache entry storing rendered parts for the HTML header or body |
||
299 | * can be tagged with information which items like texts, media, etc. |
||
300 | * are used in the HTML. This allows removing only those cache entries |
||
301 | * whose content has really changed and only that entries have to be |
||
302 | * rebuild the next time. |
||
303 | * |
||
304 | * The standard behavior stores only tags for each used domain, e.g. if |
||
305 | * a text is used, only the tag "text" is added. If you change a text |
||
306 | * in the administration interface, all cache entries with the tag |
||
307 | * "text" will be removed from the cache. This effectively wipes out |
||
308 | * almost all cached entries, which have to be rebuild with the next |
||
309 | * request. |
||
310 | * |
||
311 | * Important: As a list or detail view can use several hundred items, |
||
312 | * this configuration option will also add this number of tags to the |
||
313 | * cache entry. When using a cache adapter that can't insert all tags |
||
314 | * at once, this slows down the initial cache insert (and therefore the |
||
315 | * page speed) drastically! It's only recommended to enable this option |
||
316 | * if you use the DB, Mysql or Redis adapter that can insert all tags |
||
317 | * at once. |
||
318 | * |
||
319 | * @param boolean True to add tags for all items, false to use only a domain tag |
||
320 | * @since 2014.07 |
||
321 | * @see client/html/common/cache/force |
||
322 | * @see madmin/cache/manager/name |
||
323 | * @see madmin/cache/name |
||
324 | */ |
||
325 | $tagAll = $this->context->config()->get( 'client/html/common/cache/tag-all', true ); |
||
326 | |||
327 | $expires = []; |
||
328 | |||
329 | foreach( map( $items ) as $item ) |
||
330 | { |
||
331 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) { |
||
332 | $this->addMetaItemRef( $item, $expires, $tags, $tagAll ); |
||
333 | } |
||
334 | |||
335 | $this->addMetaItemSingle( $item, $expires, $tags, $tagAll ); |
||
336 | } |
||
337 | |||
338 | if( $expire !== null ) { |
||
339 | $expires[] = $expire; |
||
340 | } |
||
341 | |||
342 | if( !empty( $expires ) ) { |
||
343 | $expire = min( $expires ); |
||
344 | } |
||
345 | |||
346 | $tags = array_unique( array_merge( $tags, $custom ) ); |
||
347 | |||
348 | return $items; |
||
349 | } |
||
350 | |||
351 | |||
352 | /** |
||
353 | * Adds the cache tags to the given list and sets a new expiration date if necessary based on the given catalog tree. |
||
354 | * |
||
355 | * @param \Aimeos\MShop\Catalog\Item\Iface $tree Tree node, maybe with sub-nodes |
||
356 | * @param string|null &$expire Expiration date that will be overwritten if an earlier date is found |
||
357 | * @param array &$tags List of tags the new tags will be added to |
||
358 | * @param array $custom List of custom tags which are added too |
||
359 | */ |
||
360 | protected function addMetaItemCatalog( \Aimeos\MShop\Catalog\Item\Iface $tree, ?string &$expire = null, array &$tags = [], array $custom = [] ) |
||
361 | { |
||
362 | $this->addMetaItems( $tree, $expire, $tags, $custom ); |
||
363 | |||
364 | foreach( $tree->getChildren() as $child ) { |
||
365 | $this->addMetaItemCatalog( $child, $expire, $tags, $custom ); |
||
366 | } |
||
367 | } |
||
368 | |||
369 | |||
370 | /** |
||
371 | * Adds expire date and tags for a single item. |
||
372 | * |
||
373 | * @param \Aimeos\MShop\Common\Item\Iface $item Item, maybe with associated list items |
||
374 | * @param array &$expires Will contain the list of expiration dates |
||
375 | * @param array &$tags List of tags the new tags will be added to |
||
376 | * @param bool $tagAll True of tags for all items should be added, false if only for the main item |
||
377 | */ |
||
378 | private function addMetaItemSingle( \Aimeos\MShop\Common\Item\Iface $item, array &$expires, array &$tags, bool $tagAll ) |
||
379 | { |
||
380 | $domain = str_replace( '/', '_', $item->getResourceType() ); // maximum compatiblity |
||
381 | |||
382 | if( in_array( $item->getResourceType(), ['catalog', 'product', 'supplier', 'cms'] ) ) { |
||
383 | $tags[] = $tagAll ? $domain . '-' . $item->getId() : $domain; |
||
384 | } |
||
385 | |||
386 | if( $item instanceof \Aimeos\MShop\Common\Item\Time\Iface && ( $date = $item->getDateEnd() ) !== null ) { |
||
387 | $expires[] = $date; |
||
388 | } |
||
389 | |||
390 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) { |
||
391 | $this->addMetaItemRef( $item, $expires, $tags, $tagAll ); |
||
392 | } |
||
393 | } |
||
394 | |||
395 | |||
396 | /** |
||
397 | * Adds expire date and tags for referenced items |
||
398 | * |
||
399 | * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with associated list items |
||
400 | * @param array &$expires Will contain the list of expiration dates |
||
401 | * @param array &$tags List of tags the new tags will be added to |
||
402 | * @param bool $tagAll True of tags for all items should be added, false if only for the main item |
||
403 | */ |
||
404 | private function addMetaItemRef( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array &$expires, array &$tags, bool $tagAll ) |
||
405 | { |
||
406 | foreach( $item->getListItems() as $listItem ) |
||
407 | { |
||
408 | if( ( $refItem = $listItem->getRefItem() ) === null ) { |
||
409 | continue; |
||
410 | } |
||
411 | |||
412 | if( $tagAll === true && in_array( $listItem->getDomain(), ['catalog', 'product', 'supplier'] ) ) { |
||
413 | $tags[] = str_replace( '/', '_', $listItem->getDomain() ) . '-' . $listItem->getRefId(); |
||
414 | } |
||
415 | |||
416 | if( ( $date = $listItem->getDateEnd() ) !== null ) { |
||
417 | $expires[] = $date; |
||
418 | } |
||
419 | |||
420 | $this->addMetaItemSingle( $refItem, $expires, $tags, $tagAll ); |
||
421 | } |
||
422 | } |
||
423 | |||
424 | |||
425 | /** |
||
426 | * Returns the client type of the class |
||
427 | * |
||
428 | * @return string Client type, e.g. "catalog/detail" |
||
429 | */ |
||
430 | protected function clientType() : string |
||
431 | { |
||
432 | return strtolower( trim( dirname( str_replace( '\\', '/', substr( get_class( $this ), 19 ) ) ), '/' ) ); |
||
433 | } |
||
434 | |||
435 | |||
436 | /** |
||
437 | * Returns the sub-client given by its name. |
||
438 | * |
||
439 | * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
||
440 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
441 | * @return \Aimeos\Client\Html\Iface Sub-part object |
||
442 | * @throws \LogicException If class can't be instantiated |
||
443 | */ |
||
444 | protected function createSubClient( string $path, ?string $name = null ) : \Aimeos\Client\Html\Iface |
||
445 | { |
||
446 | $path = strtolower( $path ); |
||
447 | $name = $name ?: $this->context->config()->get( 'client/html/' . $path . '/name', 'Standard' ); |
||
448 | |||
449 | if( empty( $name ) || ctype_alnum( $name ) === false ) { |
||
450 | throw new \LogicException( sprintf( 'Invalid characters in client name "%1$s"', $name ), 400 ); |
||
451 | } |
||
452 | |||
453 | $subnames = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
||
454 | $classname = '\\Aimeos\\Client\\Html\\' . $subnames . '\\' . $name; |
||
455 | $interface = \Aimeos\Client\Html\Iface::class; |
||
456 | |||
457 | $object = \Aimeos\Utils::create( $classname, [$this->context], $interface ); |
||
458 | $object = $this->addClientDecorators( $object, $path ); |
||
459 | |||
460 | return $object->setObject( $object ); |
||
461 | } |
||
462 | |||
463 | |||
464 | /** |
||
465 | * Returns the minimal expiration date. |
||
466 | * |
||
467 | * @param string|null $first First expiration date or null |
||
468 | * @param string|null $second Second expiration date or null |
||
469 | * @return string|null Expiration date |
||
470 | */ |
||
471 | protected function expires( ?string $first = null, ?string $second = null ) : ?string |
||
472 | { |
||
473 | return ( $first !== null ? ( $second !== null ? min( $first, $second ) : $first ) : $second ); |
||
474 | } |
||
475 | |||
476 | |||
477 | /** |
||
478 | * Returns the parameters used by the html client. |
||
479 | * |
||
480 | * @param array $params Associative list of all parameters |
||
481 | * @param array $prefixes List of prefixes the parameters must start with |
||
482 | * @return array Associative list of parameters used by the html client |
||
483 | */ |
||
484 | protected function getClientParams( array $params, array $prefixes = ['f_', 'l_', 'd_', 'path'] ) : array |
||
485 | { |
||
486 | return map( $params )->filter( function( $val, $key ) use ( $prefixes ) { |
||
487 | return \Aimeos\Base\Str::starts( $key, $prefixes ); |
||
488 | } )->toArray(); |
||
489 | } |
||
490 | |||
491 | |||
492 | /** |
||
493 | * Returns the context object. |
||
494 | * |
||
495 | * @return \Aimeos\MShop\ContextIface Context object |
||
496 | */ |
||
497 | protected function context() : \Aimeos\MShop\ContextIface |
||
498 | { |
||
499 | return $this->context; |
||
500 | } |
||
501 | |||
502 | |||
503 | /** |
||
504 | * Generates an unique hash from based on the input suitable to be used as part of the cache key |
||
505 | * |
||
506 | * @param array $prefixes List of prefixes the parameters must start with |
||
507 | * @param string $key Unique identifier if the content is placed more than once on the same page |
||
508 | * @param array $config Multi-dimensional array of configuration options used by the client and sub-clients |
||
509 | * @return string Unique hash |
||
510 | */ |
||
511 | protected function getParamHash( array $prefixes = ['f_', 'l_', 'd_'], string $key = '', array $config = [] ) : string |
||
512 | { |
||
513 | $locale = $this->context()->locale(); |
||
514 | $pstr = map( $this->getClientParams( $this->view()->param(), $prefixes ) )->ksort()->toJson(); |
||
515 | |||
516 | if( ( $cstr = json_encode( $config ) ) === false ) { |
||
517 | throw new \Aimeos\Client\Html\Exception( 'Unable to encode parameters or configuration options' ); |
||
518 | } |
||
519 | |||
520 | return md5( $key . $pstr . $cstr . $locale->getLanguageId() . $locale->getCurrencyId() . $locale->getSiteId() ); |
||
521 | } |
||
522 | |||
523 | |||
524 | /** |
||
525 | * Returns the list of sub-client names configured for the client. |
||
526 | * |
||
527 | * @return array List of HTML client names |
||
528 | */ |
||
529 | protected function getSubClientNames() : array |
||
530 | { |
||
531 | return []; |
||
532 | } |
||
533 | |||
534 | |||
535 | /** |
||
536 | * Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
||
537 | * |
||
538 | * @return array List of sub-clients implementing \Aimeos\Client\Html\Iface ordered in the same way as the names |
||
539 | */ |
||
540 | protected function getSubClients() : array |
||
541 | { |
||
542 | if( !isset( $this->subclients ) ) |
||
543 | { |
||
544 | $this->subclients = []; |
||
545 | |||
546 | foreach( $this->getSubClientNames() as $name ) { |
||
547 | $this->subclients[$name] = $this->getSubClient( $name ); |
||
548 | } |
||
549 | } |
||
550 | |||
551 | return $this->subclients; |
||
552 | } |
||
553 | |||
554 | |||
555 | /** |
||
556 | * Returns the cache entry for the given unique ID and type. |
||
557 | * |
||
558 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
559 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
560 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
561 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
562 | * @return string|null Cached entry or null if not available |
||
563 | */ |
||
564 | protected function cached( string $type, string $uid, array $prefixes, string $confkey ) : ?string |
||
565 | { |
||
566 | $context = $this->context(); |
||
567 | $config = $context->config(); |
||
568 | |||
569 | /** client/html/common/cache/force |
||
570 | * Enforces content caching regardless of user logins |
||
571 | * |
||
572 | * Caching the component output is normally disabled as soon as the |
||
573 | * user has logged in. This enables displaying user or user group |
||
574 | * specific content without mixing standard and user specific output. |
||
575 | * |
||
576 | * If you don't have any user or user group specific content |
||
577 | * (products, categories, attributes, media, prices, texts, etc.), |
||
578 | * you can enforce content caching nevertheless to keep response |
||
579 | * times as low as possible. |
||
580 | * |
||
581 | * @param boolean True to cache output regardless of login, false for no caching |
||
582 | * @since 2015.08 |
||
583 | * @see client/html/common/cache/tag-all |
||
584 | */ |
||
585 | $force = $config->get( 'client/html/common/cache/force', false ); |
||
586 | $enable = $config->get( $confkey . '/cache', true ); |
||
587 | |||
588 | if( $enable == false || $force == false && $context->user() !== null ) { |
||
589 | return null; |
||
590 | } |
||
591 | |||
592 | $cfg = array_merge( $config->get( 'client/html', [] ), $this->getSubClientNames() ); |
||
593 | |||
594 | $keys = array( |
||
595 | 'body' => $this->getParamHash( $prefixes, $uid . ':' . $confkey . ':body', $cfg ), |
||
596 | 'header' => $this->getParamHash( $prefixes, $uid . ':' . $confkey . ':header', $cfg ), |
||
597 | ); |
||
598 | |||
599 | if( !isset( $this->cache[$keys[$type]] ) ) { |
||
600 | $this->cache = $context->cache()->getMultiple( $keys ); |
||
601 | } |
||
602 | |||
603 | return ( isset( $this->cache[$keys[$type]] ) ? $this->cache[$keys[$type]] : null ); |
||
604 | } |
||
605 | |||
606 | |||
607 | /** |
||
608 | * Returns the cache entry for the given type and unique ID. |
||
609 | * |
||
610 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
611 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
612 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
613 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
614 | * @param string $value Value string that should be stored for the given key |
||
615 | * @param array $tags List of tag strings that should be assoicated to the given value in the cache |
||
616 | * @param string|null $expire Date/time string in "YYYY-MM-DD HH:mm:ss" format when the cache entry expires |
||
617 | * @return string Cached value |
||
618 | */ |
||
619 | protected function cache( string $type, string $uid, array $prefixes, string $confkey, string $value, array $tags, ?string $expire = null ) : string |
||
620 | { |
||
621 | $context = $this->context(); |
||
622 | $config = $context->config(); |
||
623 | |||
624 | $force = $config->get( 'client/html/common/cache/force', false ); |
||
625 | $enable = $config->get( $confkey . '/cache', true ); |
||
626 | |||
627 | if( !$value || !$enable || !$force && $context->user() ) { |
||
628 | return $value; |
||
629 | } |
||
630 | |||
631 | $cfg = array_merge( $config->get( 'client/html', [] ), $this->getSubClientNames() ); |
||
632 | $key = $this->getParamHash( $prefixes, $uid . ':' . $confkey . ':' . $type, $cfg ); |
||
633 | |||
634 | $context->cache()->set( $key, $value, $expire, array_unique( $tags ) ); |
||
635 | |||
636 | return $value; |
||
637 | } |
||
638 | |||
639 | |||
640 | /** |
||
641 | * Writes the exception details to the log |
||
642 | * |
||
643 | * @param \Exception $e Exception object |
||
644 | * @param int $level Log level of the exception |
||
645 | */ |
||
646 | protected function logException( \Exception $e, int $level = \Aimeos\Base\Logger\Iface::WARN ) |
||
647 | { |
||
648 | $uri = $this->view()->request()->getServerParams()['REQUEST_URI'] ?? ''; |
||
649 | $msg = ( $uri ? $uri . PHP_EOL : '' ) . $e->getMessage() . PHP_EOL . $e->getTraceAsString(); |
||
650 | $this->context->logger()->log( $msg, $level, 'client/html' ); |
||
651 | } |
||
652 | |||
653 | |||
654 | /** |
||
655 | * Replaces the section in the content that is enclosed by the marker. |
||
656 | * |
||
657 | * @param string $content Cached content |
||
658 | * @param string $section New section content |
||
659 | * @param string $marker Name of the section marker without "<!-- " and " -->" parts |
||
660 | */ |
||
661 | protected function replaceSection( string $content, string $section, string $marker ) : string |
||
662 | { |
||
663 | $marker = '<!-- ' . $marker . ' -->'; |
||
664 | $clen = strlen( $content ); |
||
665 | $mlen = strlen( $marker ); |
||
666 | $len = strlen( $section ); |
||
667 | $start = 0; |
||
668 | |||
669 | while( $start + 2 * $mlen <= $clen && ( $start = strpos( $content, $marker, $start ) ) !== false ) |
||
670 | { |
||
671 | if( ( $end = strpos( $content, $marker, $start + $mlen ) ) !== false ) { |
||
672 | $content = substr_replace( $content, $section, $start + $mlen, $end - $start - $mlen ); |
||
673 | } |
||
674 | |||
675 | $start += 2 * $mlen + $len; |
||
676 | } |
||
677 | |||
678 | return $content; |
||
679 | } |
||
680 | } |
||
681 |