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

/tools/librarygenerator.php

http://github.com/TheFrozenFire/PHP-Mindbody-API-Library
PHP | 173 lines | 101 code | 13 blank | 59 comment | 39 complexity | bb8769606718b77daa761e0c8a056028 MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. chdir(__DIR__."/..");
  3. function is_includeable($filename, $returnpaths = false) {
  4. $include_paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  5. foreach ($include_paths as $path) {
  6. $include = $path.DIRECTORY_SEPARATOR.$filename;
  7. if (is_file($include) && is_readable($include)) {
  8. if ($returnpaths == true) {
  9. $includable_paths[] = $path;
  10. } else {
  11. return true;
  12. }
  13. }
  14. }
  15. return (isset($includeable_paths) && $returnpaths == true) ? $includeable_paths : false;
  16. }
  17. function split_file($file, $dest) {
  18. $tokens = token_get_all(file_get_contents($file));
  19. $buffer = false;
  20. while ($token = next($tokens)) {
  21. if ($token[0] == T_CLASS) {
  22. $buffer = true;
  23. $name = null;
  24. $code = '';
  25. $braces = 1;
  26. do {
  27. $code .= is_string($token) ? $token : $token[1];
  28. if (is_array($token)
  29. && $token[0] == T_STRING
  30. && empty($name)) {
  31. $name = $token[1];
  32. }
  33. } while (!(is_string($token) && $token == '{') && $token = next($tokens));
  34. } elseif ($buffer) {
  35. if (is_string($token)) {
  36. $code .= $token;
  37. if ($token == '{') {
  38. $braces++;
  39. } elseif ($token == '}') {
  40. $braces--;
  41. if ($braces == 0) {
  42. $buffer = false;
  43. $file = $dest . '/' . $name . '.php';
  44. $code = '<?php' . PHP_EOL . $code . PHP_EOL . '?>';
  45. file_put_contents($file, $code);
  46. }
  47. }
  48. } else {
  49. $code .= $token[1];
  50. }
  51. }
  52. }
  53. }
  54. if(!class_exists("SoapClient")) die("SOAP is required for this script to function.");
  55. if(!class_exists("DOMDocument")) die("DOM is required for this script to function.");
  56. if(!class_exists("ReflectionClass")) die("Reflection is required for this script to function.");
  57. if(!is_includeable("wsdl2php.php")) die("wsdl2php is required for this script to function.");
  58. if(!ini_get('user_agent')) die("This script requires that your user_agent ini setting be configured. This is due to Mindbody blocking all requests from clients not providing a user_agent header.\nAn example would be: 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19'\n");
  59. $services = array(
  60. "https://api.mindbodyonline.com/0_5/AppointmentService.asmx?WSDL",
  61. "https://api.mindbodyonline.com/0_5/ClassService.asmx?WSDL",
  62. "https://api.mindbodyonline.com/0_5/ClientService.asmx?WSDL",
  63. "https://api.mindbodyonline.com/0_5/SiteService.asmx?WSDL",
  64. "https://api.mindbodyonline.com/0_5/SaleService.asmx?WSDL",
  65. "https://api.mindbodyonline.com/0_5/StaffService.asmx?WSDL",
  66. "https://api.mindbodyonline.com/0_5/DataService.asmx?WSDL",
  67. "https://api.mindbodyonline.com/0_5/FinderService.asmx?WSDL",
  68. );
  69. if(!is_dir("wsdl2phpoutput")) {
  70. mkdir("wsdl2phpoutput");
  71. }
  72. chdir("wsdl2phpoutput");
  73. foreach($services as $service) {
  74. $result = shell_exec("wsdl2php {$service}");
  75. if(!strpos($result, "Writing")) die("Failed to fetch {$service}.");
  76. }
  77. chdir("..");
  78. if(!is_dir("splitteroutput")) mkdir("splitteroutput");
  79. // Fix for MBO API 0.5 naming clusterfsck
  80. if(file_exists("wsdl2phpoutput/Class_x0020_Service.php")) {
  81. $file = file_get_contents("wsdl2phpoutput/Class_x0020_Service.php");
  82. if(strpos($file, "class Class {")) {
  83. $file = str_replace(
  84. array(
  85. "class Class {"
  86. ),
  87. array(
  88. "class MindbodyClass {"
  89. ),
  90. $file
  91. );
  92. file_put_contents("wsdl2phpoutput/Class_x0020_Service.php", $file);
  93. }
  94. }
  95. foreach(glob("wsdl2phpoutput/*.php") as $serviceFile) split_file($serviceFile, "splitteroutput");
  96. if(!is_dir("splitteroutput/structures")) mkdir("splitteroutput/structures");
  97. if(!is_dir("splitteroutput/services")) mkdir("splitteroutput/services");
  98. include_once("MindbodyAPI/MindbodyClient.php");
  99. foreach(array_merge(glob("splitteroutput/*_x0020_*.php"), array("splitteroutput/DataService.php")) as $serviceFile) {
  100. $oldServiceName = basename($serviceFile, ".php");
  101. $serviceName = str_replace("_x0020_", "", $oldServiceName);
  102. $endpoint = 'https://api.mindbodyonline.com/0_5/' . $serviceName . '.asmx';
  103. $file = file_get_contents($serviceFile);
  104. $constructorcode = <<<EOD
  105. if(!ini_get('user_agent')) ini_set('user_agent', 'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.6; en-US; rv:1.9.2.19) Gecko/20110707 Firefox/3.6.19');
  106. if(!isset(\$options['location'])) \$options['location'] = '$endpoint';
  107. parent::__construct(\$wsdl, \$options);
  108. EOD;
  109. $file = str_replace("<?php\nclass", "<?php\nnamespace MindbodyAPI\\services;use MindbodyAPI\\structures;\nclass", $file);
  110. $file = str_replace($oldServiceName, $serviceName, $file);
  111. $file = str_replace("class {$serviceName} extends SoapClient", "class {$serviceName} extends \MindbodyAPI\MindbodyClient", $file);
  112. $file = str_replace("public function {$serviceName}(", "public function __construct(", $file);
  113. $file = str_replace("parent::__construct(\$wsdl, \$options);", $constructorcode, $file);
  114. $file = str_replace("private static \$classmap = array(", "public static \$classmap = array(", $file);
  115. eval(trim($file, "<?php")); // Oh yes, I did that. Sue me. :P
  116. $reflection = new ReflectionClass('MindbodyAPI\\services\\'.$serviceName);
  117. $classmapProperty = $reflection->getProperty('classmap');
  118. $classmap = $classmapProperty->getValue();
  119. $mapReplacements = array(0 => array(), 1 => array());
  120. foreach($classmap as $key => $value) {
  121. $mapReplacements[0][] = "'{$key}' => '{$value}'";
  122. $mapReplacements[0][] = "'Class' => 'Class',";
  123. $mapReplacements[0][] = "public function {$value}({$value}";
  124. $mapReplacements[1][] = "'{$key}' => 'MindbodyAPI\structures\\{$value}'";
  125. $mapReplacements[1][] = "'Class' => 'MindbodyAPI\structures\\MindbodyClass',";
  126. $mapReplacements[1][] = "public function {$value}(structures\\{$value}";
  127. }
  128. $file = str_replace($mapReplacements[0], $mapReplacements[1], $file);
  129. file_put_contents($serviceFile, $file);
  130. rename($serviceFile, "splitteroutput/services/{$serviceName}.php");
  131. }
  132. foreach(glob("splitteroutput/*.php") as $structureFile) {
  133. $file = file_get_contents($structureFile);
  134. $file = str_replace("<?php\nclass", "<?php\nnamespace MindbodyAPI\\structures;\nclass", $file);
  135. file_put_contents($structureFile, $file);
  136. rename($structureFile, "splitteroutput/structures/".basename($structureFile));
  137. }
  138. include_once("PHP/Beautifier.php");
  139. if(class_exists('PHP_Beautifier')) {
  140. $beautifier = new PHP_Beautifier();
  141. $beautifier->addFilter('ArrayNested');
  142. $beautifier->setIndentChar("\t");
  143. $beautifier->setIndentNumber(1);
  144. foreach(glob("splitteroutput/*/*.php") as $file) {
  145. $beautifier->setInputFile($file);
  146. $beautifier->setOutputFile($file);
  147. $beautifier->process();
  148. $beautifier->save();
  149. }
  150. }