forked from TheFrozenFire/PHP-Mindbody-API-Library
/
librarygenerator.php
125 lines (124 loc) · 5.16 KB
/
librarygenerator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
function is_includeable($filename, $returnpaths = false) {
$include_paths = explode(PATH_SEPARATOR, ini_get('include_path'));
foreach ($include_paths as $path) {
$include = $path . DIRECTORY_SEPARATOR . $filename;
if (is_file($include) && is_readable($include)) {
if ($returnpaths == true) {
$includable_paths[] = $path;
} else {
return true;
}
}
}
return (isset($includeable_paths) && $returnpaths == true) ? $includeable_paths : false;
}
function split_file($file, $dest) {
$tokens = token_get_all(file_get_contents($file));
$buffer = false;
while ($token = next($tokens)) {
if ($token[0] == T_CLASS) {
$buffer = true;
$name = null;
$code = '';
$braces = 1;
do {
$code.= is_string($token) ? $token : $token[1];
if (is_array($token) && $token[0] == T_STRING && empty($name)) {
$name = $token[1];
}
} while (!(is_string($token) && $token == '{') && $token = next($tokens));
} elseif ($buffer) {
if (is_string($token)) {
$code.= $token;
if ($token == '{') {
$braces++;
} elseif ($token == '}') {
$braces--;
if ($braces == 0) {
$buffer = false;
$file = $dest . '/' . $name . '.php';
$code = '<?php' . PHP_EOL . $code . PHP_EOL . '?>';
file_put_contents($file, $code);
}
}
} else {
$code.= $token[1];
}
}
}
} if (!class_exists("SoapClient")) die("SOAP is required for this script to function.");
if (!class_exists("DOMDocument")) die("DOM is required for this script to function.");
if (!is_includeable("wsdl2php.php")) die("wsdl2php is required for this script to function.");
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");
$services = array(
"https://api.mindbodyonline.com/0_5/AppointmentService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/ClassService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/ClientService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/SiteService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/SaleService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/StaffService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/DataService.asmx?WSDL",
"https://api.mindbodyonline.com/0_5/FinderService.asmx?WSDL",
);
if (!is_dir("wsdl2phpoutput")) {
mkdir("wsdl2phpoutput");
}
chdir("wsdl2phpoutput");
foreach ($services as $service) {
$result = shell_exec("wsdl2php {$service}");
if (!strpos($result, "Writing")) die("Failed to fetch {$service}.");
}
chdir("..");
if (!is_dir("splitteroutput")) mkdir("splitteroutput");
// Fix for MBO API 0.5 naming clusterfsck
foreach (glob("wsdl2phpoutput/*.php") as $serviceFile) {
$contents = file_get_contents($serviceFile);
file_put_contents($serviceFile, str_replace(array(
"class Class {",
"'Class' => 'Class',"
) , array(
"class Mindbody_Class {",
"'Class' => 'Mindbody_Class',"
) , $contents));
split_file($serviceFile, "splitteroutput");
}
if (!is_dir("splitteroutput/structures")) mkdir("splitteroutput/structures");
if (!is_dir("splitteroutput/services")) mkdir("splitteroutput/services");
foreach (glob("splitteroutput/*_x0020_*.php") as $serviceFile) {
$oldServiceName = basename($serviceFile, ".php");
$serviceName = str_replace("_x0020", "", $oldServiceName);
$file = file_get_contents($serviceFile);
$constructorcode = "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');\n";
if ($serviceName == 'Sale_Service') {
$constructorcode.= " if(empty(\$options['location'])) \$options['location'] = 'https://api.mindbodyonline.com/0_5/SaleService.asmx';\n";
}
$constructorcode.= " \$directory = dirname(__FILE__).DIRECTORY_SEPARATOR;\n";
$constructorcode.= " foreach(\$options['classmap'] as \$key => \$value) if(file_exists('{\$directory}../structures/{\$value}.php')) include_once('{\$directory}../structures/{\$value}.php');\n";
$constructorcode.= " parent::__construct(\$wsdl, \$options);";
$aliascode = <<<EOD
class_alias("{$serviceName}", "{$oldServiceName}");
?>
EOD;
$file = str_replace($oldServiceName, $serviceName, $file);
$file = str_replace("parent::__construct(\$wsdl, \$options);", $constructorcode, $file);
$file = str_replace("?>", $aliascode, $file);
file_put_contents($serviceFile, $file);
rename($serviceFile, "splitteroutput/services/{$serviceName}.php");
file_put_contents("splitteroutput/services/${oldServiceName}.php", "<?php include_once(\"${serviceName}.php\"); ?>\n");
}
foreach (glob("splitteroutput/*.php") as $structureFile) rename($structureFile, "splitteroutput/structures/" . basename($structureFile));
include_once ("PHP/Beautifier.php");
if (class_exists('PHP_Beautifier')) {
$beautifier = new PHP_Beautifier();
$beautifier->addFilter('ArrayNested');
$beautifier->setIndentChar("\t");
$beautifier->setIndentNumber(1);
foreach (glob("splitteroutput/*/*.php") as $file) {
$beautifier->setInputFile($file);
$beautifier->setOutputFile($file);
$beautifier->process();
$beautifier->save();
}
}
?>