Skip to content

Commit ab2adf8

Browse files
author
Nate Nasteff
committed
logic overhaul
1 parent 8c0b0e0 commit ab2adf8

File tree

2 files changed

+62
-48
lines changed

2 files changed

+62
-48
lines changed

minify.php

Lines changed: 60 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
#!/usr/bin/php
22
<?php # minify.php - CSS minifier for CLI - Nate Nasteff 2020
33

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+
418
// If no args are received from CLI or help is requested, print instructions..
519

620
if ($argc == 1 || in_array($argv[1], array('--help', '-help', '-h', '-?'))) {
@@ -34,57 +48,57 @@
3448

3549
else {
3650

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+
*/
3854

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;
5972
};
6073

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;
7293
}
94+
else return "Minify error - Not a valid CSS file / invalid filename!" . PHP_EOL;
95+
};
7396

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+
*/
84100

85-
else {
86-
echo "File not found or incorrect filename!";
87-
}
101+
print($write_out($validate()));
88102
}
89103

90-
?>
104+
?>

test.css

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ html {
1919

2020
a {
2121
padding: 0;
22-
22+
2323
content: '/* Test */';
24-
}
24+
}

0 commit comments

Comments
 (0)