1
1
#!/usr/bin/php
2
2
<?php # minify.php - CSS minifier for CLI - Nate Nasteff 2020
3
3
4
+ /* Define regex patterns to strip whitespaces, newlines,
5
+ ** comments and certain semicolons from a string of CSS.
6
+ */
7
+
8
+ $ regex = [
9
+ "`^([ \t\s]+)`ism " =>'' ,
10
+ "`^\/\*(.+?)\*\/`ism " =>"" ,
11
+ "`([ \n\A;]+)\/\*(.+?)\*\/`ism " =>"" ,
12
+ "`([ \n\A;\s]+)//(.+?)[ \n\r]`ism " =>"" ,
13
+ "`(^[ \r\n]*|[ \r\n]+)[\s \t]*[ \r\n]+`ism " =>"" ,
14
+ "/\s+/ " =>'' ,
15
+ "/;}/ " =>'} '
16
+ ];
17
+
4
18
// If no args are received from CLI or help is requested, print instructions..
5
19
6
20
if ($ argc == 1 || in_array ($ argv [1 ], array ('--help ' , '-help ' , '-h ' , '-? ' ))) {
34
48
35
49
else {
36
50
37
- // Define regex patterns to strip comments
51
+ /* Define func to return a minified CSS string.
52
+ ** Takes an argument of str (unmodified .css string).
53
+ */
38
54
39
- $ regex = [
40
- "`^([ \t\s]+)`ism " =>'' ,
41
- "`^\/\*(.+?)\*\/`ism " =>"" ,
42
- "`([ \n\A;]+)\/\*(.+?)\*\/`ism " =>"" ,
43
- "`([ \n\A;\s]+)//(.+?)[ \n\r]`ism " =>"" ,
44
- "`(^[ \r\n]*|[ \r\n]+)[\s \t]*[ \r\n]+`ism " =>"" ,
45
- "/\s+/ " =>'' ,
46
- "/;}/ " =>'} '
47
- ];
48
-
49
- // Check that the file name is valid
50
- // TODO: Add logic for multiple files
51
-
52
- if (file_exists ($ argv [1 ])){
53
-
54
- // Define anonymous func to return a minified string
55
-
56
- $ minify = function () use (&$ argv , &$ regex ){
57
- $ css_str = file_get_contents ($ argv [1 ]);
58
- return preg_replace (array_keys ($ regex ), $ regex , $ css_str );
55
+ function minify ($ css_str , $ regex ){
56
+ return preg_replace (array_keys ($ regex ), $ regex , $ css_str );
57
+ }
58
+
59
+ /* Define func to validate the
60
+ ** file. If it exists, and is a valid .css
61
+ ** file, return a minified .css file extension
62
+ ** ie test.css => test.min.css
63
+ */
64
+
65
+ $ validate = function () use ($ argv ){
66
+ $ x = !file_exists ($ argv [1 ])
67
+ ? null
68
+ : (!strpos ($ argv [1 ], '.css ' )
69
+ ? null
70
+ : str_replace (".css " , ".min.css " , $ argv [1 ]));
71
+ return $ x ;
59
72
};
60
73
61
- // Update filename
62
-
63
- $ minified_filename = str_replace (".css " , ".min.css " , $ argv [1 ]);
64
-
65
- // Attempt to save the new minified CSS
66
- try {
67
- file_put_contents ($ minified_filename , $ minify ());
68
- }
69
-
70
- catch (Exception $ e ) {
71
- echo $ e ->getMessage ();
74
+ /* Define anonymous function to write the new .min.css file
75
+ ** to disk. Checks that $midified_filename has been validated,
76
+ ** then passes the filename as the first arg for file_put_contents.
77
+ ** The second argument calls minify(), which returns a modified CSS
78
+ ** string. Returns a string to be printed to the user to
79
+ ** notify them of success or failure
80
+ */
81
+
82
+ $ write_out = function ($ minified_filename ) use ($ argv , $ regex ) {
83
+ if (isset ($ minified_filename )){
84
+ try {
85
+ file_put_contents (
86
+ $ minified_filename , minify (
87
+ file_get_contents ($ argv [1 ]), $ regex ));
88
+ }
89
+ catch (Exception $ e ) {
90
+ return "I/O Error! Check your file permissions. " . PHP_EOL ;
91
+ }
92
+ return "Minified CSS file written to " . $ minified_filename . PHP_EOL ;
72
93
}
94
+ else return "Minify error - Not a valid CSS file / invalid filename! " . PHP_EOL ;
95
+ };
73
96
74
- echo "Minified CSS file written to " . $ minified_filename ."\n" ;
75
- }
76
-
77
- // Make sure file is actually a valid CSS file..
78
-
79
- else if (!strpos ($ argv [1 ], '.css ' )) {
80
- echo "Not a valid CSS file! " ;
81
- }
82
-
83
- // If no file is found / incorrect filename..
97
+ /* Call write_out, with $validate() func as arg, returning
98
+ ** a string to be printed to the user for confirmation.
99
+ */
84
100
85
- else {
86
- echo "File not found or incorrect filename! " ;
87
- }
101
+ print ($ write_out ($ validate ()));
88
102
}
89
103
90
- ?>
104
+ ?>
0 commit comments