Ejemplo n.º 1
0
 /**
  * Combine all files and retrieve minified file
  *
  * @param string|int $indent
  *            Amount of whitespaces or string to use for indention
  * @return string
  */
 public function toString($indent = null)
 {
     try {
         if ($this->minifyEnabled === false) {
             return parent::toString($indent);
         }
         $indent = null !== $indent ? $this->getWhitespace($indent) : $this->getIndent();
         $filesToMinify = array();
         $lastModifiedTime = 0;
         $items = [];
         $this->getContainer()->ksort();
         foreach ($this as $item) {
             if (!$this->isValid($item)) {
                 continue;
             }
             $itemSrcPath = !empty($item->href) ? $this->minifyDocRootPath . trim($item->href, '/\\ ') : null;
             if ($item->type === 'text/css' && $itemSrcPath && file_exists($itemSrcPath) && empty($item->conditionalStylesheet) && (!isset($item->attributes['minify']) || $item->attributes['minify'] !== false)) {
                 $filesToMinify[$item->media][] = $itemSrcPath;
                 $lastModifiedTime = max(filemtime($itemSrcPath), $lastModifiedTime);
             } else {
                 $items[] = $this->itemToString($item);
             }
         }
         if (count($filesToMinify, COUNT_RECURSIVE) > 0) {
             foreach ($filesToMinify as $media => $filePaths) {
                 $minifiedFileName = md5(implode('|', $filePaths)) . '.min.css';
                 $minifiedFileBasePath = $this->view->basePath($this->minifyCacheDir . '/' . $minifiedFileName);
                 $minifiedFilePath = $this->minifyDocRootPath . trim($minifiedFileBasePath, '\\/ ');
                 $lockFilePath = sys_get_temp_dir() . '/' . $minifiedFileName . '.lock';
                 $isMinifiedFileBuildRequired = !file_exists($minifiedFilePath) || filemtime($minifiedFilePath) < $lastModifiedTime;
                 $isMinifiedFileBuildLocked = file_exists($lockFilePath);
                 if ($isMinifiedFileBuildRequired && !$isMinifiedFileBuildLocked) {
                     file_put_contents($lockFilePath, 'locked', LOCK_EX);
                     try {
                         $pieces = array();
                         foreach ($filePaths as $filePath) {
                             $pieces[] = $this->minifyService->minify(file_get_contents($filePath), array('docRoot' => $this->minifyDocRootPath, 'currentDir' => dirname($filePath)));
                         }
                         $content = implode($this->getSeparator(), $pieces);
                         file_put_contents($minifiedFilePath, $content, LOCK_EX);
                     } catch (\Exception $e) {
                         unlink($lockFilePath);
                         throw new \Exception($e->getMessage());
                     }
                     unlink($lockFilePath);
                     //clean out old files
                     $flattened = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->minifyCachePath));
                     $files = new \RegexIterator($flattened, '/^[a-f0-9]{32}\\.min\\.css$/i');
                     foreach ($files as $file) {
                         if (filemtime($file) < time() - 86400 * 7) {
                             unlink($file);
                         }
                     }
                 }
                 $item = $this->createData(array('type' => 'text/css', 'rel' => 'stylesheet', 'media' => $media, 'href' => $minifiedFileBasePath, 'conditionalStylesheet' => false));
                 array_unshift($items, $this->itemToString($item));
             }
         }
         return $indent . implode($this->escape($this->getSeparator()) . $indent, $items);
     } catch (\Exception $e) {
         die("Fatal VcoZfMinify Error: " . $e->getMessage());
     }
 }
Ejemplo n.º 2
0
 /**
  * Combine all files and retrieve minified file
  *
  * @param string|int $indent
  *            Amount of whitespaces or string to use for indention
  * @return string
  */
 public function toString($indent = null)
 {
     try {
         if ($this->minifyEnabled === false) {
             return parent::toString($indent);
         }
         $indent = null !== $indent ? $this->getWhitespace($indent) : $this->getIndent();
         if ($this->view) {
             $useCdata = $this->view->plugin('doctype')->isXhtml();
         } else {
             $useCdata = $this->useCdata;
         }
         $escapeStart = $useCdata ? '//<![CDATA[' : '//<!--';
         $escapeEnd = $useCdata ? '//]]>' : '//-->';
         $filesToMinify = array();
         $lastModifiedTime = 0;
         $items = [];
         $this->getContainer()->ksort();
         foreach ($this as $item) {
             if (!$this->isValid($item)) {
                 continue;
             }
             $itemSrcPath = !empty($item->attributes) && !empty($item->attributes['src']) ? $this->minifyDocRootPath . trim($item->attributes['src'], '/\\ ') : null;
             if ($item->type === 'text/javascript' && $itemSrcPath && file_exists($itemSrcPath) && empty($item->attributes['conditional']) && (!isset($item->attributes['minify']) || $item->attributes['minify'] !== false)) {
                 $filesToMinify[] = $itemSrcPath;
                 $lastModifiedTime = max(filemtime($itemSrcPath), $lastModifiedTime);
             } else {
                 if (isset($item->attributes['minify'])) {
                     unset($item->attributes['minify']);
                 }
                 $items[] = $this->itemToString($item, $indent, $escapeStart, $escapeEnd);
             }
         }
         if (count($filesToMinify) > 0) {
             $minifiedFileName = md5(implode('|', $filesToMinify)) . '.min.js';
             $minifiedFileBasePath = $this->view->basePath($this->minifyCacheDir . '/' . $minifiedFileName);
             $minifiedFilePath = $this->minifyDocRootPath . trim($minifiedFileBasePath, '\\/ ');
             $lockFilePath = sys_get_temp_dir() . '/' . $minifiedFileName . '.lock';
             $isMinifiedFileBuildRequired = !file_exists($minifiedFilePath) || filemtime($minifiedFilePath) < $lastModifiedTime;
             $isMinifiedFileBuildLocked = file_exists($lockFilePath);
             if ($isMinifiedFileBuildRequired && !$isMinifiedFileBuildLocked) {
                 file_put_contents($lockFilePath, 'locked', LOCK_EX);
                 try {
                     $pieces = array();
                     foreach ($filesToMinify as $filePath) {
                         $pieces[] = file_get_contents($filePath);
                     }
                     $content = implode($this->getSeparator(), $pieces);
                     $content = $this->minifyService->minify($content);
                     file_put_contents($minifiedFilePath, $content, LOCK_EX);
                 } catch (\Exception $e) {
                     unlink($lockFilePath);
                     throw new \Exception($e->getMessage());
                 }
                 unlink($lockFilePath);
                 //clean out old files
                 $flattened = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->minifyCachePath));
                 $files = new \RegexIterator($flattened, '/^[a-f0-9]{32}\\.min\\.js$/i');
                 foreach ($files as $file) {
                     if (filemtime($file) < time() - 86400 * 7) {
                         unlink($file);
                     }
                 }
             }
             $attributes = array('src' => $minifiedFileBasePath);
             if (isset($this->minifyConfig['minifyJS']['async']) && $this->minifyConfig['minifyJS']['async'] === true) {
                 $attributes['async'] = true;
             }
             $item = $this->createData('text/javascript', $attributes);
             array_unshift($items, $this->itemToString($item, $indent, $escapeStart, $escapeEnd));
         }
         return implode($this->getSeparator(), $items);
     } catch (\Exception $e) {
         die("Fatal VcoZfMinify Error: " . $e->getMessage());
     }
 }