/** * Get aggregated and possibly minified content from js and css files * * @param array $aUrlArray Array of urls of css or js files for aggregation * @param string $sType css or js * @return string Aggregated (and possibly minified) contents of files */ public function getContents($aUrlArray, $sType, $oParser) { $oCssParser = new JchOptimizeCssParser($oParser, $this->bBackend); $sCriticalCss = ''; $aSpriteCss = array(); $aFontFace = array(); $aContentsArray = array(); foreach ($aUrlArray as $index => $aUrlInnerArray) { $sContents = $this->combineFiles($aUrlInnerArray, $sType, $oCssParser); $sContents = $this->prepareContents($sContents); $aContentsArray[$index] = $sContents; } if ($sType == 'css') { if ($this->params->get('csg_enable', 0)) { try { $oSpriteGenerator = new JchOptimizeSpriteGenerator($this->params); $aSpriteCss = $oSpriteGenerator->getSprite($this->{$sType}); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); $aSpriteCss = array(); } } if ($this->params->get('pro_optimizeCssDelivery', '0')) { if (!empty($aSpriteCss)) { $this->{$sType} = str_replace($aSpriteCss['needles'], $aSpriteCss['replacements'], $this->{$sType}); } $oParser->params->set('pro_InlineScripts', '0'); $oParser->params->set('pro_InlineStyles', '0'); $sHtml = $oParser->cleanHtml(); $aCssContents = $oCssParser->optimizeCssDelivery($this->{$sType}, $sHtml); $sCriticalCss .= $aCssContents['criticalcss']; $aFontFace = preg_split('#}\\K[^@]*+#', $aCssContents['font-face'], -1, PREG_SPLIT_NO_EMPTY); } } $aContents = array('filemtime' => JchPlatformUtility::unixCurrentDate(), 'file' => $aContentsArray, 'criticalcss' => $sCriticalCss, 'spritecss' => $aSpriteCss, 'font-face' => $aFontFace); return $aContents; }
/** * Get aggregated and possibly minified content from js and css files * * @param array $aUrlArray Array of urls of css or js files for aggregation * @param string $sType css or js * @return string Aggregated (and possibly minified) contents of files */ public function getContents($aUrlArray, $sType, $oParser) { JCH_DEBUG ? JchPlatformProfiler::start('GetContents - ' . $sType, TRUE) : null; $oCssParser = $this->oCssParser; $sCriticalCss = ''; $aSpriteCss = array(); $aFontFace = array(); $aContentsArray = array(); foreach ($aUrlArray as $index => $aUrlInnerArray) { $sContents = $this->combineFiles($aUrlInnerArray, $sType, $oCssParser); $sContents = $this->prepareContents($sContents); $aContentsArray[$index] = $sContents; } if ($sType == 'css') { if ($this->params->get('csg_enable', 0)) { try { $oSpriteGenerator = new JchOptimizeSpriteGenerator($this->params); $aSpriteCss = $oSpriteGenerator->getSprite($this->{$sType}); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); $aSpriteCss = array(); } } if ($this->params->get('pro_optimizeCssDelivery', '0')) { if (is_array($aSpriteCss) && !empty($aSpriteCss) && isset($aSpriteCss['needles']) && isset($aSpriteCss['replacements'])) { $this->{$sType} = str_replace($aSpriteCss['needles'], $aSpriteCss['replacements'], $this->{$sType}); } $oParser->params->set('pro_InlineScripts', '1'); $oParser->params->set('pro_InlineStyles', '1'); $sHtml = $oParser->cleanHtml(); $aCssContents = $oCssParser->optimizeCssDelivery($this->{$sType}, $sHtml); $sCriticalCss .= $oCssParser->sortImports($aCssContents['criticalcss']); $sCriticalCss = $sCriticalCss; $aFontFace = preg_split('#}\\K[^@]*+#', $aCssContents['font-face'], -1, PREG_SPLIT_NO_EMPTY); } } try { $oAdmin = new JchOptimizeAdmin($this->params); $oAdmin->getAdminLinks($oParser, JchPlatformUtility::menuId(), $this->css); } catch (Exception $ex) { JchOptimizeLogger::log($ex->getMessage(), $this->params); } $aContents = array('filemtime' => JchPlatformUtility::unixCurrentDate(), 'file' => $aContentsArray, 'criticalcss' => $sCriticalCss, 'spritecss' => $aSpriteCss, 'font-face' => $aFontFace); JCH_DEBUG ? JchPlatformProfiler::stop('GetContents - ' . $sType) : null; return $aContents; }
/** * Remove placeholders from aggregated file for caching * * @param string $sContents Aggregated file contents * @param string $sType js or css * @return string */ public function prepareContents($sContents, $sType, $oCssParser) { if ($sType == 'css') { $sContents = $oCssParser->sortImports($sContents); if ($this->params->get('csg_enable', 0)) { try { $oSpriteGenerator = new JchOptimizeSpriteGenerator($this->params); $sCssContents = $oSpriteGenerator->getSprite($sContents); } catch (Exception $ex) { if ($this->params->get('log', 0)) { JchOptimizeLogger::log($ex->getMessage()); } $sCssContents = $sContents; } $sContents = $sCssContents; } if (function_exists('mb_convert_encoding')) { $sContents = '@charset "utf-8";' . $this->sLnEnd . trim($sContents); $sContents = mb_convert_encoding($sContents, 'utf-8'); } } $sContents = str_replace(array('COMMENT_START', 'COMMENT_IMPORT_START', 'COMMENT_END', 'DELIMITER'), array($this->sLnEnd . $this->sLnEnd . '/***! ', $this->sLnEnd . $this->sLnEnd . '/***! @import url', ' !***/' . $this->sLnEnd . $this->sLnEnd, ''), $sContents); return trim($sContents); }