]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/CommentPreserver.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / min / lib / Minify / CommentPreserver.php
1 <?php
2 /**
3  * Class Minify_CommentPreserver 
4  * @package Minify
5  */
6
7 /**
8  * Process a string in pieces preserving C-style comments that begin with "/*!"
9  * 
10  * @package Minify
11  * @author Stephen Clay <steve@mrclay.org>
12  */
13 class Minify_CommentPreserver {
14     
15     /**
16      * String to be prepended to each preserved comment
17      *
18      * @var string
19      */
20     public static $prepend = "\n";
21     
22     /**
23      * String to be appended to each preserved comment
24      *
25      * @var string
26      */
27     public static $append = "\n";
28     
29     /**
30      * Process a string outside of C-style comments that begin with "/*!"
31      *
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.
36      * 
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())
41      * @return string
42      */
43     public static function process($content, $processor, $args = array())
44     {
45         $ret = '';
46         while (true) {
47             list($beforeComment, $comment, $afterComment) = self::_nextComment($content);
48             if ('' !== $beforeComment) {
49                 $callArgs = $args;
50                 array_unshift($callArgs, $beforeComment);
51                 $ret .= call_user_func_array($processor, $callArgs);    
52             }
53             if (false === $comment) {
54                 break;
55             }
56             $ret .= $comment;
57             $content = $afterComment;
58         }
59         return $ret;
60     }
61     
62     /**
63      * Extract comments that YUI Compressor preserves.
64      * 
65      * @param string $in input
66      * 
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.
71      */
72     private static function _nextComment($in)
73     {
74         if (
75             false === ($start = strpos($in, '/*!'))
76             || false === ($end = strpos($in, '*/', $start + 3))
77         ) {
78             return array($in, false, false);
79         }
80         $ret = array(
81             substr($in, 0, $start)
82             ,self::$prepend . '/*' . substr($in, $start + 3, $end - $start - 1) . self::$append
83         );
84         $endChars = (strlen($in) - $end - 2);
85         $ret[] = (0 === $endChars)
86             ? ''
87             : substr($in, -$endChars);
88         return $ret;
89     }
90 }