3 * Class Minify_CommentPreserver
8 * Process a string in pieces preserving C-style comments that begin with "/*!"
11 * @author Stephen Clay <steve@mrclay.org>
13 class Minify_CommentPreserver {
16 * String to be prepended to each preserved comment
20 public static $prepend = "\n";
23 * String to be appended to each preserved comment
27 public static $append = "\n";
30 * Process a string outside of C-style comments that begin with "/*!"
32 * On each non-empty string outside these comments, the given processor
33 * function will be called. The comments will be surrounded by
34 * Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
36 * @param string $content
37 * @param callback $processor function
38 * @param array $args array of extra arguments to pass to the processor
39 * function (default = array())
42 public static function process($content, $processor, $args = array())
46 list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
47 if ('' !== $beforeComment) {
49 array_unshift($callArgs, $beforeComment);
50 $ret .= call_user_func_array($processor, $callArgs);
52 if (false === $comment) {
56 $content = $afterComment;
62 * Extract comments that YUI Compressor preserves.
64 * @param string $in input
66 * @return array 3 elements are returned. If a YUI comment is found, the
67 * 2nd element is the comment and the 1st and 3rd are the surrounding
68 * strings. If no comment is found, the entire string is returned as the
69 * 1st element and the other two are false.
71 private static function _nextComment($in)
74 false === ($start = strpos($in, '/*!'))
75 || false === ($end = strpos($in, '*/', $start + 3))
77 return array($in, false, false);
80 substr($in, 0, $start)
81 ,self::$prepend . '/*!' . substr($in, $start + 3, $end - $start - 1) . self::$append
83 $endChars = (strlen($in) - $end - 2);
84 $ret[] = (0 === $endChars)
86 : substr($in, -$endChars);