minifyjs 1.2 KB

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