Lines.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <?php
  2. /**
  3. * Class Minify_Lines
  4. * @package Minify
  5. */
  6. /**
  7. * Add line numbers in C-style comments for easier debugging of combined content
  8. *
  9. * @package Minify
  10. * @author Stephen Clay <steve@mrclay.org>
  11. * @author Adam Pedersen (Issue 55 fix)
  12. */
  13. class Minify_Lines {
  14. /**
  15. * Add line numbers in C-style comments
  16. *
  17. * This uses a very basic parser easily fooled by comment tokens inside
  18. * strings or regexes, but, otherwise, generally clean code will not be
  19. * mangled. URI rewriting can also be performed.
  20. *
  21. * @param string $content
  22. *
  23. * @param array $options available options:
  24. *
  25. * 'id': (optional) string to identify file. E.g. file name/path
  26. *
  27. * 'currentDir': (default null) if given, this is assumed to be the
  28. * directory of the current CSS file. Using this, minify will rewrite
  29. * all relative URIs in import/url declarations to correctly point to
  30. * the desired files, and prepend a comment with debugging information about
  31. * this process.
  32. *
  33. * @return string
  34. */
  35. public static function minify($content, $options = array())
  36. {
  37. $id = (isset($options['id']) && $options['id'])
  38. ? $options['id']
  39. : '';
  40. $content = str_replace("\r\n", "\n", $content);
  41. // Hackily rewrite strings with XPath expressions that are
  42. // likely to throw off our dumb parser (for Prototype 1.6.1).
  43. $content = str_replace('"/*"', '"/"+"*"', $content);
  44. $content = preg_replace('@([\'"])(\\.?//?)\\*@', '$1$2$1+$1*', $content);
  45. $lines = explode("\n", $content);
  46. $numLines = count($lines);
  47. // determine left padding
  48. $padTo = strlen((string) $numLines); // e.g. 103 lines = 3 digits
  49. $inComment = false;
  50. $i = 0;
  51. $newLines = array();
  52. while (null !== ($line = array_shift($lines))) {
  53. if (('' !== $id) && (0 == $i % 50)) {
  54. array_push($newLines, '', "/* {$id} */", '');
  55. }
  56. ++$i;
  57. $newLines[] = self::_addNote($line, $i, $inComment, $padTo);
  58. $inComment = self::_eolInComment($line, $inComment);
  59. }
  60. $content = implode("\n", $newLines) . "\n";
  61. // check for desired URI rewriting
  62. if (isset($options['currentDir'])) {
  63. require_once 'Minify/CSS/UriRewriter.php';
  64. Minify_CSS_UriRewriter::$debugText = '';
  65. $content = Minify_CSS_UriRewriter::rewrite(
  66. $content
  67. ,$options['currentDir']
  68. ,isset($options['docRoot']) ? $options['docRoot'] : $_SERVER['DOCUMENT_ROOT']
  69. ,isset($options['symlinks']) ? $options['symlinks'] : array()
  70. );
  71. $content = "/* Minify_CSS_UriRewriter::\$debugText\n\n"
  72. . Minify_CSS_UriRewriter::$debugText . "*/\n"
  73. . $content;
  74. }
  75. return $content;
  76. }
  77. /**
  78. * Is the parser within a C-style comment at the end of this line?
  79. *
  80. * @param string $line current line of code
  81. *
  82. * @param bool $inComment was the parser in a comment at the
  83. * beginning of the line?
  84. *
  85. * @return bool
  86. */
  87. private static function _eolInComment($line, $inComment)
  88. {
  89. while (strlen($line)) {
  90. $search = $inComment
  91. ? '*/'
  92. : '/*';
  93. $pos = strpos($line, $search);
  94. if (false === $pos) {
  95. return $inComment;
  96. } else {
  97. if ($pos == 0
  98. || ($inComment
  99. ? substr($line, $pos, 3)
  100. : substr($line, $pos-1, 3)) != '*/*')
  101. {
  102. $inComment = ! $inComment;
  103. }
  104. $line = substr($line, $pos + 2);
  105. }
  106. }
  107. return $inComment;
  108. }
  109. /**
  110. * Prepend a comment (or note) to the given line
  111. *
  112. * @param string $line current line of code
  113. *
  114. * @param string $note content of note/comment
  115. *
  116. * @param bool $inComment was the parser in a comment at the
  117. * beginning of the line?
  118. *
  119. * @param int $padTo minimum width of comment
  120. *
  121. * @return string
  122. */
  123. private static function _addNote($line, $note, $inComment, $padTo)
  124. {
  125. return $inComment
  126. ? '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' *| ' . $line
  127. : '/* ' . str_pad($note, $padTo, ' ', STR_PAD_RIGHT) . ' */ ' . $line;
  128. }
  129. }