Beispiel #1
0
 /**
  * @param $string
  * @param null $document
  * @return mixed
  * @throws \Exception
  */
 public static function embedAndModifyCss($string, $document = null)
 {
     if ($document && $document instanceof Model\Document == false) {
         throw new \Exception('$document has to be an instance of Document');
     }
     //matches all <link> Tags
     preg_match_all("@<link.*?href\\s*=\\s*[\"']([^http].*?)[\"'].*?(/?>|</\\s*link>)@is", $string, $matches);
     if (!empty($matches[0])) {
         $css = "";
         foreach ($matches[0] as $key => $value) {
             $fullMatch = $matches[0][$key];
             $path = $matches[1][$key];
             $fileInfo = self::getNormalizedFileInfo($path, $document);
             if (in_array($fileInfo['fileExtension'], array('css', 'less'))) {
                 if (is_readable($fileInfo['filePathNormalized'])) {
                     if ($fileInfo['fileExtension'] == 'css') {
                         $fileContent = file_get_contents($fileInfo['filePathNormalized']);
                     } else {
                         $fileContent = \Pimcore\Tool\Less::compile($fileInfo['filePathNormalized']);
                         $fileContent = str_replace('/**** compiled with lessphp ****/', '', $fileContent);
                     }
                     if ($fileContent) {
                         $fileContent = self::normalizeCssContent($fileContent, $fileInfo);
                         $css .= "\n\n\n";
                         $css .= $fileContent;
                         // remove <link> tag
                         $string = str_replace($fullMatch, '', $string);
                     }
                 }
             }
         }
         $autoloader = \Zend_Loader_Autoloader::getInstance();
         $autoloader->registerNamespace('TijsVerkoyen');
         $cssToInlineStyles = new CssToInlineStyles();
         $cssToInlineStyles->setHTML($string);
         $cssToInlineStyles->setCSS($css);
         $string = $cssToInlineStyles->convert();
     }
     return $string;
 }
Beispiel #2
0
 /**
  * @param $string
  * @param null $document
  * @return mixed
  * @throws \Exception
  */
 public static function embedAndModifyCss($string, $document = null)
 {
     if ($document && $document instanceof Model\Document == false) {
         throw new \Exception('$document has to be an instance of Document');
     }
     //matches all <link> Tags
     preg_match_all("@<link.*?href\\s*=\\s*[\"'](.*?)[\"'].*?(/?>|</\\s*link>)@is", $string, $matches);
     if (!empty($matches[0])) {
         $css = "";
         foreach ($matches[0] as $key => $value) {
             $fullMatch = $matches[0][$key];
             $path = $matches[1][$key];
             $fileContent = "";
             $fileInfo = [];
             if (stream_is_local($path)) {
                 $fileInfo = self::getNormalizedFileInfo($path, $document);
                 if (in_array($fileInfo['fileExtension'], ['css', 'less'])) {
                     if (is_readable($fileInfo['filePathNormalized'])) {
                         if ($fileInfo['fileExtension'] == 'css') {
                             $fileContent = file_get_contents($fileInfo['filePathNormalized']);
                         } else {
                             $fileContent = \Pimcore\Tool\Less::compile($fileInfo['filePathNormalized']);
                             $fileContent = str_replace('/**** compiled with lessphp ****/', '', $fileContent);
                         }
                     }
                 }
             } elseif (strpos($path, "http") === 0) {
                 $fileContent = \Pimcore\Tool::getHttpData($path);
                 $fileInfo = ["fileUrlNormalized" => $path];
             }
             if ($fileContent) {
                 $fileContent = self::normalizeCssContent($fileContent, $fileInfo);
                 $css .= "\n\n\n";
                 $css .= $fileContent;
                 // remove <link> tag
                 $string = str_replace($fullMatch, '', $string);
             }
         }
         $cssToInlineStyles = new CssToInlineStyles();
         $cssToInlineStyles->setHTML($string);
         $cssToInlineStyles->setCSS($css);
         $string = $cssToInlineStyles->convert();
     }
     return $string;
 }