Example #1
0
 public static function minifyCss($cssList, $concat = false)
 {
     $websiteHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('website');
     $cacheHelper = Zend_Controller_Action_HelperBroker::getExistingHelper('cache');
     if (null === ($hashStack = $cacheHelper->load(strtolower(__CLASS__), ''))) {
         $hashStack = array();
     }
     $container = $cssList->getContainer();
     foreach ($container->getArrayCopy() as $css) {
         if (preg_match('/^https?:\\/\\//', $css->href) != false && strpos($css->href, $websiteHelper->getUrl()) !== 0) {
             continue;
         }
         $path = str_replace($websiteHelper->getUrl(), '', $css->href);
         if (!is_file($websiteHelper->getPath() . $path) || !file_exists($websiteHelper->getPath() . $path)) {
             continue;
         }
         $hash = sha1_file($websiteHelper->getPath() . $path);
         $name = Tools_Filesystem_Tools::basename($path);
         if (!$hash) {
             continue;
         }
         if (!isset($hashStack[$path]) || $hashStack[$path]['hash'] !== $hash) {
             $compressor = new CssMin();
             $cssContent = Tools_Filesystem_Tools::getFile($path);
             $cssContent = preg_replace('/url\\([\'"]?([^)\'"]*)[\'"]?\\)/', 'url("../' . dirname($path) . DIRECTORY_SEPARATOR . '${1}")', $cssContent);
             $hashStack[$path] = array('hash' => $hash, 'content' => $compressor->run($cssContent));
             Tools_Filesystem_Tools::saveFile($websiteHelper->getPath() . $websiteHelper->getTmp() . $hash . '.css', $hashStack[$path]['content']);
             unset($cssContent);
         }
         if (!$concat) {
             $css->href = $websiteHelper->getUrl() . $websiteHelper->getTmp() . $hash . '.css?' . $name;
         } else {
             $concatCss = isset($concatCss) ? $concatCss . PHP_EOL . "/* {$path} */" . PHP_EOL . $hashStack[$path]['content'] : "/* {$path} */" . PHP_EOL . $hashStack[$path]['content'];
         }
     }
     if (isset($concatCss) && !empty($concatCss)) {
         $cname = sha1($concatCss) . '.concat.min.css';
         $concatPath = $websiteHelper->getPath() . $websiteHelper->getTmp() . $cname;
         if (!file_exists($concatPath) || sha1_file($concatPath) !== sha1($concatCss)) {
             Tools_Filesystem_Tools::saveFile($concatPath, $concatCss);
         }
         $cssList->setStylesheet($websiteHelper->getUrl() . $websiteHelper->getTmp() . $cname);
     }
     $cacheHelper->save(strtolower(__CLASS__), $hashStack, '', array(), Helpers_Action_Cache::CACHE_LONG);
     return $cssList;
 }
Example #2
0
 private function _getContent()
 {
     $content = '';
     $compressor = new CssMin();
     foreach ($this->_getFilesCss() as $file) {
         if (!file_exists($this->_themeFullPath . $file)) {
             continue;
         }
         $fileName = explode('/', $this->_themeFullPath . $file);
         $fileName = strtoupper(end($fileName));
         $content .= "/**** " . $fileName . " start ****/\n";
         $content .= $compressor->run(preg_replace('~\\@charset\\s\\"utf-8\\"\\;~Ui', '', file_get_contents($this->_themeFullPath . $file)));
         $content .= "\n/**** " . $fileName . " end ****/\n";
     }
     return $content;
 }