Skip to content

Commit a038537

Browse files
Merge pull request #463 from appwrite/feat-refactor-templates
2 parents bc37094 + 341d654 commit a038537

File tree

4 files changed

+103
-99
lines changed

4 files changed

+103
-99
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
2+
{% for parameter in method.parameters.header %}
3+
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
4+
{% endfor %}
5+
{% for key, header in method.headers %}
6+
'{{ key }}' => '{{ header }}',
7+
{% endfor %}
8+
], $params);
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
{% if 'multipart/form-data' in method.consumes %}
2+
{% for parameter in method.parameters.all %}
3+
{% if parameter.type == 'file' %}
4+
$size = filesize(${{ parameter.name | caseCamel }});
5+
$mimeType = mime_content_type(${{ parameter.name | caseCamel }});
6+
$postedName = basename(${{ parameter.name | caseCamel }});
7+
//send single file if size is less than or equal to 5MB
8+
if ($size <= Client::CHUNK_SIZE) {
9+
$params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName);
10+
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
11+
{% for param in method.parameters.header %}
12+
'{{ param.name }}' => ${{ param.name | caseCamel }},
13+
{% endfor %}
14+
{% for key, header in method.headers %}
15+
'{{ key }}' => '{{ header }}',
16+
{% endfor %}
17+
], $params);
18+
}
19+
20+
$id = '';
21+
$counter = 0;
22+
23+
{% for parameter in method.parameters.all %}
24+
{% if parameter.isUploadID %}
25+
if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
26+
try {
27+
$response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers);
28+
$counter = $response['chunksUploaded'] ?? 0;
29+
} catch(\Exception $e) {
30+
}
31+
}
32+
{% endif %}
33+
{% endfor %}
34+
35+
$headers = ['content-type' => 'multipart/form-data'];
36+
$handle = @fopen(${{ parameter.name | caseCamel }}, "rb");
37+
$start = $counter * Client::CHUNK_SIZE;
38+
while ($start < $size) {
39+
fseek($handle, $start);
40+
$params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
41+
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
42+
if(!empty($id)) {
43+
$headers['x-{{spec.title | caseLower }}-id'] = $id;
44+
}
45+
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
46+
$counter++;
47+
$start += Client::CHUNK_SIZE;
48+
if(empty($id)) {
49+
$id = $response['$id'];
50+
}
51+
if($onProgress !== null) {
52+
$onProgress([
53+
'$id' => $response['$id'],
54+
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
55+
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
56+
'chunksTotal' => $response['chunksTotal'],
57+
'chunksUploaded' => $response['chunksUploaded'],
58+
]);
59+
}
60+
}
61+
@fclose($handle);
62+
return $response;
63+
{% endif %}
64+
{% endfor %}
65+
{% endif %}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$params = [];
2+
{% if method.parameters.all | length %}
3+
{% for parameter in method.parameters.all %}
4+
{% if parameter.required %}
5+
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
6+
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
7+
}
8+
{% endif %}
9+
{% endfor %}
10+
{% for parameter in method.parameters.query %}
11+
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
12+
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
13+
}
14+
15+
{% endfor %}
16+
{% for parameter in method.parameters.body %}
17+
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
18+
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
19+
}
20+
21+
{% endfor %}
22+
{% for parameter in method.parameters.formData %}
23+
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
24+
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
25+
}
26+
{% endfor %}
27+
{% endif %}

templates/php/src/Services/Service.php.twig

Lines changed: 3 additions & 99 deletions
Original file line numberDiff line numberDiff line change
@@ -24,110 +24,14 @@ class {{ service.name | caseUcfirst }} extends Service
2424
2525
*/
2626
public function {{ method.name | caseCamel }}({% for parameter in method.parameters.all %}{{ parameter.type | typeName }}${{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %} = null{% endif %}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, callable $onProgress = null{% endif %}): {{ method | getReturn }}
27-
2827
{
29-
{% for parameter in method.parameters.all %}
30-
{% if parameter.required %}
31-
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
32-
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
33-
}
34-
35-
{% endif %}
36-
{% endfor %}
3728
$path = str_replace([{% for parameter in method.parameters.path %}'{{ '{' }}{{ parameter.name | caseCamel }}{{ '}' }}'{% if not loop.last %}, {% endif %}{% endfor %}], [{% for parameter in method.parameters.path %}${{ parameter.name | caseCamel | escapeKeyword }}{% if not loop.last %}, {% endif %}{% endfor %}], '{{ method.path }}');
38-
$params = [];
39-
40-
{% for parameter in method.parameters.query %}
41-
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
42-
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
43-
}
4429
45-
{% endfor %}
46-
{% for parameter in method.parameters.body %}
47-
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
48-
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
49-
}
50-
51-
{% endfor %}
52-
{% for parameter in method.parameters.formData %}
53-
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
54-
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
55-
}
56-
57-
{% endfor %}
30+
{{ include('php/base/requests/params.twig') }}
5831
{% if 'multipart/form-data' in method.consumes %}
59-
{% for parameter in method.parameters.all %}
60-
{% if parameter.type == 'file' %}
61-
$size = filesize(${{ parameter.name | caseCamel }});
62-
$mimeType = mime_content_type(${{ parameter.name | caseCamel }});
63-
$postedName = basename(${{ parameter.name | caseCamel }});
64-
//send single file if size is less than or equal to 5MB
65-
if ($size <= Client::CHUNK_SIZE) {
66-
$params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName);
67-
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
68-
{% for param in method.parameters.header %}
69-
'{{ param.name }}' => ${{ param.name | caseCamel }},
70-
{% endfor %}
71-
{% for key, header in method.headers %}
72-
'{{ key }}' => '{{ header }}',
73-
{% endfor %}
74-
], $params);
75-
}
76-
77-
$id = '';
78-
$counter = 0;
79-
80-
{% for parameter in method.parameters.all %}
81-
{% if parameter.isUploadID %}
82-
if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
83-
try {
84-
$response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers);
85-
$counter = $response['chunksUploaded'] ?? 0;
86-
} catch(\Exception $e) {
87-
}
88-
}
89-
{% endif %}
90-
{% endfor %}
91-
92-
$headers = ['content-type' => 'multipart/form-data'];
93-
$handle = @fopen(${{ parameter.name | caseCamel }}, "rb");
94-
$start = $counter * Client::CHUNK_SIZE;
95-
while ($start < $size) {
96-
fseek($handle, $start);
97-
$params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
98-
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
99-
if(!empty($id)) {
100-
$headers['x-{{spec.title | caseLower }}-id'] = $id;
101-
}
102-
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
103-
$counter++;
104-
$start += Client::CHUNK_SIZE;
105-
if(empty($id)) {
106-
$id = $response['$id'];
107-
}
108-
if($onProgress !== null) {
109-
$onProgress([
110-
'$id' => $response['$id'],
111-
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
112-
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
113-
'chunksTotal' => $response['chunksTotal'],
114-
'chunksUploaded' => $response['chunksUploaded'],
115-
]);
116-
}
117-
}
118-
@fclose($handle);
119-
return $response;
120-
{% endif %}
121-
{% endfor %}
32+
{{ include('php/base/requests/file.twig') }}
12233
{% else %}
123-
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
124-
{% for parameter in method.parameters.header %}
125-
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
126-
{% endfor %}
127-
{% for key, header in method.headers %}
128-
'{{ key }}' => '{{ header }}',
129-
{% endfor %}
130-
], $params);
34+
{{ include('php/base/requests/api.twig') }}
13135
{% endif %}
13236
}
13337
{% if not loop.last %}

0 commit comments

Comments
 (0)