-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_core_operations.php
125 lines (100 loc) · 3.64 KB
/
generate_core_operations.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
use NovakSolutions\FrontDesk\Generate;
use Doctrine\Common\Inflector;
require_once('/vendor/autoload.php');
$coreParser = new Generate\CoreParser();
$coreApiDocsAsHtml = http_get_contents("https://developer.frontdeskhq.com/docs/api/v2");
$coreDefinitions = $coreParser->extractDataFromHtml($coreApiDocsAsHtml);
//Process Definitions Info Select, Update, Insert, etc...
/** @var mixed $models */
$models = array();
$loader = new Twig_Loader_Filesystem('src/Generate/Templates');
$twig = new Twig_Environment($loader);
$twig->addExtension(new Twig_Extension_Debug());
$twig->setCache(false);
$function = new Twig_SimpleFunction('descriptionToPhpDocType',
function ($type) {
switch($type){
case 'boolean':
return "boolean";
default:
return $type;
}
}
);
$twig->addFunction($function);
$rpad10Filter = new Twig_SimpleFilter("rpad10", function($string){
return str_pad($string, 10, " ", STR_PAD_RIGHT);
});
$rpad20Filter = new Twig_SimpleFilter("rpad20", function($string){
return str_pad($string, 20, " ", STR_PAD_RIGHT);
});
$twig->addFilter($rpad10Filter);
$twig->addFilter($rpad20Filter);
$template = $twig->loadTemplate('core_operation.twig');
$endPoints = array();
foreach($coreDefinitions as $objectName => $objectDetails){
$standardMethods = array();
$selectMethod = null;
foreach($objectDetails['methods'] as $method){
//The Account operation is neither front nor desk.
$frontOrDesk = '';
if(strpos($method['url'], "/front/") !== false){
$frontOrDesk = '\Front';
} elseif (strpos($method['url'], "/desk/") !== false){
$frontOrDesk = '\Desk';
} else {
$frontOrDesk = '\Account';
}
$className = \NovakSolutions\FrontDesk\Generate\CoreParser::buildOperationNameFromEndPoint($method['httpMethod'], $method['url']);
$urlArguments = getUrlArguments($method['url']);
$fileName = "src/Operation/{$frontOrDesk}/" . $className . '.php';
if(file_exists($fileName)){
unlink($fileName);
}
if(!file_exists("src/Operation/{$frontOrDesk}/")){
mkdir("src/Operation/{$frontOrDesk}/");
}
if($frontOrDesk == '\Account'){
$noSubdomain = "true";
} else {
$noSubdomain = "false";
}
$renderedTemplate = $template->render(compact('noSubdomain', 'method', 'frontOrDesk', 'className', 'urlArguments'));
file_put_contents($fileName, $renderedTemplate);
}
}
echo implode("\n", $endPoints);
function http_get_contents($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if(FALSE === ($retval = curl_exec($ch))) {
error_log(curl_error($ch));
} else {
return $retval;
}
}
function getStandardMethodsForObject($objectName, $definitions){
//foreach($definitions[''])
}
function getUrlArguments($endPointUrl){
$urlArguments = array();
$endPointUrl = str_replace("/api/v2/", "", $endPointUrl);
if(strpos($endPointUrl, ".json") !== false){
$endPointUrl = str_replace(".json", "", $endPointUrl);
}
if(strpos($endPointUrl, '?') !== false){
$urlParts = explode("?", $endPointUrl);
$endPointUrl = array_shift($urlParts);;
}
$urlPieces = explode("/", $endPointUrl);
foreach($urlPieces as $urlPiece){
if(strpos($urlPiece, ":") === 0){
$urlArguments = str_replace(":", "", $urlPiece);
}
}
return $urlArguments;
}