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 first "!" will be removed from the
34 * preserved comments, and the comments will be surrounded by
35 * Minify_CommentPreserver::$preprend and Minify_CommentPreserver::$append.
37 * @param string $content
38 * @param callback $processor function
39 * @param array $args array of extra arguments to pass to the processor
40 * function (default = array())
43 public static function process($content, $processor, $args = array())
47 list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
48 if ('' !== $beforeComment) {
50 array_unshift($callArgs, $beforeComment);
51 $ret .= call_user_func_array($processor, $callArgs);
53 if (false === $comment) {
57 $content = $afterComment;
63 * Extract comments that YUI Compressor preserves.
65 * @param string $in input
67 * @return array 3 elements are returned. If a YUI comment is found, the
68 * 2nd element is the comment and the 1st and 2nd are the surrounding
69 * strings. If no comment is found, the entire string is returned as the
70 * 1st element and the other two are false.
72 private static function _nextComment($in)
75 false === ($start = strpos($in, '/*!'))
76 || false === ($end = strpos($in, '*/', $start + 3))
78 return array($in, false, false);
81 substr($in, 0, $start)
82 ,self::$prepend . '/*' . substr($in, $start + 3, $end - $start - 1) . self::$append
84 $endChars = (strlen($in) - $end - 2);
85 $ret[] = (0 === $endChars)
87 : substr($in, -$endChars);