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

/create_monolith_2.2.php

https://github.com/shish/shimmie2-utils
PHP | 213 lines | 100 code | 18 blank | 95 comment | 9 complexity | b914f06d4f45237686abb3ccc344a31a MD5 | raw file
  1. <?php
  2. define("THEME", "default");
  3. define("PHP4_COMPAT", false);
  4. define("AGGRESSIVE", true);
  5. if(!file_exists("config.php")) {die("config.php not found");}
  6. function striphp($text) {
  7. $text = preg_replace('/^<\?php/', '', $text);
  8. $text = preg_replace('/\?>$/', '', $text);
  9. $text = preg_replace('#function send_event(.|\s)*?\n}#m', '
  10. function send_event(Event $event) {
  11. global $_event_listeners, $_event_count, $_event_routes, $_event_listener_map;
  12. if(empty($_event_listener_map)) {
  13. $_event_listener_map = array();
  14. foreach($_event_listeners as $extension) {
  15. $_event_listener_map[get_class($extension)] = $extension;
  16. }
  17. }
  18. $my_event_listeners = $_event_routes[get_class($event)];
  19. ksort($my_event_listeners);
  20. foreach($my_event_listeners as $listener) {
  21. $_event_listener_map[$listener]->receive_event($event);
  22. }
  23. $_event_count++;
  24. }
  25. ', $text);
  26. if(AGGRESSIVE) {
  27. $text = preg_replace('#\n\s*/\*(.|\s)*?\*/#m', '', $text); /* ... */
  28. // "//-->" is important
  29. //$text = preg_replace('#\n\s+//.*#', '', $text); // ...
  30. $text = preg_replace('/\n\s+#.*/', '', $text); # ...
  31. $text = preg_replace('/\n\s*\n/', "\n", $text);
  32. }
  33. // most requires are built-in, but we want /lib separately
  34. $text = str_replace('require_', '// require_', $text);
  35. $text = str_replace('// require_once "lib', 'require_once "lib', $text);
  36. // php4 fails
  37. if(PHP4_COMPAT) {
  38. $text = str_replace('public function', 'function', $text);
  39. $text = str_replace('protected function', 'function', $text);
  40. $text = str_replace('private function', 'function', $text);
  41. $text = str_replace('$event->get_panel()->add_side_block($sb);',
  42. '$panel=$event->get_panel(); $panel->add_side_block($sb);', $text);
  43. }
  44. return $text;
  45. }
  46. function get_events($text) {
  47. $matches = array();
  48. preg_match_all("/\s([a-zA-Z]+Event)/", $text, $matches);
  49. $events = $matches[1];
  50. preg_match_all("/\son([a-zA-Z]+)\(/", $text, $matches);
  51. $handlers = array_unique($matches[1]);
  52. foreach($handlers as $handler) {
  53. $events[] = $handler."Event";
  54. }
  55. if(preg_match("/\sformat\(/", $text)) {
  56. $events[] = "TextFormattingEvent";
  57. }
  58. if(preg_match('#\nclass (.*) extends DataHandlerExtension#', $text, $matches)) {
  59. $events[] = "DataUploadEvent";
  60. $events[] = "ThumbnailGenerationEvent";
  61. $events[] = "DisplayingImageEvent";
  62. }
  63. return array_unique($events);
  64. }
  65. function get_prio($text) {
  66. $matches = array();
  67. if(preg_match('#\nclass (.*) extends SimpleExtension#m', $text, $matches)) {
  68. return 50;
  69. }
  70. if(preg_match('#add_event_listener\(new .*, (\d+)\);#', $text, $matches)) {
  71. return $matches[1];
  72. }
  73. return 50;
  74. }
  75. function get_ext($text) {
  76. $matches = array();
  77. preg_match('#\nclass (.*) (extends|implements) .*Extension#m', $text, $matches);
  78. if($matches) return $matches[1];
  79. else return null;
  80. }
  81. function invert_map($map) {
  82. $map2 = array();
  83. foreach($map as $ext_name => $info) {
  84. foreach($info["events"] as $event) {
  85. $pos = $info["prio"];
  86. while(isset($map2[$event][$pos])) {
  87. $pos++;
  88. }
  89. $map2[$event][$pos] = $ext_name;
  90. }
  91. }
  92. return $map2;
  93. }
  94. $text = "<?php
  95. error_reporting(E_ALL);
  96. assert_options(ASSERT_ACTIVE, 1);
  97. assert_options(ASSERT_BAIL, 1);
  98. ";
  99. $files = array_merge(
  100. glob("core/*.php"),
  101. glob("ext/*/main.php"),
  102. glob("themes/".THEME."/*.class.php"),
  103. glob("ext/*/theme.php"),
  104. glob("themes/".THEME."/*.theme.php")
  105. );
  106. $routes = array();
  107. foreach($files as $filename) {
  108. print "Converting $filename...\n";
  109. $text .= "\n\n// Original: $filename\n";
  110. $data = striphp(file_get_contents($filename));
  111. $ext = get_ext($data);
  112. if($ext) {
  113. $prio = get_prio($data);
  114. $events = get_events($data);
  115. $routes[$ext] = array("prio"=>$prio, "events"=>$events);
  116. }
  117. $text .= $data;
  118. }
  119. //var_export(invert_map($routes));
  120. $text .= '
  121. define("DEBUG", true);
  122. define("COVERAGE", true);
  123. define("CACHE_MEMCACHE", false);
  124. define("CACHE_DIR", false);
  125. define("VERSION", "trunk");
  126. define("SCORE_VERSION", "s2hack/".VERSION);
  127. define("COOKIE_PREFIX", "shm");
  128. '.striphp(file_get_contents("config.php")).'
  129. $_event_routes = '.var_export(invert_map($routes), true).';
  130. if(COVERAGE) {_start_coverage();}
  131. _version_check();
  132. _sanitise_environment();
  133. _start_cache();
  134. try {
  135. // connect to the database
  136. $database = new Database();
  137. $database->db->fnExecute = "_count_execs";
  138. $config = new DatabaseConfig($database);
  139. // initialise the extensions
  140. foreach(get_declared_classes() as $class) {
  141. if(is_subclass_of($class, "SimpleExtension")) {
  142. $c = new $class();
  143. $c->i_am($c);
  144. add_event_listener($c);
  145. }
  146. }
  147. // start the page generation waterfall
  148. $page = class_exists("CustomPage") ? new CustomPage() : new Page();
  149. $user = _get_user($config, $database);
  150. send_event(new InitExtEvent());
  151. send_event(_get_page_request());
  152. $page->display();
  153. // for databases which support transactions
  154. if($database->engine->name != "sqlite") {
  155. $database->db->CommitTrans(true);
  156. }
  157. _end_cache();
  158. }
  159. catch(Exception $e) {
  160. $version = VERSION;
  161. $message = $e->getMessage();
  162. header("HTTP/1.0 500 Internal Error");
  163. print <<<EOD
  164. <html>
  165. <head>
  166. <title>Internal error - SCore-$version</title>
  167. </head>
  168. <body>
  169. <h1>Internal Error</h1>
  170. <p>$message
  171. </body>
  172. </html>
  173. EOD;
  174. }
  175. if(COVERAGE) {_end_coverage();}
  176. ?>';
  177. print "Writing monolith.php\n";
  178. file_put_contents("monolith.php", $text);
  179. ?>