-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteHandle.php
More file actions
191 lines (158 loc) · 6.78 KB
/
WriteHandle.php
File metadata and controls
191 lines (158 loc) · 6.78 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
/**
* Created by PhpStorm.
* User: QDFish
* Date: 2018/4/24
* Time: 下午6:04
*/
require_once "AnalysisAPI.php";
require_once "iOSModelConstant.php";
require_once "iOSTools.php";
require_once "ModelTemplate/ItemInterfaceTemplate.php";
require_once "ModelTemplate/ItemImplementationTemplate.php";
require_once "ModelTemplate/URLImplementationTemplate.php";
require_once "ModelTemplate/URLInterfaceTemplate.php";
require_once "ModelTemplate/RequestInterfaceTemplate.php";
require_once "ModelTemplate/RequestImplementationTemplate.php";
class WriteHandle
{
private $api;
//数据头文件名称
private $interfaceModelFilename;
//数据实现文件名称
private $implementationFilename;
public function __construct(AnalysisAPI $analysis)
{
$this->api = $analysis;
$this->initModelFile();
}
private function initModelFile() {
global $modelState; global $modelType;
$this->interfaceModelFilename = ucfirst($this->api->modelName) . $modelType[$modelState] . '.h';
$this->implementationFilename = ucfirst($this->api->modelName) . $modelType[$modelState] . '.m';
}
//写入数据模型头文件的头部信息
public function writeInterfaceFileHeader() {
global $desPath, $fileAnnotation, $classMap, $modelState;
$filePath = $desPath . DIRECTORY_SEPARATOR . $this->interfaceModelFilename;
$writeHandle = fopen($filePath, 'w+');
fwrite($writeHandle, $fileAnnotation);
fwrite($writeHandle, ($modelState === 0 ? FoundationFrameworkImport : HBHttpRequestImport) . "\n\n");
$this->writeForwardClass($writeHandle, $this->api->jsonData);
fwrite($writeHandle, PHP_EOL);
fclose($writeHandle);
echo "创建{$filePath}成功" . PHP_EOL;
}
//写入数据模型实现文件的头部信息
public function writeImplementationFileHeader() {
global $desPath, $fileAnnotation;
$filePath = $desPath . DIRECTORY_SEPARATOR . $this->implementationFilename;
$writeHandle = fopen($desPath . DIRECTORY_SEPARATOR . $this->implementationFilename, 'w+');
fwrite($writeHandle, $fileAnnotation);
fwrite($writeHandle, '#import "' . $this->interfaceModelFilename . "\"\n\n");
fclose($writeHandle);
echo "创建{$filePath}成功" . PHP_EOL;
}
public function writeToManager() {
$this->writeInterfaceFile();
$this->writeImplementation();
}
public function writeToModel() {
if ($this->api->jsonData['data']->value === null) {
return;
}
global $desPath;
$writeHandle = fopen($desPath . DIRECTORY_SEPARATOR . $this->interfaceModelFilename, 'a+');
$this->writeModelsToInteraface($this->api->jsonData, $writeHandle);
fclose($writeHandle);
echo "写入{$this->api->modelName}Item.h成功" . PHP_EOL;
$writeHandle = fopen($desPath . DIRECTORY_SEPARATOR . $this->implementationFilename, 'a+');
$this->writeModelsToImplementation($this->api->jsonData, $writeHandle);
fclose($writeHandle);
echo "写入{$this->api->modelName}Item.m成功" . PHP_EOL;
}
public function writeToRequest() {
global $desPath;
$filePath = $desPath . DIRECTORY_SEPARATOR . $this->interfaceModelFilename;
$writeHandle = fopen($filePath, 'a+');
fwrite($writeHandle, RequestInterfaceTemplate::template($this->api));
fclose($writeHandle);
echo "写入{$this->interfaceModelFilename}成功" . PHP_EOL;
$filePath = $desPath . DIRECTORY_SEPARATOR . $this->implementationFilename;
$writeHandle = fopen($filePath, 'a+');
fwrite($writeHandle, RequestImplementationTemplate::template($this->api));
fclose($writeHandle);
echo "写入{$this->interfaceModelFilename}成功" . PHP_EOL;
}
//写入.m文件循环递归
private function writeModelsToImplementation($jsonData, $handle) {
foreach ($jsonData as $key => $value) {
if (is_object($value) && get_class($value) === 'IOSObject') {
$_type = $value->type;
$_value = $value->value;
if (($iOSType = classMapContainType($_type)) !== false) {
fwrite($handle, ItemImplementationTemplate::template($this->api, $iOSType, $value));
$this->writeModelsToImplementation($_value, $handle);
}
}
}
}
//写入model头文件循环递归
private function writeModelsToInteraface($jsonData, $handle) {
foreach ($jsonData as $key => $value) {
if (is_object($value) && get_class($value) === 'IOSObject') {
$_type = $value->type;
$_value = $value->value;
if (($iOSType = classMapContainType($_type)) !== false) {
fwrite($handle, ItemInterfaceTemplate::template($this->api, $iOSType, $value));
$this->writeModelsToInteraface($_value, $handle);
}
}
}
}
//写入Url头文件
private function writeInterfaceFile() {
$writeHandle = fopen(UrlMangerInterfacePath, 'c+');
if ($writeHandle === false) {
echo 'writeURLInterface failure' . PHP_EOL;
exit(1);
}
while (!feof($writeHandle)) {
$buffer = fgets($writeHandle);
if ($buffer == "@end\n") {
fseek($writeHandle, -5, SEEK_CUR);
fwrite($writeHandle, URLInterfaceTemplate::template($this->api));
}
}
fclose($writeHandle);
echo "写入" . UrlMangerInterfacePath . "成功" . PHP_EOL;
}
//写入URL实现文件
private function writeImplementation() {
$writeHandle = fopen(UrlMangerImplementationPath, 'c+');
if ($writeHandle === false) {
echo 'writeURLImplementation failure' . PHP_EOL;
exit(1);
}
while (!feof($writeHandle)) {
$buffer = fgets($writeHandle);
if ($buffer == EndLabel) {
fseek($writeHandle, -5, SEEK_CUR);
fwrite($writeHandle, URLImplementationTemplate::template($this->api));
}
}
fclose($writeHandle);
echo "写入" . UrlMangerImplementationPath . "成功" . PHP_EOL;
}
private function writeForwardClass($handle, $jsonData) {
global $classMap;
foreach ($jsonData as $key => $value) {
if (is_object($value) && get_class($value) === 'IOSObject' && $value->value !== null) {
if (($forwardType = classMapContainType($value->type)) !== false) {
fwrite($handle, '@class ' . $forwardType . ';'. PHP_EOL);
$this->writeForwardClass($handle, $value->value);
}
}
}
}
}