blob: 6a681a850fe7905e22d6a7d2265d37920a06fa63 [file] [log] [blame]
Matthias Andreas Benkardb382b102021-01-02 15:32:21 +01001#!/usr/bin/env php
2<?php
3use MatthiasMullie\Minify;
4
5// command line utility to minify CSS
6if (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
15error_reporting(E_ALL);
16// check PHP setup for cli arguments
17if (!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
24if (!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
29if ('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
34if (!file_exists($argv[1])) {
35 fwrite(STDERR, 'Source file "' . $argv[1] . '" not found' . PHP_EOL);
36 exit(1);
37}
38
39try {
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}