-
Notifications
You must be signed in to change notification settings - Fork 2
/
Minifier.phpclass
executable file
·826 lines (615 loc) · 28.2 KB
/
Minifier.phpclass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
<?php
/**************************************************************************************************************
NAME
Minifier.phpclass
DESCRIPTION
Abstract base class for code minifiers.
A source file is considered as a flow of comments (either single- or multi-line), strings enquoted by
some sequence(s), spaces, newlines and tokens.
By default, the minifier base class treats single characters as tokens. If the derived class wants to
perform more detailed syntax analysis, it will have to override the ProcessToken() method.
AUTHOR
Christian Vigh, 10/2015.
HISTORY
[Version : 1.0] [Date : 2015/10/16] [Author : CV]
Initial version.
**************************************************************************************************************/
class MinifierException extends \RuntimeException {} ;
/*==============================================================================================================
Minifier class -
Abstract base class for code minifiers.
==============================================================================================================*/
abstract class Minifier // extends Object
{
// Token types
const TOKEN_NONE = 0 ;
const TOKEN_SPACE = 1 ;
const TOKEN_NEWLINE = 2 ;
const TOKEN_STRING = 3 ;
const TOKEN_ELEMENT = 4 ;
const TOKEN_IDENTIFIER = 5 ;
// Definitions provided by derived classes
protected $SingleLineComments = [] ;
protected $MultiLineComments = [] ;
protected $QuotedStrings = [] ;
protected $Continuation = false ;
protected $Spaces = [ " " => " ", "\t" => "\t", "\v" => "\v", "\r" => "\r", "\xA0" => "\xA0" ] ;
protected $IdentifierRegex = false ;
protected $Tokens = [] ;
// Internal data
private $ContinuationLength ; // Continuation string length - avoid repeated calls to strlen
// The call table is here for optimization purposes ; it holds pointer to internal functions that will
// be called when
// - The first character of a comment start or string start is encountered in the input flow
// - A space or a newline is found
// This table is built by the class constructor
private $CallTable = [] ;
// Contents to be minified
protected $Content ;
protected $ContentLength ;
// Current line in the input flow - mainly used for error messages
protected $CurrentLine ;
// Last token
protected $LastToken ;
protected $LastTokenType ;
/*--------------------------------------------------------------------------------------------------------------
NAME
Constructor
PROTOTYPE
$minifier = new Minifier ( ) ;
DESCRIPTION
Instanciates a Minifier class.
NOTES
The constructor must be called by derived classes AFTER any call to the SetXXX functions.
*-------------------------------------------------------------------------------------------------------------*/
public function __construct ( )
{
//parent::__construct ( ) ;
if ( ! $this -> Spaces )
$this -> SetSpaces ( " \t\v\r" . chr ( 160 ) ) ;
// Build the call table for the GetNextToken() method
$this -> Finalize ( ) ;
// Initialize input-dependent members
$this -> Reset ( ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
Finalize - Finalizes the object instance.
PROTOTYPE
$minifier -> Finalize ( ) ;
DESCRIPTION
Finalizes the object instance, by rebuilding the call table that is used by the GetNextToken() method.
NOTES
This function MUST be called if one or more of the Setxxx methods have been called after the class
constructor.
*-------------------------------------------------------------------------------------------------------------*/
protected function Finalize ( )
{
foreach ( array_keys ( $this -> Spaces ) as $space )
$this -> CallTable [ $space ] [] = [ 'callback' => [ $this, 'ProcessSpaces' ], 'return' => true ] ;
$this -> CallTables [ "\n" ] [] = [ 'callback' => [ $this, 'ProcessNewlines' ], 'return' => true ] ;
if ( $this -> Continuation )
$this -> CallTables [ $this -> Continuation [0] ] [] = [ 'callback' => [ $this, 'ProcessContinuation' ], 'return' => false ] ;
foreach ( $this -> SingleLineComments as $def )
$this -> CallTable [ $def [ 'value' ] [0] ] [] = [ 'callback' => [ $this, 'ProcessSingleLineComments' ], 'return' => false ] ;
foreach ( $this -> MultiLineComments as $def )
$this -> CallTable [ $def [ 'start' ] [0] ] [] = [ 'callback' => [ $this, 'ProcessMultiLineComments' ], 'return' => false ] ;
foreach ( $this -> QuotedStrings as $def )
$this -> CallTable [ $def [ 'left-quote' ] ] [] = [ 'callback' => [ $this, 'ProcessString' ], 'return' => true ] ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetComments - Sets the single- and multi-line comment definitions.
PROTOTYPE
$minifier -> SetComments ( $single_comments, $multi_comments ) ;
DESCRIPTION
Defines the comment style for the derived class.
PARAMETERS
$single_comments (array of strings) -
An array specifying the sequences starting single-line comments. An example definition for
PHP sources would be : [ '#', '//' ].
$multi_comments (array of associative arrays) -
Array of multiline comment definitions, which contains the following entries :
- 'start' :
Multi-line comment start sequence.
- 'end' :
Multi-line comment end sequence.
- 'nested' :
A boolean indicating whether multi-line comments can be nested.
The default value is false if not specified.
An example definition for PHP sources would be :
[ [ 'start' => '/*', 'end' => '* /' ] ]
NOTES
This function must be called by the derived class BEFORE the Minifier constructor.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetComments ( $single_comments, $multi_comments )
{
$this -> SingleLineComments = [] ;
$this -> MultiLineComments = [] ;
foreach ( $single_comments as $single_comment )
$this -> SingleLineComments [] = [ 'value' => $single_comment, 'length' => strlen ( $single_comment ) ] ;
$index = 0 ;
foreach ( $multi_comments as $comment_def )
{
if ( ! isset ( $comment_def [ 'start' ] ) )
throw ( new MinifierException ( "Missing 'start' entry for multiline comment definition #$index." ) ) ;
if ( ! isset ( $comment_def [ 'end' ] ) )
throw ( new MinifierException ( "Missing 'end' entry for multiline comment definition #$index." ) ) ;
if ( ! isset ( $comment_def [ 'nested' ] ) || ! $comment_def [ 'nested' ] )
$comment_def [ 'nested' ] = false ;
$comment_def [ 'start-length' ] = strlen ( $comment_def [ 'start' ] ) ;
$comment_def [ 'end-length' ] = strlen ( $comment_def [ 'end' ] ) ;
$this -> MultiLineComments [] = $comment_def ;
$index ++ ;
}
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetQuotedStrings - Sets the quoted string definitions.
PROTOTYPE
$minifier -> SetQuotedStrings ( $strings ) ;
DESCRIPTION
Defines the string style for the derived class.
PARAMETERS
$strings (array of associative arrays) -
Arrays of string definitions, which contain the following entries :
- 'left-quote', 'right-quote' :
Starting and ending quote sequence (the minifier can handle string quotes of
more than one character).
If only 'left-quote' is defined, 'right-quote' will be set to its value.
- 'quote' :
Specifies both the 'left-quote' and 'right-quote' entries.
- 'escape' :
Either boolean false to indicate that the language does not support escape
sequences within a string token, or a string specifying an escape character
that cancel any meaning of the next one.
When not specified, the default value is false.
- 'continuation' :
Some languages, such as PHP, allow multiline strings ; some not, like
Javascript ; but Javascript allows for continuation lines (lines terminated by '\').
Spaces are not authorized after a continuation sequence ; a continuation line is
recognized by having a newline character after the continuation sequence (note that
carriage returns are ignored).
When not specified or set to boolean false, the default value is false, which means that
the language does not support continuation lines.
NOTES
This function must be called by the derived class BEFORE the Minifier constructor.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetQuotedStrings ( $strings )
{
$index = 0 ;
$this -> QuotedStrings = [] ;
foreach ( $strings as $quoted_def )
{
if ( ! isset ( $quoted_def [ 'quote' ] ) && ! isset ( $quoted_def [ 'left-quote' ] ) )
throw ( new MinifierException ( "Either the 'quote' or 'left-quote' entry is required for string definition #$index." ) ) ;
if ( ! isset ( $quoted_def [ 'left-quote' ] ) )
$quoted_def [ 'left-quote' ] = $quoted_def [ 'quote' ] ;
if ( ! isset ( $quoted_def [ 'right-quote' ] ) )
$quoted_def [ 'right-quote' ] = $quoted_def [ 'left-quote' ] ;
if ( ! isset ( $quoted_def [ 'escape' ] ) || ! $quoted_def [ 'escape' ] )
$quoted_def [ 'escape' ] = false ;
if ( ! isset ( $quoted_def [ 'continuation' ] ) || ! $quoted_def [ 'continuation' ] )
$quoted_def [ 'continuation' ] = false ;
$quoted_def [ 'left-quote-length' ] = strlen ( $quoted_def [ 'left-quote' ] ) ;
$quoted_def [ 'right-quote-length' ] = strlen ( $quoted_def [ 'right-quote' ] ) ;
$quoted_def [ 'escape-length' ] = ( $quoted_def [ 'escape' ] ) ? strlen ( $quoted_def [ 'escape' ] ) : 0 ;
$this -> QuotedStrings [] = $quoted_def ;
$index ++ ;
}
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetContinuation - Defines the continuation line.
PROTOTYPE
$minify -> SetContinuation ( $string ) ;
DESCRIPTION
Defines the continuation line construct, which must be foollowed either by crlf or lf.
PARAMETERS
$string (string) -
Continuation line sequence.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetContinuation ( $string )
{
$this -> Continuation = $string ;
$this -> ContinuationLength = strlen ( $string ) ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetSpaces - Defines the spaces recognized by the minifier.
PROTOTYPE
$minifier -> SetSpaces ( $string ) ;
DESCRIPTION
Defines the space characters recognized by the minifier.
PARAMETERS
$string (string) -
String of characters to be considered as spaces.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetSpaces ( $spaces )
{
$length = strlen ( $spaces ) ;
$this -> Spaces = [] ;
for ( $i = 0 ; $i < $length ; $i ++ )
$this -> Spaces [ $spaces [$i] ] = $spaces [$i] ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetIdentifierRegex - Sets the regex to be used for identifier matching.
PROTOTYPE
$minifier -> SetIdentifierRegex ( $re ) ;
DESCRIPTION
Sets the regex to be used for identifier matching.
PARAMETERS
$re (string) -
Regular expression to be used for matching an identifier. The regex options '/imsx' are
automatically added. The supplied regular expression must not contain any delimiter.
NOTES
If this method is not called by the derived class, no identifier will be recognized and characters will
be scanned one by one by the ProcessToken() method.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetIdentifierRegex ( $re )
{
$new_re = '/(?P<capture> (?P<name> ' . $re . ') [ \t\r\n]*)/ix' ;
$this -> IdentifierRegex = $new_re ;
}
/*--------------------------------------------------------------------------------------------------------------
NAME
SetTokens - Sets the tokens around which spaces are to be eaten up.
PROTOTYPE
$minifier -> SetTokens ( $tokens ) ;
DESCRIPTION
Sets the tokens around which spaces are to be eaten up ; this is the case for example for the
relational operators, parentheses, etc.
PARAMETERS
$tokens (array of strings) -
Tokens around which spaces should be eaten.
*-------------------------------------------------------------------------------------------------------------*/
protected function SetTokens ( $tokens )
{
$this -> Tokens = [] ;
foreach ( $tokens as $token )
{
$ch = $token [0] ;
$this -> Tokens [ $ch ] [] = [ 'value' => $token, 'length' => strlen ( $token ) ] ;
}
}
/*--------------------------------------------------------------------------------------------------------------
Reset -
Resets this instance so that a new file can be minified.
*-------------------------------------------------------------------------------------------------------------*/
protected function Reset ( $data = null )
{
$this -> Content = $data ;
$this -> ContentLength = strlen ( $data ) ;
$this -> CurrentLine = 0 ;
$this -> LastToken = '' ;
$this -> LastTokenType = self::TOKEN_NONE ;
}
/*--------------------------------------------------------------------------------------------------------------
MinifyData -
To be implemented by derived classes for minifying data.
*-------------------------------------------------------------------------------------------------------------*/
abstract protected function MinifyData ( ) ;
/*--------------------------------------------------------------------------------------------------------------
Minify, MinifyFile -
Minifies a string/file.
*-------------------------------------------------------------------------------------------------------------*/
public function Minify ( $contents )
{
$this -> Reset ( $contents ) ;
return ( $this-> MinifyData ( ) ) ;
}
public function MinifyFrom ( $input )
{
if ( ! file_exists ( $input ) )
throw ( new \MinifierException ( "File \"$input\" not found." ) ) ;
return ( $this -> Minify ( file_get_contents ( $input ) ) ) ;
}
public function MinifyTo ( $output, $contents )
{
$data = $this -> Minify ( $contents ) ;
file_put_contents ( $output, $data ) ;
}
public function MinifyFileTo ( $output, $input )
{
if ( ! file_exists ( $input ) )
throw ( new MinifierException ( "File \"$input\" not found." ) ) ;
$this -> MinifyTo ( $output, file_get_contents ( $input ) ) ;
}
/*--------------------------------------------------------------------------------------------------------------
GetNextToken -
Retrieves the next token, either a newline, a string or a token returned by the ProcessToken() method.
Returns false if no more tokens are available.
*-------------------------------------------------------------------------------------------------------------*/
protected function GetNextToken ( &$offset, &$token, &$token_type )
{
$contents = $this -> Content ;
$length = $this -> ContentLength ;
$token = null ;
$this -> LastToken = $token ;
$this -> LastTokenType = $token_type ;
ShootAgain :
// Stop if no more characters are available
if ( ! isset ( $contents [ $offset ] ) )
return ( false ) ;
$ch = $contents [ $offset ] ;
// To avoid unnecessary calls to the Processxxx() methods, a call table is built by the Finalize method
// It is an array of callbacks whose keys are a character : when we find such a character in the input
// flow, then it means that the corresponding Processxxx() method is a good candidate for processing next
// input.
if ( isset ( $this -> CallTable [ $ch ] ) )
{
foreach ( $this -> CallTable [ $ch ] as $entry )
{
$callback = $entry [ 'callback' ] ;
$return_result = $entry [ 'return' ] ;
$status = $callback ( $contents, $length, $offset, $token, $token_type ) ;
if ( $status )
{
if ( $return_result )
return ( true ) ;
else
goto ShootAgain ;
}
}
}
// Other cases : return the character (or the token) as is
return ( $this -> ProcessToken ( $contents, $length, $offset, $token, $token_type ) ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessContinuation -
Checks if the current position specifies a continuation line (ie, a continuation sequence followed by
"\r\n" or "\n"). Returns true if yes (and set the $offset argument to point to the next character after
the newline), or false otherwise.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessContinuation ( $contents, $length, &$offset, $continuation )
{
if ( $continuation )
{
$continuation_length = $this -> ContinuationLength ;
if ( ! substr_compare ( $contents, "$continuation\r\n", $offset, $continuation_length + 2 ) )
{
$offset += $continuation_length + 2 ;
$this-> CurrentLine ++ ;
return ( true ) ;
}
else if ( ! substr_compare ( $contents, "$continuation\n", $offset, $continuation_length + 2 ) )
{
$offset += $continuation_length + 1 ;
$this-> CurrentLine ++ ;
return ( true ) ;
}
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessSingleLineComment -
Checks if the current position starts a single-line comment. Returns true if yes (and sets the $offset
argument to point to the next character after the end of line), or false otherwise.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessSingleLineComments ( $contents, $length, &$offset )
{
foreach ( $this -> SingleLineComments as $def )
{
if ( ! substr_compare ( $contents, $def [ 'value' ], $offset, $def [ 'length' ] ) )
{
$nlpos = strpos ( $contents, "\n", $offset ) ;
if ( $nlpos !== false )
{
$this -> CurrentLine ++ ;
$offset = $nlpos + 1 ;
$this -> EatSpaces ( $contents, $length, $offset ) ;
}
else
$offset = $length ;
return ( true ) ;
}
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessSpaces -
Checks if the current position starts a sequence of spaces. Returns true if yes (and sets the $offset
argument to point to the next character after the very last space), or false otherwise.
Spaces characters are : space, tab, vertical tab and carriage return. Line feed are not counted as
spaces.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessSpaces ( $contents, $length, &$offset, &$token, &$token_type )
{
$found_space = false ;
while ( isset ( $contents [ $offset ] ) && isset ( $this -> Spaces [ $contents [ $offset ] ] ) )
{
$found_space = true ;
$token = ' ' ;
$token_type = self::TOKEN_SPACE ;
$offset ++ ;
}
return ( $found_space ) ;
}
protected function EatSpaces ( $contents, $length, &$offset )
{
$count = 0 ;
while ( isset ( $contents [ $offset ] ) && ( isset ( $this -> Spaces [ $contents [ $offset ] ] ) || $contents [ $offset ] == "\n" ) )
{
if ( $contents [ $offset ] == "\n" )
$this -> CurrentLine ++ ;
$offset ++ ;
$count ++ ;
}
return ( $count ) ;
}
protected function ProcessNewlines ( $contents, $length, &$offset, &$token, &$token_type )
{
$found_newline = false ;
while ( isset ( $contents [ $offset ] ) && $contents [ $offset ] == "\n" )
{
$found_newline = true ;
$token = "\n" ;
$token_type = self::TOKEN_NEWLINE ;
$offset ++ ;
$this -> CurrentLine ++ ;
}
return ( $found_newline ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessMultiLineComment -
Checks if the current position starts a single-line comment. Returns true if yes (and sets the $offset
argument to point to the next character after the comment terminator), or false otherwise.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessMultiLineComments ( $contents, $length, &$offset )
{
foreach ( $this -> MultiLineComments as $def )
{
$comment_start = $def [ 'start' ] ;
$comment_end = $def [ 'end' ] ;
$nested = $def [ 'nested' ] ;
$comment_start_length = $def [ 'start-length' ] ;
$comment_end_length = $def [ 'end-length' ] ;
if ( ! substr_compare ( $contents, $comment_start, $offset, $comment_start_length ) )
{
$found_end_comment = false ;
// When the language accepts nested multiline comments, we only have the solution of
// checking one character after another
if ( $nested )
{
$offset += $comment_start_length ;
$nesting_level = 1 ;
while ( $nesting_level && isset ( $contents [ $offset ] ) )
{
if ( ! substr_compare ( $contents, $comment_start, $offset, $comment_start_length ) )
{
$nesting_level ++ ;
$offset += $comment_start_length ;
}
else if ( ! substr_compare ( $contents, $comment_end, $offset, $comment_end_length ) )
{
$nesting_level -- ;
$offset += $comment_end_length ;
}
}
if ( ! $nesting_level )
$found_end_comment = true ;
}
// ... but when not allowed, we can use builtin functions to go faster
else
{
$endpos = strpos ( $contents, $comment_end, $offset ) ;
if ( $endpos !== false )
{
$this -> CurrentLine += substr_count ( $contents, "\n", $offset, $endpos - $offset ) ;
$offset = $endpos + $comment_end_length ;
$found_end_comment = true ;
$this -> EatSpaces ( $contents, $length, $offset ) ;
}
}
if ( ! $found_end_comment )
throw ( new MinifierException ( "Unterminated comment started at line #{$this -> CurrentLine}." ) ) ;
return ( true ) ;
}
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessString -
Checks if the current position starts a string. Returns true if yes (and sets the $offset argument to
point to the next character after the end of the string), or false otherwise.
The $token parameter will be set to the found string, if any.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessString ( $contents, $length, &$offset, &$token, &$token_type )
{
foreach ( $this -> QuotedStrings as $def )
{
$left = $def [ 'left-quote' ] ;
$right = $def [ 'right-quote' ] ;
$escape = $def [ 'escape' ] ;
$continuation = $def [ 'continuation' ] ;
$left_length = $def [ 'left-quote-length' ] ;
$right_length = $def [ 'right-quote-length' ] ;
$escape_length = $def [ 'escape-length' ] ;
if ( $contents [ $offset ] == $left [0] && ! substr_compare ( $contents, $left, $offset, $left_length ) )
{
$offset += $left_length ;
$found_eos = false ;
$token = $left ;
// Find the terminating quoted string sequence
while ( isset ( $contents [ $offset ] ) )
{
if ( $this -> Continuation && $this -> Continuation [0] == $contents [ $offset ] &&
$this -> ProcessContinuation ( $contents, $length, $offset, $continuation ) )
{
if ( ! isset ( $contents [ $offset ] ) )
break ;
}
// Escape within the string
if ( $escape && ! substr_compare ( $contents, $escape, $offset, $escape_length ) )
{
$token .= $escape ;
$offset += $escape_length ;
// String with an escape sequence not terminated
if ( ! isset ( $contents [ $offset ] ) )
break ;
$token .= $contents [ $offset ++ ] ;
}
// String termination sequence
else if ( ! substr_compare ( $contents, $right, $offset, $right_length ) )
{
$token .= $right ;
$offset += $right_length ;
$found_eos = true ;
break ;
}
else
$token .= $contents [ $offset ++ ] ;
}
if ( ! $found_eos )
throw ( new MinifierException ( "Unterminated string started at line #{$this -> CurrentLine}." ) ) ;
$token_type = self::TOKEN_STRING ;
$this -> CurrentLine += substr_count ( $token, "\n" ) ;
return ( true ) ;
}
}
return ( false ) ;
}
/*--------------------------------------------------------------------------------------------------------------
ProcessToken-
Processes the next token that is neither a string, a comment, a group of spaces or a newline.
The default implementation processes one character at a time.
*-------------------------------------------------------------------------------------------------------------*/
protected function ProcessToken ( $contents, $length, &$offset, &$token, &$token_type )
{
$ch = $contents [ $offset ] ;
if ( $ch <= '~' )
{
if ( isset ( $this -> Tokens [ $ch ] ) )
{
foreach ( $this -> Tokens [ $ch ] as $item )
{
$value = $item [ 'value' ] ;
$value_length = $item [ 'length' ] ;
if ( ! substr_compare ( $contents, $value, $offset, $value_length ) )
{
$token = $value ;
$token_type = self::TOKEN_ELEMENT ;
$offset += $value_length ;
$this -> EatSpaces ( $contents, $length, $offset ) ;
return ( true ) ;
}
}
}
if ( $this -> IdentifierRegex && preg_match ( $this -> IdentifierRegex, $contents, $match, PREG_OFFSET_CAPTURE, $offset ) )
{
if ( $match [ 'name' ] [1] == $offset )
{
$token = $match [ 'name' ] [0] ;
$token_type = self::TOKEN_IDENTIFIER ;
$offset += strlen ( $match [ 'capture' ] [0] ) ;
$this -> CurrentLine += substr_count ( $match [ 'capture' ] [0], "\n" ) ;
return ( true ) ;
}
}
}
$token = $contents [ $offset ++ ] ;
$token_type = self::TOKEN_ELEMENT ;
return ( true ) ;
}
}