| Total Complexity | 77 |
| Total Lines | 647 |
| Duplicated Lines | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Base often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Base, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | abstract class Base |
||
| 24 | implements \Aimeos\Client\Html\Iface, \Aimeos\MW\Macro\Iface |
||
| 25 | { |
||
| 26 | use \Aimeos\MW\Macro\Traits; |
||
| 27 | |||
| 28 | |||
| 29 | private $view; |
||
| 30 | private $cache; |
||
| 31 | private $object; |
||
| 32 | private $context; |
||
| 33 | private $subclients; |
||
| 34 | |||
| 35 | |||
| 36 | /** |
||
| 37 | * Initializes the class instance. |
||
| 38 | * |
||
| 39 | * @param \Aimeos\MShop\Context\Item\Iface $context Context object |
||
| 40 | */ |
||
| 41 | public function __construct( \Aimeos\MShop\Context\Item\Iface $context ) |
||
| 42 | { |
||
| 43 | $this->context = $context; |
||
| 44 | } |
||
| 45 | |||
| 46 | |||
| 47 | /** |
||
| 48 | * Adds the data to the view object required by the templates |
||
| 49 | * |
||
| 50 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 51 | * @param array &$tags Result array for the list of tags that are associated to the output |
||
| 52 | * @param string|null &$expire Result variable for the expiration date of the output (null for no expiry) |
||
| 53 | * @return \Aimeos\MW\View\Iface The view object with the data required by the templates |
||
| 54 | * @since 2018.01 |
||
| 55 | */ |
||
| 56 | public function data( \Aimeos\MW\View\Iface $view, array &$tags = [], string &$expire = null ) : \Aimeos\MW\View\Iface |
||
| 57 | { |
||
| 58 | foreach( $this->getSubClients() as $name => $subclient ) { |
||
| 59 | $view = $subclient->data( $view, $tags, $expire ); |
||
| 60 | } |
||
| 61 | |||
| 62 | return $view; |
||
| 63 | } |
||
| 64 | |||
| 65 | |||
| 66 | /** |
||
| 67 | * Returns the HTML string for insertion into the header. |
||
| 68 | * |
||
| 69 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 70 | * @return string|null String including HTML tags for the header on error |
||
| 71 | */ |
||
| 72 | public function header( string $uid = '' ) : ?string |
||
| 73 | { |
||
| 74 | $html = ''; |
||
| 75 | |||
| 76 | foreach( $this->getSubClients() as $subclient ) { |
||
| 77 | $html .= $subclient->setView( $this->view )->header( $uid ); |
||
| 78 | } |
||
| 79 | |||
| 80 | return $html; |
||
| 81 | } |
||
| 82 | |||
| 83 | |||
| 84 | /** |
||
| 85 | * Processes the input, e.g. store given values. |
||
| 86 | * |
||
| 87 | * A view must be available and this method doesn't generate any output |
||
| 88 | * besides setting view variables. |
||
| 89 | */ |
||
| 90 | public function init() |
||
| 91 | { |
||
| 92 | $view = $this->view(); |
||
| 93 | |||
| 94 | foreach( $this->getSubClients() as $subclient ) { |
||
| 95 | $subclient->setView( $view )->init(); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | |||
| 100 | /** |
||
| 101 | * Modifies the cached body content to replace content based on sessions or cookies. |
||
| 102 | * |
||
| 103 | * @param string $content Cached content |
||
| 104 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 105 | * @return string Modified body content |
||
| 106 | */ |
||
| 107 | public function modifyBody( string $content, string $uid ) : string |
||
| 108 | { |
||
| 109 | $view = $this->view(); |
||
| 110 | |||
| 111 | foreach( $this->getSubClients() as $subclient ) |
||
| 112 | { |
||
| 113 | $subclient->setView( $view ); |
||
| 114 | $content = $subclient->modifyBody( $content, $uid ); |
||
| 115 | } |
||
| 116 | |||
| 117 | return $content; |
||
| 118 | } |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * Modifies the cached header content to replace content based on sessions or cookies. |
||
| 123 | * |
||
| 124 | * @param string $content Cached content |
||
| 125 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 126 | * @return string Modified header content |
||
| 127 | */ |
||
| 128 | public function modifyHeader( string $content, string $uid ) : string |
||
| 129 | { |
||
| 130 | $view = $this->view(); |
||
| 131 | |||
| 132 | foreach( $this->getSubClients() as $subclient ) |
||
| 133 | { |
||
| 134 | $subclient->setView( $view ); |
||
| 135 | $content = $subclient->modifyHeader( $content, $uid ); |
||
| 136 | } |
||
| 137 | |||
| 138 | return $content; |
||
| 139 | } |
||
| 140 | |||
| 141 | |||
| 142 | /** |
||
| 143 | * Returns the PSR-7 response object for the request |
||
| 144 | * |
||
| 145 | * @return \Psr\Http\Message\ResponseInterface Response object |
||
| 146 | */ |
||
| 147 | public function response() : \Psr\Http\Message\ResponseInterface |
||
| 148 | { |
||
| 149 | return $this->view()->response(); |
||
| 150 | } |
||
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Injects the reference of the outmost client object or decorator |
||
| 155 | * |
||
| 156 | * @param \Aimeos\Client\Html\Iface $object Reference to the outmost client or decorator |
||
| 157 | * @return \Aimeos\Client\Html\Iface Client object for chaining method calls |
||
| 158 | */ |
||
| 159 | public function setObject( \Aimeos\Client\Html\Iface $object ) : \Aimeos\Client\Html\Iface |
||
| 160 | { |
||
| 161 | $this->object = $object; |
||
| 162 | return $this; |
||
| 163 | } |
||
| 164 | |||
| 165 | |||
| 166 | /** |
||
| 167 | * Sets the view object that will generate the HTML output. |
||
| 168 | * |
||
| 169 | * @param \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 170 | * @return \Aimeos\Client\Html\Iface Reference to this object for fluent calls |
||
| 171 | */ |
||
| 172 | public function setView( \Aimeos\MW\View\Iface $view ) : \Aimeos\Client\Html\Iface |
||
| 173 | { |
||
| 174 | $this->view = $view; |
||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | |||
| 179 | /** |
||
| 180 | * Returns the outmost decorator of the decorator stack |
||
| 181 | * |
||
| 182 | * @return \Aimeos\Client\Html\Iface Outmost decorator object |
||
| 183 | */ |
||
| 184 | protected function object() : \Aimeos\Client\Html\Iface |
||
| 185 | { |
||
| 186 | if( $this->object !== null ) { |
||
| 187 | return $this->object; |
||
| 188 | } |
||
| 189 | |||
| 190 | return $this; |
||
| 191 | } |
||
| 192 | |||
| 193 | |||
| 194 | /** |
||
| 195 | * Returns the view object that will generate the HTML output. |
||
| 196 | * |
||
| 197 | * @return \Aimeos\MW\View\Iface $view The view object which generates the HTML output |
||
| 198 | */ |
||
| 199 | protected function view() : \Aimeos\MW\View\Iface |
||
| 200 | { |
||
| 201 | if( !isset( $this->view ) ) { |
||
| 202 | throw new \Aimeos\Client\Html\Exception( sprintf( 'No view available' ) ); |
||
| 203 | } |
||
| 204 | |||
| 205 | return $this->view; |
||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * Adds the decorators to the client object |
||
| 211 | * |
||
| 212 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
| 213 | * @param array $decorators List of decorator name that should be wrapped around the client |
||
| 214 | * @param string $classprefix Decorator class prefix, e.g. "\Aimeos\Client\Html\Catalog\Decorator\" |
||
| 215 | * @return \Aimeos\Client\Html\Iface Client object |
||
| 216 | */ |
||
| 217 | protected function addDecorators( \Aimeos\Client\Html\Iface $client, array $decorators, string $classprefix ) : \Aimeos\Client\Html\Iface |
||
| 218 | { |
||
| 219 | foreach( $decorators as $name ) |
||
| 220 | { |
||
| 221 | if( ctype_alnum( $name ) === false ) |
||
| 222 | { |
||
| 223 | $classname = is_string( $name ) ? $classprefix . $name : '<not a string>'; |
||
| 224 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid class name "%1$s"', $classname ) ); |
||
| 225 | } |
||
| 226 | |||
| 227 | $classname = $classprefix . $name; |
||
| 228 | |||
| 229 | if( class_exists( $classname ) === false ) { |
||
| 230 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Class "%1$s" not found', $classname ) ); |
||
| 231 | } |
||
| 232 | |||
| 233 | $client = new $classname( $client, $this->context ); |
||
| 234 | |||
| 235 | \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Client\\Html\\Common\\Decorator\\Iface', $client ); |
||
| 236 | } |
||
| 237 | |||
| 238 | return $client; |
||
| 239 | } |
||
| 240 | |||
| 241 | |||
| 242 | /** |
||
| 243 | * Adds the decorators to the client object |
||
| 244 | * |
||
| 245 | * @param \Aimeos\Client\Html\Iface $client Client object |
||
| 246 | * @param string $path Client string in lower case, e.g. "catalog/detail/basic" |
||
| 247 | * @return \Aimeos\Client\Html\Iface Client object |
||
| 248 | */ |
||
| 249 | protected function addClientDecorators( \Aimeos\Client\Html\Iface $client, string $path ) : \Aimeos\Client\Html\Iface |
||
| 250 | { |
||
| 251 | if( !is_string( $path ) || $path === '' ) { |
||
|
|
|||
| 252 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid domain "%1$s"', $path ) ); |
||
| 253 | } |
||
| 254 | |||
| 255 | $localClass = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
||
| 256 | $config = $this->context->getConfig(); |
||
| 257 | |||
| 258 | $classprefix = '\\Aimeos\\Client\\Html\\Common\\Decorator\\'; |
||
| 259 | $decorators = $config->get( 'client/html/' . $path . '/decorators/global', [] ); |
||
| 260 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
| 261 | |||
| 262 | $classprefix = '\\Aimeos\\Client\\Html\\' . $localClass . '\\Decorator\\'; |
||
| 263 | $decorators = $config->get( 'client/html/' . $path . '/decorators/local', [] ); |
||
| 264 | $client = $this->addDecorators( $client, $decorators, $classprefix ); |
||
| 265 | |||
| 266 | return $client; |
||
| 267 | } |
||
| 268 | |||
| 269 | |||
| 270 | /** |
||
| 271 | * Adds the cache tags to the given list and sets a new expiration date if necessary based on the given item. |
||
| 272 | * |
||
| 273 | * @param array|\Aimeos\MShop\Common\Item\Iface $items Item or list of items, maybe with associated list items |
||
| 274 | * @param string|null &$expire Expiration date that will be overwritten if an earlier date is found |
||
| 275 | * @param array &$tags List of tags the new tags will be added to |
||
| 276 | * @param array $custom List of custom tags which are added too |
||
| 277 | */ |
||
| 278 | protected function addMetaItems( $items, string &$expire = null, array &$tags, array $custom = [] ) |
||
| 279 | { |
||
| 280 | /** client/html/common/cache/tag-all |
||
| 281 | * Adds tags for all items used in a cache entry |
||
| 282 | * |
||
| 283 | * Each cache entry storing rendered parts for the HTML header or body |
||
| 284 | * can be tagged with information which items like texts, media, etc. |
||
| 285 | * are used in the HTML. This allows removing only those cache entries |
||
| 286 | * whose content has really changed and only that entries have to be |
||
| 287 | * rebuild the next time. |
||
| 288 | * |
||
| 289 | * The standard behavior stores only tags for each used domain, e.g. if |
||
| 290 | * a text is used, only the tag "text" is added. If you change a text |
||
| 291 | * in the administration interface, all cache entries with the tag |
||
| 292 | * "text" will be removed from the cache. This effectively wipes out |
||
| 293 | * almost all cached entries, which have to be rebuild with the next |
||
| 294 | * request. |
||
| 295 | * |
||
| 296 | * Important: As a list or detail view can use several hundred items, |
||
| 297 | * this configuration option will also add this number of tags to the |
||
| 298 | * cache entry. When using a cache adapter that can't insert all tags |
||
| 299 | * at once, this slows down the initial cache insert (and therefore the |
||
| 300 | * page speed) drastically! It's only recommended to enable this option |
||
| 301 | * if you use the DB, Mysql or Redis adapter that can insert all tags |
||
| 302 | * at once. |
||
| 303 | * |
||
| 304 | * @param boolean True to add tags for all items, false to use only a domain tag |
||
| 305 | * @since 2014.07 |
||
| 306 | * @category Developer |
||
| 307 | * @category User |
||
| 308 | * @see client/html/common/cache/force |
||
| 309 | * @see madmin/cache/manager/name |
||
| 310 | * @see madmin/cache/name |
||
| 311 | */ |
||
| 312 | $tagAll = $this->context->getConfig()->get( 'client/html/common/cache/tag-all', false ); |
||
| 313 | |||
| 314 | if( !is_array( $items ) && !is_map( $items ) ) { |
||
| 315 | $items = map( [$items] ); |
||
| 316 | } |
||
| 317 | |||
| 318 | $expires = $idMap = []; |
||
| 319 | |||
| 320 | foreach( $items as $item ) |
||
| 321 | { |
||
| 322 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) |
||
| 323 | { |
||
| 324 | $this->addMetaItemRef( $item, $expires, $tags, $tagAll ); |
||
| 325 | $idMap[$item->getResourceType()][] = $item->getId(); |
||
| 326 | } |
||
| 327 | |||
| 328 | $this->addMetaItemSingle( $item, $expires, $tags, $tagAll ); |
||
| 329 | } |
||
| 330 | |||
| 331 | if( $expire !== null ) { |
||
| 332 | $expires[] = $expire; |
||
| 333 | } |
||
| 334 | |||
| 335 | if( !empty( $expires ) ) { |
||
| 336 | $expire = min( $expires ); |
||
| 337 | } |
||
| 338 | |||
| 339 | $tags = array_unique( array_merge( $tags, $custom ) ); |
||
| 340 | } |
||
| 341 | |||
| 342 | |||
| 343 | /** |
||
| 344 | * Adds expire date and tags for a single item. |
||
| 345 | * |
||
| 346 | * @param \Aimeos\MShop\Common\Item\Iface $item Item, maybe with associated list items |
||
| 347 | * @param array &$expires Will contain the list of expiration dates |
||
| 348 | * @param array &$tags List of tags the new tags will be added to |
||
| 349 | * @param bool $tagAll True of tags for all items should be added, false if only for the main item |
||
| 350 | */ |
||
| 351 | private function addMetaItemSingle( \Aimeos\MShop\Common\Item\Iface $item, array &$expires, array &$tags, bool $tagAll ) |
||
| 352 | { |
||
| 353 | $domain = str_replace( '/', '_', $item->getResourceType() ); // maximum compatiblity |
||
| 354 | |||
| 355 | if( $tagAll === true ) { |
||
| 356 | $tags[] = $domain . '-' . $item->getId(); |
||
| 357 | } else { |
||
| 358 | $tags[] = $domain; |
||
| 359 | } |
||
| 360 | |||
| 361 | if( $item instanceof \Aimeos\MShop\Common\Item\Time\Iface && ( $date = $item->getDateEnd() ) !== null ) { |
||
| 362 | $expires[] = $date; |
||
| 363 | } |
||
| 364 | |||
| 365 | if( $item instanceof \Aimeos\MShop\Common\Item\ListsRef\Iface ) { |
||
| 366 | $this->addMetaItemRef( $item, $expires, $tags, $tagAll ); |
||
| 367 | } |
||
| 368 | } |
||
| 369 | |||
| 370 | |||
| 371 | /** |
||
| 372 | * Adds expire date and tags for referenced items |
||
| 373 | * |
||
| 374 | * @param \Aimeos\MShop\Common\Item\ListsRef\Iface $item Item with associated list items |
||
| 375 | * @param array &$expires Will contain the list of expiration dates |
||
| 376 | * @param array &$tags List of tags the new tags will be added to |
||
| 377 | * @param bool $tagAll True of tags for all items should be added, false if only for the main item |
||
| 378 | */ |
||
| 379 | private function addMetaItemRef( \Aimeos\MShop\Common\Item\ListsRef\Iface $item, array &$expires, array &$tags, bool $tagAll ) |
||
| 380 | { |
||
| 381 | foreach( $item->getListItems() as $listitem ) |
||
| 382 | { |
||
| 383 | if( ( $refItem = $listitem->getRefItem() ) === null ) { |
||
| 384 | continue; |
||
| 385 | } |
||
| 386 | |||
| 387 | if( $tagAll === true ) { |
||
| 388 | $tags[] = str_replace( '/', '_', $listitem->getDomain() ) . '-' . $listitem->getRefId(); |
||
| 389 | } |
||
| 390 | |||
| 391 | if( ( $date = $listitem->getDateEnd() ) !== null ) { |
||
| 392 | $expires[] = $date; |
||
| 393 | } |
||
| 394 | |||
| 395 | $this->addMetaItemSingle( $refItem, $expires, $tags, $tagAll ); |
||
| 396 | } |
||
| 397 | } |
||
| 398 | |||
| 399 | |||
| 400 | /** |
||
| 401 | * Returns the sub-client given by its name. |
||
| 402 | * |
||
| 403 | * @param string $path Name of the sub-part in lower case (can contain a path like catalog/filter/tree) |
||
| 404 | * @param string|null $name Name of the implementation, will be from configuration (or Default) if null |
||
| 405 | * @return \Aimeos\Client\Html\Iface Sub-part object |
||
| 406 | */ |
||
| 407 | protected function createSubClient( string $path, string $name = null ) : \Aimeos\Client\Html\Iface |
||
| 408 | { |
||
| 409 | $path = strtolower( $path ); |
||
| 410 | |||
| 411 | if( $name === null ) { |
||
| 412 | $name = $this->context->getConfig()->get( 'client/html/' . $path . '/name', 'Standard' ); |
||
| 413 | } |
||
| 414 | |||
| 415 | if( empty( $name ) || ctype_alnum( $name ) === false ) { |
||
| 416 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Invalid characters in client name "%1$s"', $name ) ); |
||
| 417 | } |
||
| 418 | |||
| 419 | $subnames = str_replace( '/', '\\', ucwords( $path, '/' ) ); |
||
| 420 | $classname = '\\Aimeos\\Client\\Html\\' . $subnames . '\\' . $name; |
||
| 421 | |||
| 422 | if( class_exists( $classname ) === false ) { |
||
| 423 | throw new \Aimeos\Client\Html\Exception( sprintf( 'Class "%1$s" not available', $classname ) ); |
||
| 424 | } |
||
| 425 | |||
| 426 | $object = new $classname( $this->context ); |
||
| 427 | $object = \Aimeos\MW\Common\Base::checkClass( '\\Aimeos\\Client\\Html\\Iface', $object ); |
||
| 428 | $object = $this->addClientDecorators( $object, $path ); |
||
| 429 | |||
| 430 | return $object->setObject( $object ); |
||
| 431 | } |
||
| 432 | |||
| 433 | |||
| 434 | /** |
||
| 435 | * Returns the minimal expiration date. |
||
| 436 | * |
||
| 437 | * @param string|null $first First expiration date or null |
||
| 438 | * @param string|null $second Second expiration date or null |
||
| 439 | * @return string|null Expiration date |
||
| 440 | */ |
||
| 441 | protected function expires( string $first = null, string $second = null ) : ?string |
||
| 442 | { |
||
| 443 | return ( $first !== null ? ( $second !== null ? min( $first, $second ) : $first ) : $second ); |
||
| 444 | } |
||
| 445 | |||
| 446 | |||
| 447 | /** |
||
| 448 | * Returns the parameters used by the html client. |
||
| 449 | * |
||
| 450 | * @param array $params Associative list of all parameters |
||
| 451 | * @param array $prefixes List of prefixes the parameters must start with |
||
| 452 | * @return array Associative list of parameters used by the html client |
||
| 453 | */ |
||
| 454 | protected function getClientParams( array $params, array $prefixes = ['f_', 'l_', 'd_'] ) : array |
||
| 455 | { |
||
| 456 | return map( $params )->filter( function( $val, $key ) use ( $prefixes ) { |
||
| 457 | return \Aimeos\MW\Str::starts( $key, $prefixes ); |
||
| 458 | } )->toArray(); |
||
| 459 | } |
||
| 460 | |||
| 461 | |||
| 462 | /** |
||
| 463 | * Returns the context object. |
||
| 464 | * |
||
| 465 | * @return \Aimeos\MShop\Context\Item\Iface Context object |
||
| 466 | */ |
||
| 467 | protected function getContext() : \Aimeos\MShop\Context\Item\Iface |
||
| 468 | { |
||
| 469 | return $this->context; |
||
| 470 | } |
||
| 471 | |||
| 472 | |||
| 473 | /** |
||
| 474 | * Generates an unique hash from based on the input suitable to be used as part of the cache key |
||
| 475 | * |
||
| 476 | * @param array $prefixes List of prefixes the parameters must start with |
||
| 477 | * @param string $key Unique identifier if the content is placed more than once on the same page |
||
| 478 | * @param array $config Multi-dimensional array of configuration options used by the client and sub-clients |
||
| 479 | * @return string Unique hash |
||
| 480 | */ |
||
| 481 | protected function getParamHash( array $prefixes = ['f_', 'l_', 'd_'], string $key = '', array $config = [] ) : string |
||
| 482 | { |
||
| 483 | $locale = $this->getContext()->getLocale(); |
||
| 484 | $pstr = map( $this->getClientParams( $this->view()->param(), $prefixes ) )->ksort()->toJson(); |
||
| 485 | |||
| 486 | if( ( $cstr = json_encode( $config ) ) === false ) { |
||
| 487 | throw new \Aimeos\Client\Html\Exception( 'Unable to encode parameters or configuration options' ); |
||
| 488 | } |
||
| 489 | |||
| 490 | return md5( $key . $pstr . $cstr . $locale->getLanguageId() . $locale->getCurrencyId() . $locale->getSiteId() ); |
||
| 491 | } |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Returns the list of sub-client names configured for the client. |
||
| 496 | * |
||
| 497 | * @return array List of HTML client names |
||
| 498 | */ |
||
| 499 | abstract protected function getSubClientNames() : array; |
||
| 500 | |||
| 501 | |||
| 502 | /** |
||
| 503 | * Returns the configured sub-clients or the ones named in the default parameter if none are configured. |
||
| 504 | * |
||
| 505 | * @return array List of sub-clients implementing \Aimeos\Client\Html\Iface ordered in the same way as the names |
||
| 506 | */ |
||
| 507 | protected function getSubClients() : array |
||
| 519 | } |
||
| 520 | |||
| 521 | |||
| 522 | /** |
||
| 523 | * Returns the template for the given configuration key |
||
| 524 | * |
||
| 525 | * If the "l_type" parameter is present, a specific template for this given |
||
| 526 | * type is used if available. |
||
| 527 | * |
||
| 528 | * @param string $confkey Key to the configuration setting for the template |
||
| 529 | * @param string $default Default template if none is configured or not found |
||
| 530 | * @return string Relative template path |
||
| 531 | */ |
||
| 532 | protected function getTemplatePath( string $confkey, string $default ) : string |
||
| 533 | { |
||
| 534 | if( ( $type = $this->view->param( 'l_type' ) ) !== null && ctype_alnum( $type ) !== false ) { |
||
| 535 | return $this->view->config( $confkey . '-' . $type, $this->view->config( $confkey, $default ) ); |
||
| 536 | } |
||
| 537 | |||
| 538 | return $this->view->config( $confkey, $default ); |
||
| 539 | } |
||
| 540 | |||
| 541 | |||
| 542 | /** |
||
| 543 | * Returns the cache entry for the given unique ID and type. |
||
| 544 | * |
||
| 545 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
| 546 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 547 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
| 548 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
| 549 | * @return string|null Cached entry or null if not available |
||
| 550 | */ |
||
| 551 | protected function getCached( string $type, string $uid, array $prefixes, string $confkey ) : ?string |
||
| 593 | } |
||
| 594 | |||
| 595 | |||
| 596 | /** |
||
| 597 | * Returns the cache entry for the given type and unique ID. |
||
| 598 | * |
||
| 599 | * @param string $type Type of the cache entry, i.e. "body" or "header" |
||
| 600 | * @param string $uid Unique identifier for the output if the content is placed more than once on the same page |
||
| 601 | * @param string[] $prefixes List of prefixes of all parameters that are relevant for generating the output |
||
| 602 | * @param string $confkey Configuration key prefix that matches all relevant settings for the component |
||
| 603 | * @param string $value Value string that should be stored for the given key |
||
| 604 | * @param array $tags List of tag strings that should be assoicated to the given value in the cache |
||
| 605 | * @param string|null $expire Date/time string in "YYYY-MM-DD HH:mm:ss" format when the cache entry expires |
||
| 606 | */ |
||
| 607 | protected function setCached( string $type, string $uid, array $prefixes, string $confkey, string $value, array $tags, string $expire = null ) |
||
| 608 | { |
||
| 609 | $context = $this->getContext(); |
||
| 610 | $config = $context->getConfig(); |
||
| 611 | |||
| 612 | $force = $config->get( 'client/html/common/cache/force', false ); |
||
| 613 | $enable = $config->get( $confkey . '/cache', true ); |
||
| 614 | |||
| 615 | if( $enable == false || $force == false && $context->getUserId() !== null ) { |
||
| 616 | return; |
||
| 617 | } |
||
| 618 | |||
| 619 | try |
||
| 620 | { |
||
| 621 | $cfg = array_merge( $config->get( 'client/html', [] ), $this->getSubClientNames() ); |
||
| 622 | $key = $this->getParamHash( $prefixes, $uid . ':' . $confkey . ':' . $type, $cfg ); |
||
| 623 | |||
| 624 | $context->getCache()->set( $key, $value, $expire, array_unique( $tags ) ); |
||
| 625 | } |
||
| 626 | catch( \Exception $e ) |
||
| 627 | { |
||
| 628 | $msg = sprintf( 'Unable to set cache entry: %1$s', $e->getMessage() ); |
||
| 629 | $context->getLogger()->log( $msg, Log::NOTICE, 'client/html' ); |
||
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | |||
| 634 | /** |
||
| 635 | * Writes the exception details to the log |
||
| 636 | * |
||
| 637 | * @param \Exception $e Exception object |
||
| 638 | */ |
||
| 639 | protected function logException( \Exception $e ) |
||
| 640 | { |
||
| 641 | $msg = $e->getMessage() . PHP_EOL . $e->getTraceAsString(); |
||
| 642 | $this->context->getLogger()->log( $msg, Log::WARN, 'client/html' ); |
||
| 643 | } |
||
| 644 | |||
| 645 | |||
| 646 | /** |
||
| 647 | * Replaces the section in the content that is enclosed by the marker. |
||
| 648 | * |
||
| 649 | * @param string $content Cached content |
||
| 650 | * @param string $section New section content |
||
| 651 | * @param string $marker Name of the section marker without "<!-- " and " -->" parts |
||
| 652 | */ |
||
| 653 | protected function replaceSection( string $content, string $section, string $marker ) : string |
||
| 670 | } |
||
| 671 | } |
||
| 672 |