Matthias Andreas Benkard | b382b10 | 2021-01-02 15:32:21 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env php |
| 2 | <?php |
| 3 | use MatthiasMullie\Minify; |
| 4 | |
| 5 | // command line utility to minify CSS |
| 6 | if (file_exists(__DIR__ . '/../../../autoload.php')) { |
| 7 | // if composer install |
| 8 | require_once __DIR__ . '/../../../autoload.php'; |
| 9 | } else { |
| 10 | require_once __DIR__ . '/../src/Minify.php'; |
| 11 | require_once __DIR__ . '/../src/CSS.php'; |
| 12 | require_once __DIR__ . '/../src/Exception.php'; |
| 13 | } |
| 14 | |
| 15 | error_reporting(E_ALL); |
| 16 | // check PHP setup for cli arguments |
| 17 | if (!isset($_SERVER['argv']) && !isset($argv)) { |
| 18 | fwrite(STDERR, 'Please enable the "register_argc_argv" directive in your php.ini' . PHP_EOL); |
| 19 | exit(1); |
| 20 | } elseif (!isset($argv)) { |
| 21 | $argv = $_SERVER['argv']; |
| 22 | } |
| 23 | // check if path to file given |
| 24 | if (!isset($argv[1])) { |
| 25 | fwrite(STDERR, 'Argument expected: path to file' . PHP_EOL); |
| 26 | exit(1); |
| 27 | } |
| 28 | // check if script run in cli environment |
| 29 | if ('cli' !== php_sapi_name()) { |
| 30 | fwrite(STDERR, $argv[1] . ' must be run in the command line' . PHP_EOL); |
| 31 | exit(1); |
| 32 | } |
| 33 | // check if source file exists |
| 34 | if (!file_exists($argv[1])) { |
| 35 | fwrite(STDERR, 'Source file "' . $argv[1] . '" not found' . PHP_EOL); |
| 36 | exit(1); |
| 37 | } |
| 38 | |
| 39 | try { |
| 40 | $minifier = new Minify\CSS($argv[1]); |
| 41 | echo $minifier->minify(); |
| 42 | } catch (Exception $e) { |
| 43 | fwrite(STDERR, $e->getMessage(), PHP_EOL); |
| 44 | exit(1); |
| 45 | } |