1
2 #ifdef HAVE_CONFIG_H
3 #include "../../../ext_config.h"
4 #endif
5
6 #include <php.h>
7 #include "../../../php_ext.h"
8 #include "../../../ext.h"
9
10 #include <Zend/zend_operators.h>
11 #include <Zend/zend_exceptions.h>
12 #include <Zend/zend_interfaces.h>
13
14 #include "kernel/main.h"
15 #include "kernel/fcall.h"
16 #include "kernel/memory.h"
17 #include "kernel/operators.h"
18 #include "kernel/exception.h"
19 #include "kernel/object.h"
20 #include "ext/spl/spl_exceptions.h"
21 #include "kernel/file.h"
22 #include "kernel/concat.h"
23 #include "kernel/array.h"
24 #include "kernel/time.h"
25 #include "kernel/string.h"
26 #include "ext/spl/spl_iterators.h"
27 #include "ext/spl/spl_directory.h"
28
29
30 /**
31 * This file is part of the Phalcon Framework.
32 *
33 * (c) Phalcon Team <team@phalcon.io>
34 *
35 * For the full copyright and license information, please view the LICENSE.txt
36 * file that was distributed with this source code.
37 */
38 /**
39 * Stream adapter
40 */
ZEPHIR_INIT_CLASS(Phalcon_Storage_Adapter_Stream)41 ZEPHIR_INIT_CLASS(Phalcon_Storage_Adapter_Stream) {
42
43 ZEPHIR_REGISTER_CLASS_EX(Phalcon\\Storage\\Adapter, Stream, phalcon, storage_adapter_stream, phalcon_storage_adapter_abstractadapter_ce, phalcon_storage_adapter_stream_method_entry, 0);
44
45 /**
46 * @var string
47 */
48 zend_declare_property_string(phalcon_storage_adapter_stream_ce, SL("storageDir"), "", ZEND_ACC_PROTECTED);
49
50 /**
51 * @var array
52 */
53 zend_declare_property_null(phalcon_storage_adapter_stream_ce, SL("options"), ZEND_ACC_PROTECTED);
54
55 phalcon_storage_adapter_stream_ce->create_object = zephir_init_properties_Phalcon_Storage_Adapter_Stream;
56 return SUCCESS;
57
58 }
59
60 /**
61 * Stream constructor.
62 *
63 * @param array options = [
64 * 'storageDir' => '',
65 * 'defaultSerializer' => 'Php',
66 * 'lifetime' => 3600,
67 * 'serializer' => null,
68 * 'prefix' => ''
69 * ]
70 *
71 * @throws Exception
72 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,__construct)73 PHP_METHOD(Phalcon_Storage_Adapter_Stream, __construct) {
74
75 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
76 zend_long ZEPHIR_LAST_CALL_STATUS;
77 zephir_fcall_cache_entry *_0 = NULL, *_4 = NULL, *_5 = NULL;
78 zval options;
79 zval *factory, factory_sub, *options_param = NULL, storageDir, _1, _2, _3;
80 zval *this_ptr = getThis();
81
82 ZVAL_UNDEF(&factory_sub);
83 ZVAL_UNDEF(&storageDir);
84 ZVAL_UNDEF(&_1);
85 ZVAL_UNDEF(&_2);
86 ZVAL_UNDEF(&_3);
87 ZVAL_UNDEF(&options);
88
89 ZEPHIR_MM_GROW();
90 zephir_fetch_params(1, 1, 1, &factory, &options_param);
91
92 if (!options_param) {
93 ZEPHIR_INIT_VAR(&options);
94 array_init(&options);
95 } else {
96 ZEPHIR_OBS_COPY_OR_DUP(&options, options_param);
97 }
98
99
100 ZEPHIR_INIT_VAR(&_1);
101 ZVAL_STRING(&_1, "storageDir");
102 ZEPHIR_INIT_VAR(&_2);
103 ZVAL_STRING(&_2, "");
104 ZEPHIR_CALL_CE_STATIC(&storageDir, phalcon_helper_arr_ce, "get", &_0, 16, &options, &_1, &_2);
105 zephir_check_call_status();
106 if (ZEPHIR_IS_EMPTY(&storageDir)) {
107 ZEPHIR_THROW_EXCEPTION_DEBUG_STR(phalcon_storage_exception_ce, "The 'storageDir' must be specified in the options", "phalcon/Storage/Adapter/Stream.zep", 57);
108 return;
109 }
110 ZEPHIR_CALL_CE_STATIC(&_3, phalcon_helper_str_ce, "dirseparator", &_4, 128, &storageDir);
111 zephir_check_call_status();
112 zephir_update_property_zval(this_ptr, ZEND_STRL("storageDir"), &_3);
113 ZEPHIR_INIT_NVAR(&_1);
114 ZVAL_STRING(&_1, "ph-strm");
115 zephir_update_property_zval(this_ptr, ZEND_STRL("prefix"), &_1);
116 zephir_update_property_zval(this_ptr, ZEND_STRL("options"), &options);
117 ZEPHIR_CALL_PARENT(NULL, phalcon_storage_adapter_stream_ce, getThis(), "__construct", &_5, 0, factory, &options);
118 zephir_check_call_status();
119 ZEPHIR_CALL_METHOD(NULL, this_ptr, "initserializer", NULL, 0);
120 zephir_check_call_status();
121 ZEPHIR_MM_RESTORE();
122
123 }
124
125 /**
126 * Flushes/clears the cache
127 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,clear)128 PHP_METHOD(Phalcon_Storage_Adapter_Stream, clear) {
129
130 zend_bool result = 0, _5$$3, _10$$5;
131 zval directory, iterator, file, _1, *_2, _3, _4$$3, _6$$3, _7$$3, _9$$5, _11$$5, _12$$5;
132 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
133 zend_long ZEPHIR_LAST_CALL_STATUS;
134 zephir_fcall_cache_entry *_0 = NULL, *_8 = NULL;
135 zval *this_ptr = getThis();
136
137 ZVAL_UNDEF(&directory);
138 ZVAL_UNDEF(&iterator);
139 ZVAL_UNDEF(&file);
140 ZVAL_UNDEF(&_1);
141 ZVAL_UNDEF(&_3);
142 ZVAL_UNDEF(&_4$$3);
143 ZVAL_UNDEF(&_6$$3);
144 ZVAL_UNDEF(&_7$$3);
145 ZVAL_UNDEF(&_9$$5);
146 ZVAL_UNDEF(&_11$$5);
147 ZVAL_UNDEF(&_12$$5);
148
149 ZEPHIR_MM_GROW();
150
151 result = 1;
152 zephir_read_property(&_1, this_ptr, ZEND_STRL("storageDir"), PH_NOISY_CC | PH_READONLY);
153 ZEPHIR_CALL_CE_STATIC(&directory, phalcon_helper_str_ce, "dirseparator", &_0, 128, &_1);
154 zephir_check_call_status();
155 ZEPHIR_CALL_METHOD(&iterator, this_ptr, "getiterator", NULL, 129, &directory);
156 zephir_check_call_status();
157 zephir_is_iterable(&iterator, 0, "phalcon/Storage/Adapter/Stream.zep", 90);
158 if (Z_TYPE_P(&iterator) == IS_ARRAY) {
159 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&iterator), _2)
160 {
161 ZEPHIR_INIT_NVAR(&file);
162 ZVAL_COPY(&file, _2);
163 ZEPHIR_CALL_METHOD(&_4$$3, &file, "isfile", NULL, 0);
164 zephir_check_call_status();
165 _5$$3 = zephir_is_true(&_4$$3);
166 if (_5$$3) {
167 ZEPHIR_CALL_METHOD(&_6$$3, &file, "getpathname", NULL, 0);
168 zephir_check_call_status();
169 ZEPHIR_CALL_FUNCTION(&_7$$3, "unlink", &_8, 130, &_6$$3);
170 zephir_check_call_status();
171 _5$$3 = !zephir_is_true(&_7$$3);
172 }
173 if (_5$$3) {
174 result = 0;
175 }
176 } ZEND_HASH_FOREACH_END();
177 } else {
178 ZEPHIR_CALL_METHOD(NULL, &iterator, "rewind", NULL, 0);
179 zephir_check_call_status();
180 while (1) {
181 ZEPHIR_CALL_METHOD(&_3, &iterator, "valid", NULL, 0);
182 zephir_check_call_status();
183 if (!zend_is_true(&_3)) {
184 break;
185 }
186 ZEPHIR_CALL_METHOD(&file, &iterator, "current", NULL, 0);
187 zephir_check_call_status();
188 ZEPHIR_CALL_METHOD(&_9$$5, &file, "isfile", NULL, 0);
189 zephir_check_call_status();
190 _10$$5 = zephir_is_true(&_9$$5);
191 if (_10$$5) {
192 ZEPHIR_CALL_METHOD(&_11$$5, &file, "getpathname", NULL, 0);
193 zephir_check_call_status();
194 ZEPHIR_CALL_FUNCTION(&_12$$5, "unlink", &_8, 130, &_11$$5);
195 zephir_check_call_status();
196 _10$$5 = !zephir_is_true(&_12$$5);
197 }
198 if (_10$$5) {
199 result = 0;
200 }
201 ZEPHIR_CALL_METHOD(NULL, &iterator, "next", NULL, 0);
202 zephir_check_call_status();
203 }
204 }
205 ZEPHIR_INIT_NVAR(&file);
206 RETURN_MM_BOOL(result);
207
208 }
209
210 /**
211 * Decrements a stored number
212 *
213 * @param string $key
214 * @param int $value
215 *
216 * @return bool|int
217 * @throws \Exception
218 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,decrement)219 PHP_METHOD(Phalcon_Storage_Adapter_Stream, decrement) {
220
221 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
222 zend_long value, ZEPHIR_LAST_CALL_STATUS, _1;
223 zval *key_param = NULL, *value_param = NULL, data, _0;
224 zval key;
225 zval *this_ptr = getThis();
226
227 ZVAL_UNDEF(&key);
228 ZVAL_UNDEF(&data);
229 ZVAL_UNDEF(&_0);
230
231 ZEPHIR_MM_GROW();
232 zephir_fetch_params(1, 1, 1, &key_param, &value_param);
233
234 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
235 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
236 RETURN_MM_NULL();
237 }
238 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
239 zephir_get_strval(&key, key_param);
240 } else {
241 ZEPHIR_INIT_VAR(&key);
242 ZVAL_EMPTY_STRING(&key);
243 }
244 if (!value_param) {
245 value = 1;
246 } else {
247 value = zephir_get_intval(value_param);
248 }
249
250
251 ZEPHIR_CALL_METHOD(&_0, this_ptr, "has", NULL, 0, &key);
252 zephir_check_call_status();
253 if (!(zephir_is_true(&_0))) {
254 RETURN_MM_BOOL(0);
255 }
256 ZEPHIR_CALL_METHOD(&data, this_ptr, "get", NULL, 0, &key);
257 zephir_check_call_status();
258 _1 = (zephir_get_intval(&data) - value);
259 ZEPHIR_INIT_NVAR(&data);
260 ZVAL_LONG(&data, _1);
261 ZEPHIR_RETURN_CALL_METHOD(this_ptr, "set", NULL, 0, &key, &data);
262 zephir_check_call_status();
263 RETURN_MM();
264
265 }
266
267 /**
268 * Reads data from the adapter
269 *
270 * @param string $key
271 *
272 * @return bool
273 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,delete)274 PHP_METHOD(Phalcon_Storage_Adapter_Stream, delete) {
275
276 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
277 zend_long ZEPHIR_LAST_CALL_STATUS;
278 zval *key_param = NULL, filepath, _0;
279 zval key;
280 zval *this_ptr = getThis();
281
282 ZVAL_UNDEF(&key);
283 ZVAL_UNDEF(&filepath);
284 ZVAL_UNDEF(&_0);
285
286 ZEPHIR_MM_GROW();
287 zephir_fetch_params(1, 1, 0, &key_param);
288
289 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
290 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
291 RETURN_MM_NULL();
292 }
293 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
294 zephir_get_strval(&key, key_param);
295 } else {
296 ZEPHIR_INIT_VAR(&key);
297 ZVAL_EMPTY_STRING(&key);
298 }
299
300
301 ZEPHIR_CALL_METHOD(&_0, this_ptr, "has", NULL, 0, &key);
302 zephir_check_call_status();
303 if (!(zephir_is_true(&_0))) {
304 RETURN_MM_BOOL(0);
305 }
306 ZEPHIR_CALL_METHOD(&filepath, this_ptr, "getfilepath", NULL, 131, &key);
307 zephir_check_call_status();
308 ZEPHIR_RETURN_CALL_FUNCTION("unlink", NULL, 130, &filepath);
309 zephir_check_call_status();
310 RETURN_MM();
311
312 }
313
314 /**
315 * Reads data from the adapter
316 *
317 * @param string $key
318 * @param null $defaultValue
319 *
320 * @return mixed|null
321 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,get)322 PHP_METHOD(Phalcon_Storage_Adapter_Stream, get) {
323
324 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
325 zephir_fcall_cache_entry *_1 = NULL;
326 zend_long ZEPHIR_LAST_CALL_STATUS;
327 zval *key_param = NULL, *defaultValue = NULL, defaultValue_sub, __$null, content, filepath, payload, _0, _2, _3;
328 zval key;
329 zval *this_ptr = getThis();
330
331 ZVAL_UNDEF(&key);
332 ZVAL_UNDEF(&defaultValue_sub);
333 ZVAL_NULL(&__$null);
334 ZVAL_UNDEF(&content);
335 ZVAL_UNDEF(&filepath);
336 ZVAL_UNDEF(&payload);
337 ZVAL_UNDEF(&_0);
338 ZVAL_UNDEF(&_2);
339 ZVAL_UNDEF(&_3);
340
341 ZEPHIR_MM_GROW();
342 zephir_fetch_params(1, 1, 1, &key_param, &defaultValue);
343
344 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
345 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
346 RETURN_MM_NULL();
347 }
348 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
349 zephir_get_strval(&key, key_param);
350 } else {
351 ZEPHIR_INIT_VAR(&key);
352 ZVAL_EMPTY_STRING(&key);
353 }
354 if (!defaultValue) {
355 defaultValue = &defaultValue_sub;
356 defaultValue = &__$null;
357 }
358
359
360 ZEPHIR_CALL_METHOD(&filepath, this_ptr, "getfilepath", NULL, 131, &key);
361 zephir_check_call_status();
362 if (!((zephir_file_exists(&filepath) == SUCCESS))) {
363 RETVAL_ZVAL(defaultValue, 1, 0);
364 RETURN_MM();
365 }
366 ZEPHIR_CALL_METHOD(&payload, this_ptr, "getpayload", NULL, 132, &filepath);
367 zephir_check_call_status();
368 if (UNEXPECTED(ZEPHIR_IS_EMPTY(&payload))) {
369 RETVAL_ZVAL(defaultValue, 1, 0);
370 RETURN_MM();
371 }
372 ZEPHIR_CALL_METHOD(&_0, this_ptr, "isexpired", NULL, 133, &payload);
373 zephir_check_call_status();
374 if (zephir_is_true(&_0)) {
375 RETVAL_ZVAL(defaultValue, 1, 0);
376 RETURN_MM();
377 }
378 ZEPHIR_INIT_VAR(&_2);
379 ZVAL_STRING(&_2, "content");
380 ZVAL_NULL(&_3);
381 ZEPHIR_CALL_CE_STATIC(&content, phalcon_helper_arr_ce, "get", &_1, 16, &payload, &_2, &_3);
382 zephir_check_call_status();
383 ZEPHIR_RETURN_CALL_METHOD(this_ptr, "getunserializeddata", NULL, 0, &content, defaultValue);
384 zephir_check_call_status();
385 RETURN_MM();
386
387 }
388
389 /**
390 * Always returns null
391 *
392 * @return null
393 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getAdapter)394 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getAdapter) {
395
396 zval *this_ptr = getThis();
397
398
399 RETURN_MEMBER(getThis(), "adapter");
400
401 }
402
403 /**
404 * Stores data in the adapter
405 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getKeys)406 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getKeys) {
407
408 zval files;
409 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
410 zend_long ZEPHIR_LAST_CALL_STATUS;
411 zval *prefix_param = NULL, directory, file, iterator, *_0, _1, _2$$4, _3$$5, _4$$5, _5$$5, _6$$6, _7$$7, _8$$7, _9$$7;
412 zval prefix;
413 zval *this_ptr = getThis();
414
415 ZVAL_UNDEF(&prefix);
416 ZVAL_UNDEF(&directory);
417 ZVAL_UNDEF(&file);
418 ZVAL_UNDEF(&iterator);
419 ZVAL_UNDEF(&_1);
420 ZVAL_UNDEF(&_2$$4);
421 ZVAL_UNDEF(&_3$$5);
422 ZVAL_UNDEF(&_4$$5);
423 ZVAL_UNDEF(&_5$$5);
424 ZVAL_UNDEF(&_6$$6);
425 ZVAL_UNDEF(&_7$$7);
426 ZVAL_UNDEF(&_8$$7);
427 ZVAL_UNDEF(&_9$$7);
428 ZVAL_UNDEF(&files);
429
430 ZEPHIR_MM_GROW();
431 zephir_fetch_params(1, 0, 1, &prefix_param);
432
433 if (!prefix_param) {
434 ZEPHIR_INIT_VAR(&prefix);
435 ZVAL_STRING(&prefix, "");
436 } else {
437 if (UNEXPECTED(Z_TYPE_P(prefix_param) != IS_STRING && Z_TYPE_P(prefix_param) != IS_NULL)) {
438 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'prefix' must be of the type string"));
439 RETURN_MM_NULL();
440 }
441 if (EXPECTED(Z_TYPE_P(prefix_param) == IS_STRING)) {
442 zephir_get_strval(&prefix, prefix_param);
443 } else {
444 ZEPHIR_INIT_VAR(&prefix);
445 ZVAL_EMPTY_STRING(&prefix);
446 }
447 }
448
449
450 ZEPHIR_INIT_VAR(&files);
451 array_init(&files);
452 ZEPHIR_CALL_METHOD(&directory, this_ptr, "getdir", NULL, 134);
453 zephir_check_call_status();
454 if (!((zephir_file_exists(&directory) == SUCCESS))) {
455 array_init(return_value);
456 RETURN_MM();
457 }
458 ZEPHIR_CALL_METHOD(&iterator, this_ptr, "getiterator", NULL, 129, &directory);
459 zephir_check_call_status();
460 zephir_is_iterable(&iterator, 0, "phalcon/Storage/Adapter/Stream.zep", 202);
461 if (Z_TYPE_P(&iterator) == IS_ARRAY) {
462 ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&iterator), _0)
463 {
464 ZEPHIR_INIT_NVAR(&file);
465 ZVAL_COPY(&file, _0);
466 ZEPHIR_CALL_METHOD(&_2$$4, &file, "isfile", NULL, 0);
467 zephir_check_call_status();
468 if (zephir_is_true(&_2$$4)) {
469 zephir_read_property(&_3$$5, this_ptr, ZEND_STRL("prefix"), PH_NOISY_CC | PH_READONLY);
470 ZEPHIR_CALL_METHOD(&_4$$5, &file, "getfilename", NULL, 0);
471 zephir_check_call_status();
472 ZEPHIR_INIT_NVAR(&_5$$5);
473 ZEPHIR_CONCAT_VV(&_5$$5, &_3$$5, &_4$$5);
474 zephir_array_append(&files, &_5$$5, PH_SEPARATE, "phalcon/Storage/Adapter/Stream.zep", 198);
475 }
476 } ZEND_HASH_FOREACH_END();
477 } else {
478 ZEPHIR_CALL_METHOD(NULL, &iterator, "rewind", NULL, 0);
479 zephir_check_call_status();
480 while (1) {
481 ZEPHIR_CALL_METHOD(&_1, &iterator, "valid", NULL, 0);
482 zephir_check_call_status();
483 if (!zend_is_true(&_1)) {
484 break;
485 }
486 ZEPHIR_CALL_METHOD(&file, &iterator, "current", NULL, 0);
487 zephir_check_call_status();
488 ZEPHIR_CALL_METHOD(&_6$$6, &file, "isfile", NULL, 0);
489 zephir_check_call_status();
490 if (zephir_is_true(&_6$$6)) {
491 zephir_read_property(&_7$$7, this_ptr, ZEND_STRL("prefix"), PH_NOISY_CC | PH_READONLY);
492 ZEPHIR_CALL_METHOD(&_8$$7, &file, "getfilename", NULL, 0);
493 zephir_check_call_status();
494 ZEPHIR_INIT_NVAR(&_9$$7);
495 ZEPHIR_CONCAT_VV(&_9$$7, &_7$$7, &_8$$7);
496 zephir_array_append(&files, &_9$$7, PH_SEPARATE, "phalcon/Storage/Adapter/Stream.zep", 198);
497 }
498 ZEPHIR_CALL_METHOD(NULL, &iterator, "next", NULL, 0);
499 zephir_check_call_status();
500 }
501 }
502 ZEPHIR_INIT_NVAR(&file);
503 ZEPHIR_RETURN_CALL_METHOD(this_ptr, "getfilteredkeys", NULL, 0, &files, &prefix);
504 zephir_check_call_status();
505 RETURN_MM();
506
507 }
508
509 /**
510 * Checks if an element exists in the cache and is not expired
511 *
512 * @param string $key
513 *
514 * @return bool
515 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,has)516 PHP_METHOD(Phalcon_Storage_Adapter_Stream, has) {
517
518 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
519 zend_long ZEPHIR_LAST_CALL_STATUS;
520 zval *key_param = NULL, payload, filepath, _0;
521 zval key;
522 zval *this_ptr = getThis();
523
524 ZVAL_UNDEF(&key);
525 ZVAL_UNDEF(&payload);
526 ZVAL_UNDEF(&filepath);
527 ZVAL_UNDEF(&_0);
528
529 ZEPHIR_MM_GROW();
530 zephir_fetch_params(1, 1, 0, &key_param);
531
532 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
533 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
534 RETURN_MM_NULL();
535 }
536 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
537 zephir_get_strval(&key, key_param);
538 } else {
539 ZEPHIR_INIT_VAR(&key);
540 ZVAL_EMPTY_STRING(&key);
541 }
542
543
544 ZEPHIR_CALL_METHOD(&filepath, this_ptr, "getfilepath", NULL, 131, &key);
545 zephir_check_call_status();
546 if (!((zephir_file_exists(&filepath) == SUCCESS))) {
547 RETURN_MM_BOOL(0);
548 }
549 ZEPHIR_CALL_METHOD(&payload, this_ptr, "getpayload", NULL, 132, &filepath);
550 zephir_check_call_status();
551 if (UNEXPECTED(ZEPHIR_IS_EMPTY(&payload))) {
552 RETURN_MM_BOOL(0);
553 }
554 ZEPHIR_CALL_METHOD(&_0, this_ptr, "isexpired", NULL, 133, &payload);
555 zephir_check_call_status();
556 RETURN_MM_BOOL(!zephir_is_true(&_0));
557
558 }
559
560 /**
561 * Increments a stored number
562 *
563 * @param string $key
564 * @param int $value
565 *
566 * @return bool|int
567 * @throws \Exception
568 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,increment)569 PHP_METHOD(Phalcon_Storage_Adapter_Stream, increment) {
570
571 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
572 zend_long value, ZEPHIR_LAST_CALL_STATUS, _1;
573 zval *key_param = NULL, *value_param = NULL, data, _0;
574 zval key;
575 zval *this_ptr = getThis();
576
577 ZVAL_UNDEF(&key);
578 ZVAL_UNDEF(&data);
579 ZVAL_UNDEF(&_0);
580
581 ZEPHIR_MM_GROW();
582 zephir_fetch_params(1, 1, 1, &key_param, &value_param);
583
584 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
585 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
586 RETURN_MM_NULL();
587 }
588 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
589 zephir_get_strval(&key, key_param);
590 } else {
591 ZEPHIR_INIT_VAR(&key);
592 ZVAL_EMPTY_STRING(&key);
593 }
594 if (!value_param) {
595 value = 1;
596 } else {
597 value = zephir_get_intval(value_param);
598 }
599
600
601 ZEPHIR_CALL_METHOD(&_0, this_ptr, "has", NULL, 0, &key);
602 zephir_check_call_status();
603 if (!(zephir_is_true(&_0))) {
604 RETURN_MM_BOOL(0);
605 }
606 ZEPHIR_CALL_METHOD(&data, this_ptr, "get", NULL, 0, &key);
607 zephir_check_call_status();
608 _1 = (zephir_get_intval(&data) + value);
609 ZEPHIR_INIT_NVAR(&data);
610 ZVAL_LONG(&data, _1);
611 ZEPHIR_RETURN_CALL_METHOD(this_ptr, "set", NULL, 0, &key, &data);
612 zephir_check_call_status();
613 RETURN_MM();
614
615 }
616
617 /**
618 * Stores data in the adapter
619 *
620 * @param string $key
621 * @param mixed $value
622 * @param DateInterval|int|null $ttl
623 *
624 * @return bool
625 * @throws \Exception
626 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,set)627 PHP_METHOD(Phalcon_Storage_Adapter_Stream, set) {
628
629 zval payload;
630 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
631 zend_long ZEPHIR_LAST_CALL_STATUS;
632 zval *key_param = NULL, *value, value_sub, *ttl = NULL, ttl_sub, __$true, __$null, directory, _0, _1, _2, _4, _5, _6, _3$$3;
633 zval key;
634 zval *this_ptr = getThis();
635
636 ZVAL_UNDEF(&key);
637 ZVAL_UNDEF(&value_sub);
638 ZVAL_UNDEF(&ttl_sub);
639 ZVAL_BOOL(&__$true, 1);
640 ZVAL_NULL(&__$null);
641 ZVAL_UNDEF(&directory);
642 ZVAL_UNDEF(&_0);
643 ZVAL_UNDEF(&_1);
644 ZVAL_UNDEF(&_2);
645 ZVAL_UNDEF(&_4);
646 ZVAL_UNDEF(&_5);
647 ZVAL_UNDEF(&_6);
648 ZVAL_UNDEF(&_3$$3);
649 ZVAL_UNDEF(&payload);
650
651 ZEPHIR_MM_GROW();
652 zephir_fetch_params(1, 2, 1, &key_param, &value, &ttl);
653
654 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
655 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
656 RETURN_MM_NULL();
657 }
658 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
659 zephir_get_strval(&key, key_param);
660 } else {
661 ZEPHIR_INIT_VAR(&key);
662 ZVAL_EMPTY_STRING(&key);
663 }
664 if (!ttl) {
665 ttl = &ttl_sub;
666 ttl = &__$null;
667 }
668
669
670 ZEPHIR_INIT_VAR(&payload);
671 zephir_create_array(&payload, 3, 0);
672 ZEPHIR_INIT_VAR(&_0);
673 zephir_time(&_0);
674 zephir_array_update_string(&payload, SL("created"), &_0, PH_COPY | PH_SEPARATE);
675 ZEPHIR_CALL_METHOD(&_1, this_ptr, "getttl", NULL, 0, ttl);
676 zephir_check_call_status();
677 zephir_array_update_string(&payload, SL("ttl"), &_1, PH_COPY | PH_SEPARATE);
678 ZEPHIR_CALL_METHOD(&_1, this_ptr, "getserializeddata", NULL, 0, value);
679 zephir_check_call_status();
680 zephir_array_update_string(&payload, SL("content"), &_1, PH_COPY | PH_SEPARATE);
681 ZEPHIR_CALL_FUNCTION(&_1, "serialize", NULL, 13, &payload);
682 zephir_check_call_status();
683 ZEPHIR_CPY_WRT(&payload, &_1);
684 ZEPHIR_CALL_METHOD(&directory, this_ptr, "getdir", NULL, 134, &key);
685 zephir_check_call_status();
686 ZEPHIR_CALL_FUNCTION(&_2, "is_dir", NULL, 135, &directory);
687 zephir_check_call_status();
688 if (!(zephir_is_true(&_2))) {
689 ZVAL_LONG(&_3$$3, 0777);
690 ZEPHIR_CALL_FUNCTION(NULL, "mkdir", NULL, 136, &directory, &_3$$3, &__$true);
691 zephir_check_call_status();
692 }
693 ZEPHIR_INIT_VAR(&_4);
694 ZEPHIR_CONCAT_VV(&_4, &directory, &key);
695 ZVAL_LONG(&_5, 2);
696 ZEPHIR_CALL_FUNCTION(&_6, "file_put_contents", NULL, 137, &_4, &payload, &_5);
697 zephir_check_call_status();
698 RETURN_MM_BOOL(!ZEPHIR_IS_FALSE_IDENTICAL(&_6));
699
700 }
701
702 /**
703 * Returns the folder based on the storageDir and the prefix
704 *
705 * @param string $key
706 *
707 * @return string
708 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getDir)709 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getDir) {
710
711 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
712 zend_long ZEPHIR_LAST_CALL_STATUS;
713 zephir_fcall_cache_entry *_0 = NULL, *_4 = NULL;
714 zval *key_param = NULL, dirFromFile, dirPrefix, _1, _2, _3, _5, _6, _7, _8;
715 zval key;
716 zval *this_ptr = getThis();
717
718 ZVAL_UNDEF(&key);
719 ZVAL_UNDEF(&dirFromFile);
720 ZVAL_UNDEF(&dirPrefix);
721 ZVAL_UNDEF(&_1);
722 ZVAL_UNDEF(&_2);
723 ZVAL_UNDEF(&_3);
724 ZVAL_UNDEF(&_5);
725 ZVAL_UNDEF(&_6);
726 ZVAL_UNDEF(&_7);
727 ZVAL_UNDEF(&_8);
728
729 ZEPHIR_MM_GROW();
730 zephir_fetch_params(1, 0, 1, &key_param);
731
732 if (!key_param) {
733 ZEPHIR_INIT_VAR(&key);
734 ZVAL_STRING(&key, "");
735 } else {
736 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
737 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
738 RETURN_MM_NULL();
739 }
740 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
741 zephir_get_strval(&key, key_param);
742 } else {
743 ZEPHIR_INIT_VAR(&key);
744 ZVAL_EMPTY_STRING(&key);
745 }
746 }
747
748
749 zephir_read_property(&_1, this_ptr, ZEND_STRL("storageDir"), PH_NOISY_CC | PH_READONLY);
750 zephir_read_property(&_2, this_ptr, ZEND_STRL("prefix"), PH_NOISY_CC | PH_READONLY);
751 ZEPHIR_INIT_VAR(&_3);
752 ZEPHIR_CONCAT_VV(&_3, &_1, &_2);
753 ZEPHIR_CALL_CE_STATIC(&dirPrefix, phalcon_helper_str_ce, "dirseparator", &_0, 128, &_3);
754 zephir_check_call_status();
755 ZEPHIR_INIT_VAR(&_5);
756 zephir_read_property(&_6, this_ptr, ZEND_STRL("prefix"), PH_NOISY_CC | PH_READONLY);
757 ZEPHIR_INIT_VAR(&_7);
758 ZVAL_STRING(&_7, "");
759 zephir_fast_str_replace(&_5, &_6, &_7, &key);
760 ZEPHIR_CALL_CE_STATIC(&dirFromFile, phalcon_helper_str_ce, "dirfromfile", &_4, 138, &_5);
761 zephir_check_call_status();
762 ZEPHIR_INIT_VAR(&_8);
763 ZEPHIR_CONCAT_VV(&_8, &dirPrefix, &dirFromFile);
764 ZEPHIR_RETURN_CALL_CE_STATIC(phalcon_helper_str_ce, "dirseparator", &_0, 128, &_8);
765 zephir_check_call_status();
766 RETURN_MM();
767
768 }
769
770 /**
771 * Returns the full path to the file
772 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getFilepath)773 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getFilepath) {
774
775 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
776 zend_long ZEPHIR_LAST_CALL_STATUS;
777 zval *key_param = NULL, _0, _1, _2, _3, _4;
778 zval key;
779 zval *this_ptr = getThis();
780
781 ZVAL_UNDEF(&key);
782 ZVAL_UNDEF(&_0);
783 ZVAL_UNDEF(&_1);
784 ZVAL_UNDEF(&_2);
785 ZVAL_UNDEF(&_3);
786 ZVAL_UNDEF(&_4);
787
788 ZEPHIR_MM_GROW();
789 zephir_fetch_params(1, 1, 0, &key_param);
790
791 if (UNEXPECTED(Z_TYPE_P(key_param) != IS_STRING && Z_TYPE_P(key_param) != IS_NULL)) {
792 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'key' must be of the type string"));
793 RETURN_MM_NULL();
794 }
795 if (EXPECTED(Z_TYPE_P(key_param) == IS_STRING)) {
796 zephir_get_strval(&key, key_param);
797 } else {
798 ZEPHIR_INIT_VAR(&key);
799 ZVAL_EMPTY_STRING(&key);
800 }
801
802
803 ZEPHIR_CALL_METHOD(&_0, this_ptr, "getdir", NULL, 134, &key);
804 zephir_check_call_status();
805 zephir_read_property(&_1, this_ptr, ZEND_STRL("prefix"), PH_NOISY_CC | PH_READONLY);
806 ZEPHIR_INIT_VAR(&_2);
807 ZVAL_STRING(&_2, "");
808 ZVAL_LONG(&_3, 1);
809 ZEPHIR_MAKE_REF(&_3);
810 ZEPHIR_CALL_FUNCTION(&_4, "str_replace", NULL, 139, &_1, &_2, &key, &_3);
811 ZEPHIR_UNREF(&_3);
812 zephir_check_call_status();
813 ZEPHIR_CONCAT_VV(return_value, &_0, &_4);
814 RETURN_MM();
815
816 }
817
818 /**
819 * Returns an iterator for the directory contents
820 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getIterator)821 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getIterator) {
822
823 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
824 zend_long ZEPHIR_LAST_CALL_STATUS;
825 zval *dir_param = NULL, _0, _1;
826 zval dir;
827 zval *this_ptr = getThis();
828
829 ZVAL_UNDEF(&dir);
830 ZVAL_UNDEF(&_0);
831 ZVAL_UNDEF(&_1);
832
833 ZEPHIR_MM_GROW();
834 zephir_fetch_params(1, 1, 0, &dir_param);
835
836 if (UNEXPECTED(Z_TYPE_P(dir_param) != IS_STRING && Z_TYPE_P(dir_param) != IS_NULL)) {
837 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'dir' must be of the type string"));
838 RETURN_MM_NULL();
839 }
840 if (EXPECTED(Z_TYPE_P(dir_param) == IS_STRING)) {
841 zephir_get_strval(&dir, dir_param);
842 } else {
843 ZEPHIR_INIT_VAR(&dir);
844 ZVAL_EMPTY_STRING(&dir);
845 }
846
847
848 object_init_ex(return_value, spl_ce_RecursiveIteratorIterator);
849 ZEPHIR_INIT_VAR(&_0);
850 object_init_ex(&_0, spl_ce_RecursiveDirectoryIterator);
851 ZVAL_LONG(&_1, 4096);
852 ZEPHIR_CALL_METHOD(NULL, &_0, "__construct", NULL, 140, &dir, &_1);
853 zephir_check_call_status();
854 ZVAL_LONG(&_1, 2);
855 ZEPHIR_CALL_METHOD(NULL, return_value, "__construct", NULL, 141, &_0, &_1);
856 zephir_check_call_status();
857 RETURN_MM();
858
859 }
860
861 /**
862 * Gets the file contents and returns an array or an error if something
863 * went wrong
864 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,getPayload)865 PHP_METHOD(Phalcon_Storage_Adapter_Stream, getPayload) {
866
867 zend_bool _4;
868 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
869 zend_long ZEPHIR_LAST_CALL_STATUS;
870 zval *filepath_param = NULL, __$false, payload, pointer, _0, _1, _2, _3;
871 zval filepath;
872 zval *this_ptr = getThis();
873
874 ZVAL_UNDEF(&filepath);
875 ZVAL_BOOL(&__$false, 0);
876 ZVAL_UNDEF(&payload);
877 ZVAL_UNDEF(&pointer);
878 ZVAL_UNDEF(&_0);
879 ZVAL_UNDEF(&_1);
880 ZVAL_UNDEF(&_2);
881 ZVAL_UNDEF(&_3);
882
883 ZEPHIR_MM_GROW();
884 zephir_fetch_params(1, 1, 0, &filepath_param);
885
886 zephir_get_strval(&filepath, filepath_param);
887
888
889 ZEPHIR_INIT_VAR(&_0);
890 ZVAL_STRING(&_0, "r");
891 ZEPHIR_CALL_FUNCTION(&pointer, "fopen", NULL, 87, &filepath, &_0);
892 zephir_check_call_status();
893 ZVAL_LONG(&_1, 1);
894 ZEPHIR_CALL_FUNCTION(&_2, "flock", NULL, 142, &pointer, &_1);
895 zephir_check_call_status();
896 if (zephir_is_true(&_2)) {
897 ZEPHIR_INIT_VAR(&payload);
898 zephir_file_get_contents(&payload, &filepath);
899 }
900 zephir_fclose(&pointer);
901 if (ZEPHIR_IS_FALSE_IDENTICAL(&payload)) {
902 array_init(return_value);
903 RETURN_MM();
904 }
905 ZEPHIR_GLOBAL(warning).enable = zend_is_true(&__$false);
906 ZEPHIR_INIT_NVAR(&_0);
907 ZEPHIR_INIT_NVAR(&_0);
908 zephir_create_closure_ex(&_0, NULL, phalcon_1__closure_ce, SL("__invoke"));
909 ZVAL_LONG(&_1, 8);
910 ZEPHIR_CALL_FUNCTION(NULL, "set_error_handler", NULL, 86, &_0, &_1);
911 zephir_check_call_status();
912 ZEPHIR_CALL_FUNCTION(&_3, "unserialize", NULL, 15, &payload);
913 zephir_check_call_status();
914 ZEPHIR_CPY_WRT(&payload, &_3);
915 ZEPHIR_CALL_FUNCTION(NULL, "restore_error_handler", NULL, 88);
916 zephir_check_call_status();
917 _4 = ZEPHIR_GLOBAL(warning).enable;
918 if (!(_4)) {
919 _4 = Z_TYPE_P(&payload) != IS_ARRAY;
920 }
921 if (UNEXPECTED(_4)) {
922 array_init(return_value);
923 RETURN_MM();
924 }
925 RETURN_CCTOR(&payload);
926
927 }
928
929 /**
930 * Returns if the cache has expired for this item or not
931 *
932 * @param array $payload
933 *
934 * @return bool
935 */
PHP_METHOD(Phalcon_Storage_Adapter_Stream,isExpired)936 PHP_METHOD(Phalcon_Storage_Adapter_Stream, isExpired) {
937
938 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
939 zend_long ZEPHIR_LAST_CALL_STATUS;
940 zephir_fcall_cache_entry *_0 = NULL;
941 zval *payload_param = NULL, created, ttl, _1, _2, _3, _4;
942 zval payload;
943 zval *this_ptr = getThis();
944
945 ZVAL_UNDEF(&payload);
946 ZVAL_UNDEF(&created);
947 ZVAL_UNDEF(&ttl);
948 ZVAL_UNDEF(&_1);
949 ZVAL_UNDEF(&_2);
950 ZVAL_UNDEF(&_3);
951 ZVAL_UNDEF(&_4);
952
953 ZEPHIR_MM_GROW();
954 zephir_fetch_params(1, 1, 0, &payload_param);
955
956 ZEPHIR_OBS_COPY_OR_DUP(&payload, payload_param);
957
958
959 ZEPHIR_INIT_VAR(&_1);
960 zephir_time(&_1);
961 ZEPHIR_INIT_VAR(&_2);
962 ZVAL_STRING(&_2, "created");
963 ZEPHIR_CALL_CE_STATIC(&created, phalcon_helper_arr_ce, "get", &_0, 16, &payload, &_2, &_1);
964 zephir_check_call_status();
965 ZEPHIR_INIT_NVAR(&_2);
966 ZVAL_STRING(&_2, "ttl");
967 ZVAL_LONG(&_3, 3600);
968 ZEPHIR_CALL_CE_STATIC(&ttl, phalcon_helper_arr_ce, "get", &_0, 16, &payload, &_2, &_3);
969 zephir_check_call_status();
970 ZEPHIR_INIT_VAR(&_4);
971 zephir_add_function(&_4, &created, &ttl);
972 ZEPHIR_INIT_NVAR(&_2);
973 zephir_time(&_2);
974 RETURN_MM_BOOL(ZEPHIR_LT(&_4, &_2));
975
976 }
977
zephir_init_properties_Phalcon_Storage_Adapter_Stream(zend_class_entry * class_type TSRMLS_DC)978 zend_object *zephir_init_properties_Phalcon_Storage_Adapter_Stream(zend_class_entry *class_type TSRMLS_DC) {
979
980 zval _0, _1$$3;
981 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
982 ZVAL_UNDEF(&_0);
983 ZVAL_UNDEF(&_1$$3);
984
985 ZEPHIR_MM_GROW();
986
987 {
988 zval local_this_ptr, *this_ptr = &local_this_ptr;
989 ZEPHIR_CREATE_OBJECT(this_ptr, class_type);
990 zephir_read_property_ex(&_0, this_ptr, ZEND_STRL("options"), PH_NOISY_CC | PH_READONLY);
991 if (Z_TYPE_P(&_0) == IS_NULL) {
992 ZEPHIR_INIT_VAR(&_1$$3);
993 array_init(&_1$$3);
994 zephir_update_property_zval_ex(this_ptr, ZEND_STRL("options"), &_1$$3);
995 }
996 ZEPHIR_MM_RESTORE();
997 return Z_OBJ_P(this_ptr);
998 }
999
1000 }
1001
1002