PageRenderTime 48ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/FixtureGenerator.php

https://github.com/bforchhammer/bnetlib
PHP | 193 lines | 157 code | 24 blank | 12 comment | 23 complexity | 63662d3a11b794efdec730241c858d63 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 (!file_exists(__DIR__ . '/FixtureConfig.php')) {
  15. die('Unable to load config file.');
  16. }
  17. $config = include __DIR__ . '/FixtureConfig.php';
  18. if (!file_exists(dirname(__DIR__) . '/tests/_autoload.php')) {
  19. die('Unable to load autoloader.');
  20. }
  21. function requestResource($game, $method, $args) {
  22. return call_user_func_array(array($game, 'get' . $method), $args);
  23. }
  24. include dirname(__DIR__) . '/tests/_autoload.php';
  25. $games = array();
  26. $output = array();
  27. $queue = array();
  28. $specialRes = array();
  29. $registry = array();
  30. $resourceMap = array();
  31. $connection = new bnetlib\Connection\ZendFramework();
  32. $locator = new bnetlib\ServiceLocator\ServiceLocator();
  33. $defArgs = (isset($config['config'])) ? $config['config'] : array();
  34. $connection->setOptions($defArgs);
  35. echo 'Create Game instances...' . PHP_EOL;
  36. foreach ($config['games'] as $game => $class) {
  37. $output[$game] = array();
  38. $games[$game] = new $class($connection, $locator);
  39. $games[$game]->setReturnType(bnetlib\AbstractGame::RETURN_PLAIN);
  40. $reflection = new ReflectionClass($games[$game]);
  41. $resourceMap[$game] = $reflection->getProperty('resources');
  42. $resourceMap[$game]->setAccessible(true);
  43. $resourceMap[$game] = $resourceMap[$game]->getValue($games[$game]);
  44. }
  45. echo 'Create request queue...' . PHP_EOL . PHP_EOL;
  46. foreach ($config['fixtures'] as $game => $requests) {
  47. if ($game === '_extend') {
  48. continue;
  49. }
  50. if (!isset($games[$game])) {
  51. continue;
  52. }
  53. $consumes = array();
  54. $queue[$game] = array();
  55. $specialRes[$game] = array();
  56. $registry[$game] = array();
  57. foreach ($requests as $resource => $arguments) {
  58. if (is_string($arguments)) {
  59. $specialRes[$game][] = $arguments;
  60. $consumes[] = array($resource, $arguments);
  61. } else {
  62. $queue[$game][] = array($resource, $arguments);
  63. }
  64. }
  65. foreach ($consumes as $value) {
  66. $queue[$game][] = $value;
  67. }
  68. }
  69. echo 'Request data...' . PHP_EOL;
  70. foreach ($queue as $game => $requests) {
  71. if ($game === '_extend') {
  72. continue;
  73. }
  74. if (!isset($games[$game])) {
  75. echo $game . 'not found!' . PHP_EOL;
  76. continue;
  77. }
  78. foreach ($requests as $id => $job) {
  79. list($resource, $arguments) = $job;
  80. if ($arguments === null) {
  81. $arguments = array();
  82. }
  83. if (is_string($arguments)) {
  84. $arguments = $registry[$game][$arguments];
  85. }
  86. printf('> %s::get%s... ', get_class($games[$game]), $resource);
  87. try {
  88. $output[$game][$resource] = requestResource(
  89. $games[$game],
  90. $resource,
  91. array($arguments)
  92. );
  93. if (substr($output[$game][$resource]['headers']['Content-Type'], 0, 5) === 'image') {
  94. $output[$game][$resource]['content'] = 'base64,'
  95. . base64_encode($output[$game][$resource]['content']);
  96. }
  97. if (in_array($resource, $specialRes[$game])) {
  98. $obj = $locator->get($resourceMap[$game][$resource]);
  99. $obj->populate($output[$game][$resource]['content']);
  100. $registry[$game][$resource] = $obj;
  101. }
  102. echo 'done!' . PHP_EOL;
  103. } catch (bnetlib\Exception\ExceptionInterface $e) {
  104. echo 'skipped!' . PHP_EOL;
  105. printf('>> %s' . PHP_EOL, $e->getMessage());
  106. continue;
  107. }
  108. }
  109. }
  110. if (isset($config['fixtures']['_extend'])) {
  111. echo PHP_EOL. 'Extend data...' . PHP_EOL;
  112. foreach ($config['fixtures']['_extend'] as $game => $list) {
  113. if (!isset($games[$game])) {
  114. echo $game . 'not found!' . PHP_EOL;
  115. continue;
  116. }
  117. foreach ($list as $i => $extend) {
  118. $error = false;
  119. if (!isset($extend['source'])) {
  120. $error = true;
  121. printf('> No source found in %s:%d' . PHP_EOL, $game, $i);
  122. } else {
  123. if (!isset($extend['source']['resource'])) {
  124. $error = true;
  125. printf('> No source resource found in %s:%d' . PHP_EOL, $game, $i);
  126. }
  127. if (!isset($extend['source']['arguments'])) {
  128. $extend['source']['arguments'] = array();
  129. }
  130. }
  131. if (!isset($extend['target'])) {
  132. $error = true;
  133. printf('> No target found in %s:%d' . PHP_EOL, $game, $i);
  134. }
  135. if ($error) {
  136. continue;
  137. }
  138. printf('> %s::get%s... ', get_class($games[$game]), $extend['source']['resource']);
  139. try {
  140. $tmp = requestResource(
  141. $games[$game],
  142. $extend['source']['resource'],
  143. array($extend['source']['arguments'])
  144. );
  145. if (!isset($tmp['content'][$extend['target']])) {
  146. echo 'error, target not found!' . PHP_EOL;
  147. } else {
  148. $output[$game][$extend['source']['resource']]['content'][$extend['target']] =
  149. $tmp['content'][$extend['target']];
  150. echo 'done!' . PHP_EOL;
  151. }
  152. } catch (bnetlib\Exception\ExceptionInterface $e) {
  153. echo 'skipped!' . PHP_EOL;
  154. printf('>> %s' . PHP_EOL, $e->getMessage());
  155. continue;
  156. }
  157. }
  158. }
  159. }
  160. echo PHP_EOL . 'Write to files...' . PHP_EOL;
  161. foreach ($output as $game => $resources) {
  162. if (!is_dir($config['path'] . '/' . $game)) {
  163. mkdir($config['path'] . '/' . $game, 0777, true);
  164. }
  165. foreach ($resources as $resource => $content) {
  166. $file = sprintf('%s/%s/%s.json', $config['path'], $game, strtolower($resource));
  167. file_put_contents($file, json_encode($content, JSON_PRETTY_PRINT));
  168. }
  169. }