Skip to content

Commit

Permalink
Merge pull request #463 from appwrite/feat-refactor-templates
Browse files Browse the repository at this point in the history
  • Loading branch information
lohanidamodar authored Jun 21, 2022
2 parents bc37094 + 341d654 commit a038537
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 99 deletions.
8 changes: 8 additions & 0 deletions templates/php/base/requests/api.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
{% for parameter in method.parameters.header %}
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}' => '{{ header }}',
{% endfor %}
], $params);
65 changes: 65 additions & 0 deletions templates/php/base/requests/file.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{% if 'multipart/form-data' in method.consumes %}
{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
$size = filesize(${{ parameter.name | caseCamel }});
$mimeType = mime_content_type(${{ parameter.name | caseCamel }});
$postedName = basename(${{ parameter.name | caseCamel }});
//send single file if size is less than or equal to 5MB
if ($size <= Client::CHUNK_SIZE) {
$params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName);
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
{% for param in method.parameters.header %}
'{{ param.name }}' => ${{ param.name | caseCamel }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}' => '{{ header }}',
{% endfor %}
], $params);
}

$id = '';
$counter = 0;

{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
try {
$response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers);
$counter = $response['chunksUploaded'] ?? 0;
} catch(\Exception $e) {
}
}
{% endif %}
{% endfor %}

$headers = ['content-type' => 'multipart/form-data'];
$handle = @fopen(${{ parameter.name | caseCamel }}, "rb");
$start = $counter * Client::CHUNK_SIZE;
while ($start < $size) {
fseek($handle, $start);
$params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
if(!empty($id)) {
$headers['x-{{spec.title | caseLower }}-id'] = $id;
}
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
$counter++;
$start += Client::CHUNK_SIZE;
if(empty($id)) {
$id = $response['$id'];
}
if($onProgress !== null) {
$onProgress([
'$id' => $response['$id'],
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
'chunksTotal' => $response['chunksTotal'],
'chunksUploaded' => $response['chunksUploaded'],
]);
}
}
@fclose($handle);
return $response;
{% endif %}
{% endfor %}
{% endif %}
27 changes: 27 additions & 0 deletions templates/php/base/requests/params.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$params = [];
{% if method.parameters.all | length %}
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
}
{% endif %}
{% endfor %}
{% for parameter in method.parameters.query %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}

{% endfor %}
{% for parameter in method.parameters.body %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}

{% endfor %}
{% for parameter in method.parameters.formData %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% endif %}
102 changes: 3 additions & 99 deletions templates/php/src/Services/Service.php.twig
Original file line number Diff line number Diff line change
Expand Up @@ -24,110 +24,14 @@ class {{ service.name | caseUcfirst }} extends Service
*/
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 }}
{
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
}
{% endif %}
{% endfor %}
$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 }}');
$params = [];
{% for parameter in method.parameters.query %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% for parameter in method.parameters.body %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{% for parameter in method.parameters.formData %}
if (!is_null(${{ parameter.name | caseCamel | escapeKeyword }})) {
$params['{{ parameter.name }}'] = ${{ parameter.name | caseCamel | escapeKeyword }};
}
{% endfor %}
{{ include('php/base/requests/params.twig') }}
{% if 'multipart/form-data' in method.consumes %}
{% for parameter in method.parameters.all %}
{% if parameter.type == 'file' %}
$size = filesize(${{ parameter.name | caseCamel }});
$mimeType = mime_content_type(${{ parameter.name | caseCamel }});
$postedName = basename(${{ parameter.name | caseCamel }});
//send single file if size is less than or equal to 5MB
if ($size <= Client::CHUNK_SIZE) {
$params['{{ parameter.name }}'] = new \CURLFile(${{ parameter.name | caseCamel }}, $mimeType, $postedName);
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
{% for param in method.parameters.header %}
'{{ param.name }}' => ${{ param.name | caseCamel }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}' => '{{ header }}',
{% endfor %}
], $params);
}
$id = '';
$counter = 0;
{% for parameter in method.parameters.all %}
{% if parameter.isUploadID %}
if(${{ parameter.name | caseCamel | escapeKeyword }} != 'unique()') {
try {
$response = $this->client->call(Client::METHOD_GET, new URL($path . '/' . {{ parameter.name }}), headers);
$counter = $response['chunksUploaded'] ?? 0;
} catch(\Exception $e) {
}
}
{% endif %}
{% endfor %}
$headers = ['content-type' => 'multipart/form-data'];
$handle = @fopen(${{ parameter.name | caseCamel }}, "rb");
$start = $counter * Client::CHUNK_SIZE;
while ($start < $size) {
fseek($handle, $start);
$params['{{ parameter.name }}'] = new \CURLFile('data://' . $mimeType . ';base64,' . base64_encode(@fread($handle, Client::CHUNK_SIZE)), $mimeType, $postedName);
$headers['content-range'] = 'bytes ' . ($counter * Client::CHUNK_SIZE) . '-' . min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) . '/' . $size;
if(!empty($id)) {
$headers['x-{{spec.title | caseLower }}-id'] = $id;
}
$response = $this->client->call(Client::METHOD_POST, $path, $headers, $params);
$counter++;
$start += Client::CHUNK_SIZE;
if(empty($id)) {
$id = $response['$id'];
}
if($onProgress !== null) {
$onProgress([
'$id' => $response['$id'],
'progress' => min(((($counter * Client::CHUNK_SIZE) + Client::CHUNK_SIZE) - 1), $size) / $size * 100,
'sizeUploaded' => min($counter * Client::CHUNK_SIZE),
'chunksTotal' => $response['chunksTotal'],
'chunksUploaded' => $response['chunksUploaded'],
]);
}
}
@fclose($handle);
return $response;
{% endif %}
{% endfor %}
{{ include('php/base/requests/file.twig') }}
{% else %}
return $this->client->call(Client::METHOD_{{ method.method | caseUpper }}, $path, [
{% for parameter in method.parameters.header %}
'{{ parameter.name }}' => ${{ parameter.name | caseCamel | escapeKeyword }},
{% endfor %}
{% for key, header in method.headers %}
'{{ key }}' => '{{ header }}',
{% endfor %}
], $params);
{{ include('php/base/requests/api.twig') }}
{% endif %}
}
{% if not loop.last %}
Expand Down

0 comments on commit a038537

Please sign in to comment.