packer.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. <?php
  2. # by DSR!
  3. # from https://github.com/xchwarze/wifi-pineapple-panel
  4. require_once __DIR__ . '/deps/vendor/autoload.php';
  5. require_once __DIR__ . '/deps/PackPHP.php'; //from: https://github.com/hareko/php-application-packer
  6. use MatthiasMullie\Minify;
  7. use voku\helper\HtmlMin;
  8. error_reporting(E_ALL);
  9. if (!isset($_SERVER['argv']) && !isset($argv)) {
  10. echo "Please enable the register_argc_argv directive in your php.ini\n";
  11. exit(1);
  12. } elseif (!isset($argv)) {
  13. $argv = $_SERVER['argv'];
  14. }
  15. if (!isset($argv[1])) {
  16. echo "Argument expected: path to folder\n";
  17. exit(1);
  18. }
  19. function getDirContents($dir) {
  20. $files = array();
  21. $iterator = new RecursiveIteratorIterator(
  22. new RecursiveDirectoryIterator($dir)
  23. );
  24. foreach ($iterator as $file) {
  25. if ($file->isDir()){
  26. continue;
  27. }
  28. $files[] = $file->getPathname();
  29. }
  30. return $files;
  31. }
  32. echo "******* Project packer by DSR! *******\n";
  33. echo "php deps: sudo apt-get update && sudo apt install php-xml php-mbstring\n\n";
  34. $counter = 0;
  35. echo "Target folder: {$argv[1]}\n";
  36. foreach (getDirContents($argv[1]) as $file) {
  37. $info = pathinfo($file);
  38. if (isset($info['extension']) && !fnmatch('*.min', $info['filename'], FNM_CASEFOLD)) {
  39. try {
  40. switch ($info['extension']) {
  41. case 'js':
  42. $counter++;
  43. $minifier = new Minify\JS($file);
  44. $minifier->minify($file);
  45. break;
  46. case 'css':
  47. $counter++;
  48. $minifier = new Minify\CSS($file);
  49. $minifier->minify($file);
  50. break;
  51. case 'php':
  52. $counter++;
  53. $file_contents = file_get_contents($file);
  54. $minified = PackPHP::minify($file_contents, ['min' => true]);
  55. file_put_contents($file, $minified);
  56. break;
  57. case 'html':
  58. $counter++;
  59. $file_contents = file_get_contents($file);
  60. $minifier = new HtmlMin();
  61. $minified = $minifier->minify($file_contents);
  62. file_put_contents($file, $minified);
  63. break;
  64. }
  65. } catch (Exception $e) {
  66. echo "[ERROR!] " . $e->getMessage() . "\n";
  67. }
  68. }
  69. }
  70. echo "Processed files: {$counter}\n";