/**
* Create workarounds for data to be cached
*
* @param string $data Cached data
* @param array $options Array of options
*
* @return array Data to be cached
*
* @since 1.7.0
*/
public static function setWorkarounds($data, $options = [])
{
$loptions = ['nopathway' => 0, 'nohead' => 0, 'nomodules' => 0, 'modulemode' => 0];
if (isset($options['nopathway'])) {
$loptions['nopathway'] = $options['nopathway'];
}
if (isset($options['nohead'])) {
$loptions['nohead'] = $options['nohead'];
}
if (isset($options['nomodules'])) {
$loptions['nomodules'] = $options['nomodules'];
}
if (isset($options['modulemode'])) {
$loptions['modulemode'] = $options['modulemode'];
}
$app = Factory::getApplication();
$document = Factory::getDocument();
if ($loptions['nomodules'] != 1) {
// Get the modules buffer before component execution.
$buffer1 = $document->getBuffer();
if (!\is_array($buffer1)) {
$buffer1 = [];
}
// Make sure the module buffer is an array.
if (!isset($buffer1['module']) || !\is_array($buffer1['module'])) {
$buffer1['module'] = [];
}
}
// View body data
$cached['body'] = $data;
// Document head data
if ($loptions['nohead'] != 1 && method_exists($document, 'getHeadData')) {
if ($loptions['modulemode'] == 1) {
$headNow = $document->getHeadData();
$unset = ['title', 'description', 'link', 'links', 'metaTags'];
foreach ($unset as $key) {
unset($headNow[$key]);
}
// Sanitize empty data
foreach (\array_keys($headNow) as $key) {
if (!isset($headNow[$key]) || $headNow[$key] === []) {
unset($headNow[$key]);
}
}
$cached['head'] = $headNow;
} else {
$cached['head'] = $document->getHeadData();
// Document MIME encoding
$cached['mime_encoding'] = $document->getMimeEncoding();
}
}
// Pathway data
if ($app->isClient('site') && $loptions['nopathway'] != 1) {
$cached['pathway'] = $data['pathway'] ?? $app->getPathway()->getPathway();
}
if ($loptions['nomodules'] != 1) {
// @todo Check if the following is needed, seems like it should be in page cache
// Get the module buffer after component execution.
$buffer2 = $document->getBuffer();
if (!\is_array($buffer2)) {
$buffer2 = [];
}
// Make sure the module buffer is an array.
if (!isset($buffer2['module']) || !\is_array($buffer2['module'])) {
$buffer2['module'] = [];
}
// Compare the second module buffer against the first buffer.
$cached['module'] = array_diff_assoc($buffer2['module'], $buffer1['module']);
}
// Headers data
if (isset($options['headers']) && $options['headers']) {
$cached['headers'] = $app->getHeaders();
}
return $cached;
}