Jarves\Utils::compressCss PHP Method

compressCss() public method

public compressCss ( array $files, string $includePath = '' ) : string
$files array
$includePath string The directory where to compressed css is. with trailing slash!
return string
    public function compressCss(array $files, $includePath = '')
    {
        $webDir = realpath($this->jarves->getRootDir() . '/../web') . '/';
        $content = '';
        foreach ($files as $assetPath) {
            $cssFile = $this->jarves->resolvePublicWebPath($assetPath);
            //bundles/jarves/css/style.css
            $cssDir = dirname($cssFile) . '/';
            //admin/css/...
            $cssDir = str_repeat('../', substr_count($includePath, '/')) . $cssDir;
            $content .= "\n\n/* file: {$assetPath} */\n\n";
            if (file_exists($file = $webDir . $cssFile)) {
                $h = fopen($file, "r");
                if ($h) {
                    while (!feof($h) && $h) {
                        $buffer = fgets($h, 4096);
                        $buffer = preg_replace('/@import \'(?!.*:\\/\\/)([^\\/].*)\'/', '@import \'' . $cssDir . '$1\'', $buffer);
                        $buffer = preg_replace('/@import "(?!.*:\\/\\/)([^\\/].*)"/', '@import "' . $cssDir . '$1"', $buffer);
                        $buffer = preg_replace('/url\\(\'(?!.*:\\/\\/)([^\\/][^\\)]*)\'\\)/', 'url(\'' . $cssDir . '$1\')', $buffer);
                        $buffer = preg_replace('/url\\(\\"(?!.*:\\/\\/)([^\\/][^\\)]*)\\"\\)/', 'url(\\"' . $cssDir . '$1\\")', $buffer);
                        $buffer = preg_replace('/url\\((?!.*data:image)(?!.*:\\/\\/)([^\\/\'].*)\\)/', 'url(' . $cssDir . '$1)', $buffer);
                        $buffer = str_replace(array('  ', '    ', "\t", "\n", "\r"), '', $buffer);
                        $buffer = str_replace(': ', ':', $buffer);
                        $content .= $buffer;
                    }
                    fclose($h);
                }
            } else {
                $content .= '/* => `' . $cssFile . '` not exist. */';
            }
        }
        return $content;
    }

Usage Example

Esempio n. 1
0
 /**
  * @param array $files
  *
  * @return AssetInfo
  */
 public function compressFiles(array $files)
 {
     $md5String = '';
     foreach ($files as $file) {
         $path = $this->getAssetPath($file);
         $md5String .= '.' . filemtime($path);
     }
     $fileUpToDate = false;
     $md5Line = '/* ' . md5($md5String) . " */\n";
     $oFile = 'cache/compressed-css/' . md5($md5String) . '.css';
     $handle = @fopen($this->getJarves()->getRootDir() . '/../web/' . $oFile, 'r');
     if ($handle) {
         $line = fgets($handle);
         fclose($handle);
         if ($line == $md5Line) {
             $fileUpToDate = true;
         }
     }
     if (!$fileUpToDate) {
         $content = $this->utils->compressCss($files, 'cache/compressed-css/');
         $content = $md5Line . $content;
         $this->webFilesystem->write($oFile, $content);
     }
     $assetInfo = new AssetInfo();
     $assetInfo->setPath($oFile);
     return $assetInfo;
 }