constant_generator.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. require __DIR__ . '/../vendor/autoload.php';
  3. $jsonfile = __DIR__ . '/../tests/user_agents.json';
  4. $uas = json_decode(
  5. file_get_contents($jsonfile),
  6. true
  7. );
  8. $platforms = array();
  9. $browsers = array();
  10. foreach( $uas as $key => $val ) {
  11. $kex = strtoupper($val['browser']);
  12. if( $kex !== '' ) {
  13. $kex = preg_replace('/\W+/', '_', $kex);
  14. if( !isset($browsers[$kex][$val['browser']]) ) {
  15. $browsers[$kex][$val['browser']] = 0;
  16. }
  17. $browsers[$kex][$val['browser']]++;
  18. }
  19. $kex = strtoupper($val['platform']);
  20. if( $kex !== '' ) {
  21. $kex = preg_replace('/\W+/', '_', $kex);
  22. if( !isset($platforms[$kex][$val['platform']]) ) {
  23. $platforms[$kex][$val['platform']] = 0;
  24. }
  25. $platforms[$kex][$val['platform']]++;
  26. }
  27. }
  28. ksort($browsers);
  29. $file = basename(__FILE__);
  30. $header = <<<EOT
  31. <?php
  32. // DO NOT EDIT THIS FILE - IT IS GENERATED BY {$file}
  33. EOT;
  34. foreach( $browsers as $browser ) {
  35. if( count($browser) !== 1 ) {
  36. echo "bad browser count\n";
  37. die(2);
  38. }
  39. }
  40. $browserBody = "{$header}namespace donatj\UserAgent;\n\ninterface Browsers {\n\n";
  41. $maxKey = max(array_map('strlen', array_keys($browsers)));
  42. foreach( $browsers as $const => $val ) {
  43. $browserBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
  44. }
  45. $browserBody .= "\n}\n\n";
  46. foreach( $platforms as $platform ) {
  47. if( count($platform) !== 1 ) {
  48. echo "bad platform count\n";
  49. die(2);
  50. }
  51. }
  52. $platformBody = "{$header}namespace donatj\UserAgent;\n\ninterface Platforms {\n\n";
  53. $maxKey = max(array_map('strlen', array_keys($platforms)));
  54. foreach( $platforms as $const => $val ) {
  55. $platformBody .= sprintf("\tconst %-{$maxKey}s = %s;\n", $const, var_export(key($val), true));
  56. }
  57. $platformBody .= "\n}\n\n";
  58. file_put_contents(__DIR__ . '/../src/UserAgent/Browsers.php', $browserBody);
  59. file_put_contents(__DIR__ . '/../src/UserAgent/Platforms.php', $platformBody);