Ejemplo n.º 1
0
 /**
  * Aggregate contents of CSS and JS files
  *
  * @param array $aUrlArray      Array of links of files
  * @param string $sType          CSS or js
  * @return string               Aggregarted contents
  * @throws Exception
  */
 public function combineFiles($aUrlArray, $sType, $oCssParser, $bCssAsync = FALSE)
 {
     global $_PROFILER;
     JCH_DEBUG ? $_PROFILER->mark('beforeCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null;
     $sContents = '';
     $bAsync = false;
     $sAsyncUrl = '';
     $oFileRetriever = JchOptimizeFileRetriever::getInstance($this->params);
     foreach ($aUrlArray as $aUrl) {
         $sContent = '';
         if (isset($aUrl['url'])) {
             if ($sType == 'js' && $sAsyncUrl != '') {
                 $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){});  DELIMITER';
                 $sAsyncUrl = '';
             }
             $sPath = $aUrl['path'];
             if ($sType == 'js' && $this->loadAsync($aUrl['url'])) {
                 $sAsyncUrl = $aUrl['url'];
                 $bAsync = true;
                 continue;
             } else {
                 if (!JchOptimizeHelper::fileExists($sPath)) {
                     if ($this->params->get('log', 0)) {
                         JchOptimizeLogger::log(JText::_('File not found: ' . $sPath));
                     }
                     $sFileContents = 'COMMENT_START File does not exist COMMENT_END';
                 } else {
                     JCH_DEBUG ? $_PROFILER->mark('beforegetFileContents - ' . $sPath . ' plgSystem (JCH Optimize)') : null;
                     $sFileContents = $oFileRetriever->getFileContents($sPath);
                     JCH_DEBUG ? $_PROFILER->mark('afterGetFileContents - ' . $sPath . ' plgSystem (JCH Optimize)') : null;
                 }
                 if ($sFileContents === FALSE) {
                     throw new Exception(JText::_('Failed getting file contents from ' . $sPath));
                 }
                 $sContent .= $sFileContents;
                 unset($sFileContents);
             }
         } else {
             if ($sType == 'js' && $sAsyncUrl != '') {
                 $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){' . $this->sLnEnd . $aUrl['content'] . $this->sLnEnd . '});  
                                     DELIMITER';
                 $sAsyncUrl = '';
             } else {
                 $sContent .= $aUrl['content'];
             }
         }
         if ($sType == 'css') {
             unset($oCssParser->sCssUrl);
             $oCssParser->aUrl = $aUrl;
             $sImportContent = preg_replace('#@import\\s(?:url\\()?[\'"]([^\'"]+)[\'"](?:\\))?#', '@import url($1)', $sContent);
             if (is_null($sImportContent)) {
                 if ($this->params->get('log', 0)) {
                     JchOptimizeLogger::log(JText::_('Error occured trying to parse for @imports in ' . $aUrl['url']));
                 }
                 $sImportContent = $sContent;
             }
             $sContent = $sImportContent;
             $sContent = $oCssParser->correctUrl($sContent);
             $sContent = $this->replaceImports($sContent, $aUrl);
             $sContent = $oCssParser->handleMediaQueries($sContent);
         }
         if ($sType == 'js' && $sContent != '') {
             $sContent = $this->addSemiColon($sContent);
         }
         $sContent = $this->MinifyContent($sContent, $sType, $aUrl);
         $sContents .= $this->addCommentedUrl($sType, $aUrl) . $sContent . 'DELIMITER';
         unset($sContent);
     }
     if ($bAsync) {
         $sContents = $this->getLoadScript() . $sContents;
         if ($sAsyncUrl != '') {
             $sContents .= $this->addCommentedUrl('js', $sAsyncUrl) . 'loadScript("' . $sAsyncUrl . '", function(){});  DELIMITER';
             $sAsyncUrl = '';
         }
     }
     JCH_DEBUG ? $_PROFILER->mark('afterCombineFiles - ' . $sType . ' plgSystem (JCH Optimize)') : null;
     return $sContents;
 }