Example #1
0
 /**
  * Add line numbers in C-style comments
  *
  * This uses a very basic parser easily fooled by comment tokens inside
  * strings or regexes, but, otherwise, generally clean code will not be 
  * mangled. URI rewriting can also be performed.
  *
  * @param string $content
  * 
  * @param array $options available options:
  * 
  * 'id': (optional) string to identify file. E.g. file name/path
  *
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files, and prepend a comment with debugging information about
  * this process.
  * 
  * @return string 
  */
 public static function minify($content, $options = array())
 {
     $id = isset($options['id']) && $options['id'] ? $options['id'] : '';
     $content = str_replace("\r\n", "\n", $content);
     $lines = explode("\n", $content);
     $numLines = count($lines);
     // determine left padding
     $padTo = strlen($numLines);
     $inComment = false;
     $i = 0;
     $newLines = array();
     while (null !== ($line = array_shift($lines))) {
         if ('' !== $id && 0 == $i % 50) {
             array_push($newLines, '', "/* {$id} */", '');
         }
         ++$i;
         $newLines[] = self::_addNote($line, $i, $inComment, $padTo);
         $inComment = self::_eolInComment($line, $inComment);
     }
     $content = implode("\n", $newLines) . "\n";
     // check for desired URI rewriting
     if (isset($options['currentDir'])) {
         #require_once 'Diglin/Minify/CSS/UriRewriter.php';
         Diglin_Minify_CSS_UriRewriter::$debugText = '';
         $content = Diglin_Minify_CSS_UriRewriter::rewrite($content, $options['currentDir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array());
         $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n" . Diglin_Minify_CSS_UriRewriter::$debugText . "*/\n" . $content;
     }
     return $content;
 }
Example #2
0
 /**
  * Prepend a path to relative URIs in CSS files
  * 
  * @param string $css
  * 
  * @param string $path The path to prepend.
  * 
  * @return string
  */
 public static function prepend($css, $path)
 {
     self::$_prependPath = $path;
     $css = self::_trimUrls($css);
     // append
     $css = preg_replace_callback('/@import\\s+([\'"])(.*?)[\'"]/', array(self::$className, '_processUriCB'), $css);
     $css = preg_replace_callback('/url\\(\\s*([^\\)\\s]+)\\s*\\)/', array(self::$className, '_processUriCB'), $css);
     self::$_prependPath = null;
     return $css;
 }
Example #3
0
 /**
  * Minify a CSS string
  * 
  * @param string $css
  * 
  * @param array $options available options:
  * 
  * 'preserveComments': (default true) multi-line comments that begin
  * with "/*!" will be preserved with newlines before and after to
  * enhance readability.
  * 
  * 'prependRelativePath': (default null) if given, this string will be
  * prepended to all relative URIs in import/url declarations
  * 
  * 'currentDir': (default null) if given, this is assumed to be the
  * directory of the current CSS file. Using this, minify will rewrite
  * all relative URIs in import/url declarations to correctly point to
  * the desired files. For this to work, the files *must* exist and be
  * visible by the PHP process.
  *
  * 'symlinks': (default = array()) If the CSS file is stored in 
  * a symlink-ed directory, provide an array of link paths to
  * target paths, where the link paths are within the document root. Because 
  * paths need to be normalized for this to work, use "//" to substitute 
  * the doc root in the link paths (the array keys). E.g.:
  * <code>
  * array('//symlink' => '/real/target/path') // unix
  * array('//static' => 'D:\\staticStorage')  // Windows
  * </code>
  * 
  * @return string
  */
 public static function minify($css, $options = array())
 {
     #require_once 'Diglin/Minify/CSS/Compressor.php';
     if (isset($options['preserveComments']) && !$options['preserveComments']) {
         $css = Diglin_Minify_CSS_Compressor::process($css, $options);
     } else {
         #require_once 'Diglin/Minify/CommentPreserver.php';
         $css = Diglin_Minify_CommentPreserver::process($css, array('Diglin_Minify_CSS_Compressor', 'process'), array($options));
     }
     if (!isset($options['currentDir']) && !isset($options['prependRelativePath'])) {
         return $css;
     }
     #require_once 'Diglin/Minify/CSS/UriRewriter.php';
     if (isset($options['currentDir'])) {
         return Diglin_Minify_CSS_UriRewriter::rewrite($css, $options['currentDir'], isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT'], isset($options['symlinks']) ? $options['symlinks'] : array());
     } else {
         return Diglin_Minify_CSS_UriRewriter::prepend($css, $options['prependRelativePath']);
     }
 }