1<?php 2 3$cwd = dirname($argv[0]) . "/../../../src"; 4chdir($cwd); 5 6$cmd = "grep -r -l 'Generated by the protocol buffer' * | grep 'php$' | grep -v Internal"; 7$handle = popen($cmd, 'r'); 8$filenames = explode("\n", stream_get_contents($handle)); 9array_pop($filenames); // empty string after last '\n' 10$filenames[] = "Google/Protobuf/DescriptorPool.php"; 11$output = "../ext/google/protobuf/bundled_php.c"; 12 13function stripSuffix($str, $suffix) { 14 return substr($str, 0, strlen($str) - strlen($suffix)); 15} 16 17function toClassName($filename) { 18 # Google/Protobuf/BoolValue.php -> Google\\Protobuf\\BoolValue 19 $ret = stripSuffix($filename, ".php"); 20 return str_replace("/", "\\\\", $ret); 21} 22 23function toCSymbolName($filename) { 24 # Google/Protobuf/BoolValue.php -> Google__Protobuf__BoolValue 25 $ret = stripSuffix($filename, ".php"); 26 return str_replace("/", "__", $ret); 27} 28 29$f = fopen($output, "w"); 30 31fwrite($f, "#include \"bundled_php.h\"\n"); 32fwrite($f, "#include \"stdlib.h\"\n"); 33 34foreach ($filenames as $filename) { 35 print("Reading $filename...\n"); 36 $contents = file_get_contents($filename); 37 $contents = substr($contents, 5); // Strip <?php 38 $c_symbol_name = toCSymbolName($filename); 39 fwrite($f, "static const char {$c_symbol_name}[] = {"); 40 for ($i = 0; $i < strlen($contents); $i++) { 41 if ($i % 10 == 0) { 42 fwrite($f, "\n"); 43 } 44 fprintf($f, " 0x%02x,", ord($contents[$i])); 45 } 46 fwrite($f, "0};\n"); 47} 48 49fwrite($f, "static BundledPhp_File php[] = {\n"); 50foreach ($filenames as $filename) { 51 $class_name = toClassName($filename); 52 $c_symbol_name = toCSymbolName($filename); 53 fwrite($f, " {\"$class_name\", $c_symbol_name},\n"); 54} 55 56fwrite($f, " {NULL, NULL}\n"); 57fwrite($f, "};\n"); 58fwrite($f, "BundledPhp_File *bundled_files = &php[0];\n"); 59fclose($f); 60 61print("Wrote $output\n"); 62?> 63