PageRenderTime 41ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bin/LocaleGenerator.php

https://github.com/bforchhammer/bnetlib
PHP | 221 lines | 173 code | 22 blank | 26 comment | 21 complexity | 4226706b5d7bb9cfd74e35fd4a7138ca MD5 | raw file
  1. #!/usr/bin/env php
  2. <?php
  3. /**
  4. * This file is part of the bnetlib Library.
  5. * Copyright (c) 2012 Eric Boh <cossish@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code. You can also view the
  9. * LICENSE file online at http://coss.github.com/bnetlib/license.html
  10. *
  11. * @copyright 2012 Eric Boh <cossish@gmail.com>
  12. * @license http://coss.gitbub.com/bnetlib/license.html MIT License
  13. */
  14. if (!is_readable(__DIR__ . '/LocaleConfig.php')) {
  15. die('Unable to load config file.');
  16. }
  17. if (!is_readable(dirname(__DIR__) . '/tests/_autoload.php')) {
  18. die('Unable to load autoloader.');
  19. }
  20. function requestResource($game, $method, $locale, $region) {
  21. try {
  22. $response = call_user_func_array(
  23. array($game, 'get' . $method),
  24. array(array(
  25. 'locale' => $locale,
  26. 'region' => $region
  27. ))
  28. );
  29. } catch (bnetlib\Exception\ResponseException $e) {
  30. echo $e->getMessage() . PHP_EOL;
  31. $code = ($e->getCode() === 0) ? 2 : $e->getCode();
  32. exit($code);
  33. }
  34. return $response['content'];
  35. }
  36. function createKeyValueList($config, $response) {
  37. $error = false;
  38. $return = array();
  39. if (!isset($response[$config['sub']])) {
  40. printf('Error: Wrong sub key %s.%s', $config['sub'], PHP_EOL);
  41. exit(3);
  42. }
  43. foreach ($response[$config['sub']] as $entry) {
  44. if (!isset($entry[$config['key']])) {
  45. $error = true;
  46. printf('Error: Missing key %s.%s', $config['key'], PHP_EOL);
  47. }
  48. if (!isset($entry[$config['value']])) {
  49. $error = true;
  50. printf('Error: Missing value key %s.%s', $config['value'], PHP_EOL);
  51. }
  52. if ($error === true) {
  53. exit(3);
  54. }
  55. $return[$entry[$config['key']]] = $entry[$config['value']];
  56. }
  57. return $return;
  58. }
  59. $config = include __DIR__ . '/LocaleConfig.php';
  60. include dirname(__DIR__) . '/tests/_autoload.php';
  61. $games = array();
  62. $output = array();
  63. $internal = array();
  64. $connection = new bnetlib\Connection\ZendFramework();
  65. #$connection->getClient()->setOptions(array('adapter' => 'Zend\Http\Client\Adapter\Curl'));
  66. $reflection = new ReflectionClass($connection);
  67. $consts = $reflection->getConstants();
  68. echo 'Gather locales...' . PHP_EOL;
  69. foreach ($config['games'] as $game => $class) {
  70. $games[$game] = new $class($connection);
  71. $games[$game]->setReturnType(bnetlib\AbstractGame::RETURN_PLAIN);
  72. $internal[$game]['locale']['_all'] = array();
  73. foreach ($consts as $const => $string) {
  74. if (substr($const, 0, 6) === 'REGION') {
  75. $internal[$game]['locale'][$string] = $games[$game]->getSupportedLocale($string);
  76. foreach ($internal[$game]['locale'][$string] as $locale) {
  77. $internal[$game]['locale']['_all'][$locale] = $string;
  78. }
  79. }
  80. }
  81. }
  82. echo 'Populate defaults...' . PHP_EOL;
  83. foreach ($config['locale'] as $game => $locales) {
  84. if (isset($games[$game])) {
  85. if (isset($config['locale'][$game][$config['default_locale']])) {
  86. foreach ($config['locale'][$game][$config['default_locale']] as $key => $value) {
  87. echo '> ' . $game . '.' . $key . PHP_EOL;
  88. $internal[$game]['keys'][] = $key;
  89. if (is_string($value)) {
  90. list($method, $keyvalue) = explode('|', $value);
  91. list($sub, $sKey, $sValue) = explode(':', $keyvalue);
  92. $internal[$game]['dynamic'][$key] = array(
  93. 'sub' => $sub,
  94. 'key' => $sKey,
  95. 'value' => $sValue,
  96. 'method' => $method,
  97. );
  98. $internal[$game]['default'][$key] = createKeyValueList(
  99. $internal[$game]['dynamic'][$key],
  100. requestResource(
  101. $games[$game],
  102. $method,
  103. $config['default_locale'],
  104. $internal[$game]['locale']['_all'][$config['default_locale']]
  105. )
  106. );
  107. } else {
  108. $internal[$game]['default'][$key] = $value;
  109. }
  110. $output[$game][$config['default_locale']][$key] = $internal[$game]['default'][$key];
  111. }
  112. } else {
  113. printf('Error: Missing key (%s) in %s.%s', $config['default_locale'], $game, PHP_EOL);
  114. exit(1);
  115. }
  116. }
  117. }
  118. echo 'Populate locales...' . PHP_EOL;
  119. foreach ($config['locale'] as $game => $locales) {
  120. if (isset($games[$game])) {
  121. foreach ($locales as $locale => $content) {
  122. if ($locale === '_all'
  123. || $locale === $config['default_locale']
  124. || !isset($internal[$game]['locale']['_all'][$locale])) {
  125. continue;
  126. }
  127. echo '> ' . $game . '.' . $locale . PHP_EOL;
  128. foreach ($internal[$game]['keys'] as $rKey) {
  129. if (!isset($content[$rKey])) {
  130. if (isset($internal[$game]['dynamic'][$rKey])) {
  131. $output[$game][$locale][$rKey] = createKeyValueList(
  132. $internal[$game]['dynamic'][$rKey],
  133. requestResource(
  134. $games[$game],
  135. $internal[$game]['dynamic'][$rKey]['method'],
  136. $locale,
  137. $internal[$game]['locale']['_all'][$locale]
  138. )
  139. );
  140. } else {
  141. $output[$game][$locale][$rKey] = $internal[$game]['default'][$rKey];
  142. }
  143. } else {
  144. $output[$game][$locale][$rKey] = $content[$rKey];
  145. }
  146. }
  147. }
  148. }
  149. }
  150. $header = <<<'EOD'
  151. <?php
  152. /**
  153. * This file is part of the bnetlib Library.
  154. * Copyright (c) 2012 Eric Boh <cossish@gmail.com>
  155. *
  156. * For the full copyright and license information, please view the LICENSE
  157. * file that was distributed with this source code. You can also view the
  158. * LICENSE file online at http://coss.github.com/bnetlib/license.html
  159. *
  160. * @see bin\LocaleGenerator.php
  161. *
  162. * @copyright 2012 Eric Boh <cossish@gmail.com>
  163. * @license http://coss.gitbub.com/bnetlib/license.html MIT License
  164. */
  165. return
  166. EOD;
  167. echo 'Write to file...' . PHP_EOL;
  168. foreach ($output as $game => $locales) {
  169. $path = sprintf($config['filepath'], DIRECTORY_SEPARATOR, $game, 'locale');
  170. $dirs = explode(DIRECTORY_SEPARATOR, $path);
  171. array_pop($dirs);
  172. $dir = implode(DIRECTORY_SEPARATOR, $dirs);
  173. if (!is_dir($dir)) {
  174. mkdir($dir, 0777, true);
  175. }
  176. foreach ($locales as $locale => $content) {
  177. $max = 0;
  178. foreach ($content as $key => $value) {
  179. ksort($content[$key]);
  180. foreach ($value as $sKey => $sValue) {
  181. $plus = (is_int($sKey)) ? 0 : 2;
  182. $max = max(strlen((string) $sKey) + $plus, $max);
  183. }
  184. }
  185. echo '> ' . $game . '.' . $locale . PHP_EOL;
  186. $data = str_replace("=> \n array", '=> array', var_export($content, true));
  187. $data = str_replace('array (', 'array(', $data);
  188. $data = str_replace(' ', ' ', $data);
  189. $data = preg_replace_callback(
  190. '/^(\s+)([^=]+)\s+=>/m',
  191. function ($match) use ($max) {
  192. return sprintf("%s%-{$max}s =>", $match[1], $match[2]);
  193. },
  194. $data
  195. );
  196. $file = sprintf($config['filepath'], DIRECTORY_SEPARATOR, $game, $locale);
  197. file_put_contents($file, sprintf('%s %s;', $header, $data));
  198. }
  199. }