PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/scripts/pack-javascript.php

https://github.com/txdv/sockso
PHP | 92 lines | 51 code | 24 blank | 17 comment | 5 complexity | e28ba4deed0adbca40b7a2670120d445 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /**
  3. * this script packs up all the javascript files in the js directory, it
  4. * makes sure that the jquery library is put first in the file
  5. *
  6. */
  7. include 'lib/php/JavaScriptPacker.class.php';
  8. list( $IGNORE, $version ) = $argv;
  9. $jsDir = 'resources/htdocs/js';
  10. $packFile = "$jsDir/packed-$version.js";
  11. $unpackFile = "$jsDir/unpacked-$version.js";
  12. $licenses = '';
  13. $allScript = '';
  14. $preloadFiles = array( 'jquery.js', 'sockso.js' );
  15. $postloadFiles = array( 'init.js' );
  16. foreach ( $preloadFiles as $preloadFile ) {
  17. $allScript .= file_get_contents( "$jsDir/$preloadFile" );
  18. $licenses .= extractLicenses( $allScript );
  19. }
  20. $d = opendir( $jsDir );
  21. $dirlist = Array();
  22. // then read in all javascript files and pack them up
  23. while ( $file = readdir($d) ) {
  24. $dirlist[] = $file;
  25. }
  26. sort($dirlist);
  27. foreach ($dirlist as $key => $file) {
  28. $path = "$jsDir/$file";
  29. // remove old pack files
  30. if ( preg_match('/packed-/',$file) ) {
  31. unlink( $path );
  32. }
  33. elseif ( !in_array($file,array_merge($preloadFiles,$postloadFiles)) && substr($file,-2) == 'js' ) {
  34. $script = file_get_contents( $path );
  35. $licenses .= extractLicenses( $script );
  36. $allScript .= $script;
  37. }
  38. }
  39. foreach ( $postloadFiles as $file ) {
  40. $allScript .= file_get_contents( "$jsDir/$file" );
  41. $licenses .= extractLicenses( $allScript );
  42. }
  43. // write unpacked javascript
  44. $f = fopen( $unpackFile, 'w' );
  45. fwrite( $f, $allScript );
  46. fclose( $f );
  47. // write packed javascript
  48. $packer = new JavaScriptPacker( $allScript, 'Normal', true, false );
  49. $f = fopen( $packFile, 'w' );
  50. fwrite( $f, $licenses . $packer->pack() );
  51. //fwrite( $f, $allScript ); // not packed
  52. fclose( $f );
  53. /**
  54. * extracts license info marked with /***'s
  55. *
  56. * @param $script javascript to search in
  57. * @return license info string
  58. *
  59. */
  60. function extractLicenses( $script ) {
  61. $pattern = '#/\*\*\*.*?\*/#s';
  62. $licenses = '';
  63. preg_match_all( $pattern, $script, $matches );
  64. if ( $matches )
  65. foreach ( $matches[0] as $match )
  66. $licenses .= $match . "\n";
  67. return $licenses;
  68. }