/
streamfactory.zep.c
212 lines (178 loc) · 6.13 KB
/
streamfactory.zep.c
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#ifdef HAVE_CONFIG_H
#include "../../../ext_config.h"
#endif
#include <php.h>
#include "../../../php_ext.h"
#include "../../../ext.h"
#include <Zend/zend_operators.h>
#include <Zend/zend_exceptions.h>
#include <Zend/zend_interfaces.h>
#include "kernel/main.h"
#include "kernel/memory.h"
#include "kernel/fcall.h"
#include "kernel/operators.h"
#include "kernel/exception.h"
#include "kernel/file.h"
#include "ext/spl/spl_exceptions.h"
#include "kernel/object.h"
/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*
* Implementation of this file has been influenced by Zend Diactoros
* @link https://github.com/zendframework/zend-diactoros
* @license https://github.com/zendframework/zend-diactoros/blob/master/LICENSE.md
*/
/**
* PSR-17 StreamFactory
*/
ZEPHIR_INIT_CLASS(Phalcon_Http_Message_StreamFactory) {
ZEPHIR_REGISTER_CLASS(Phalcon\\Http\\Message, StreamFactory, phalcon, http_message_streamfactory, phalcon_http_message_streamfactory_method_entry, ZEND_ACC_FINAL_CLASS);
zend_class_implements(phalcon_http_message_streamfactory_ce, 1, zephir_get_internal_ce(SL("psr\\http\\message\\streamfactoryinterface")));
return SUCCESS;
}
/**
* Create a new stream from a string.
*
* The stream SHOULD be created with a temporary resource.
*
* @param string $content String content with which to populate the stream.
*
* @return StreamInterface
*/
PHP_METHOD(Phalcon_Http_Message_StreamFactory, createStream) {
zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
zend_long ZEPHIR_LAST_CALL_STATUS;
zval *content_param = NULL, handle, _0, _1;
zval content;
zval *this_ptr = getThis();
ZVAL_UNDEF(&content);
ZVAL_UNDEF(&handle);
ZVAL_UNDEF(&_0);
ZVAL_UNDEF(&_1);
ZEPHIR_MM_GROW();
zephir_fetch_params(1, 0, 1, &content_param);
if (!content_param) {
ZEPHIR_INIT_VAR(&content);
ZVAL_STRING(&content, "");
} else {
if (UNEXPECTED(Z_TYPE_P(content_param) != IS_STRING && Z_TYPE_P(content_param) != IS_NULL)) {
zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'content' must be of the type string"));
RETURN_MM_NULL();
}
if (EXPECTED(Z_TYPE_P(content_param) == IS_STRING)) {
zephir_get_strval(&content, content_param);
} else {
ZEPHIR_INIT_VAR(&content);
ZVAL_EMPTY_STRING(&content);
}
}
ZEPHIR_INIT_VAR(&_0);
ZVAL_STRING(&_0, "php://temp");
ZEPHIR_INIT_VAR(&_1);
ZVAL_STRING(&_1, "r+b");
ZEPHIR_CALL_FUNCTION(&handle, "fopen", NULL, 85, &_0, &_1);
zephir_check_call_status();
if (UNEXPECTED(ZEPHIR_IS_FALSE_IDENTICAL(&handle))) {
ZEPHIR_THROW_EXCEPTION_DEBUG_STR(phalcon_http_message_exception_invalidargumentexception_ce, "Cannot write to file.", "phalcon/Http/Message/StreamFactory.zep", 41);
return;
}
zephir_fwrite(NULL, &handle, &content);
ZEPHIR_CALL_FUNCTION(NULL, "rewind", NULL, 315, &handle);
zephir_check_call_status();
ZEPHIR_RETURN_CALL_METHOD(this_ptr, "createstreamfromresource", NULL, 316, &handle);
zephir_check_call_status();
RETURN_MM();
}
/**
* Create a stream from an existing file.
*
* The file MUST be opened using the given mode, which may be any mode
* supported by the `fopen` function.
*
* The `$filename` MAY be any string supported by `fopen()`.
*
* @param string $filename The filename or stream URI to use as basis of
* stream.
* @param string $mode The mode with which to open the underlying
* filename/stream.
*
* @return StreamInterface
*/
PHP_METHOD(Phalcon_Http_Message_StreamFactory, createStreamFromFile) {
zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
zend_long ZEPHIR_LAST_CALL_STATUS;
zval *filename_param = NULL, *mode_param = NULL;
zval filename, mode;
zval *this_ptr = getThis();
ZVAL_UNDEF(&filename);
ZVAL_UNDEF(&mode);
ZEPHIR_MM_GROW();
zephir_fetch_params(1, 1, 1, &filename_param, &mode_param);
if (UNEXPECTED(Z_TYPE_P(filename_param) != IS_STRING && Z_TYPE_P(filename_param) != IS_NULL)) {
zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'filename' must be of the type string"));
RETURN_MM_NULL();
}
if (EXPECTED(Z_TYPE_P(filename_param) == IS_STRING)) {
zephir_get_strval(&filename, filename_param);
} else {
ZEPHIR_INIT_VAR(&filename);
ZVAL_EMPTY_STRING(&filename);
}
if (!mode_param) {
ZEPHIR_INIT_VAR(&mode);
ZVAL_STRING(&mode, "r+b");
} else {
if (UNEXPECTED(Z_TYPE_P(mode_param) != IS_STRING && Z_TYPE_P(mode_param) != IS_NULL)) {
zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'mode' must be of the type string"));
RETURN_MM_NULL();
}
if (EXPECTED(Z_TYPE_P(mode_param) == IS_STRING)) {
zephir_get_strval(&mode, mode_param);
} else {
ZEPHIR_INIT_VAR(&mode);
ZVAL_EMPTY_STRING(&mode);
}
}
object_init_ex(return_value, phalcon_http_message_stream_ce);
ZEPHIR_CALL_METHOD(NULL, return_value, "__construct", NULL, 42, &filename, &mode);
zephir_check_call_status();
RETURN_MM();
}
/**
* Create a new stream from an existing resource.
*
* The stream MUST be readable and may be writable.
*/
PHP_METHOD(Phalcon_Http_Message_StreamFactory, createStreamFromResource) {
zend_bool _0;
zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
zend_long ZEPHIR_LAST_CALL_STATUS;
zval *phpResource, phpResource_sub, _1, _2;
zval *this_ptr = getThis();
ZVAL_UNDEF(&phpResource_sub);
ZVAL_UNDEF(&_1);
ZVAL_UNDEF(&_2);
ZEPHIR_MM_GROW();
zephir_fetch_params(1, 1, 0, &phpResource);
_0 = Z_TYPE_P(phpResource) != IS_RESOURCE;
if (!(_0)) {
ZEPHIR_CALL_FUNCTION(&_1, "get_resource_type", NULL, 87, phpResource);
zephir_check_call_status();
ZEPHIR_INIT_VAR(&_2);
ZVAL_STRING(&_2, "stream");
_0 = !ZEPHIR_IS_IDENTICAL(&_2, &_1);
}
if (UNEXPECTED(_0)) {
ZEPHIR_THROW_EXCEPTION_DEBUG_STR(phalcon_http_message_exception_invalidargumentexception_ce, "Invalid stream provided", "phalcon/Http/Message/StreamFactory.zep", 79);
return;
}
object_init_ex(return_value, phalcon_http_message_stream_ce);
ZEPHIR_CALL_METHOD(NULL, return_value, "__construct", NULL, 42, phpResource);
zephir_check_call_status();
RETURN_MM();
}