Skip to content

Commit 201d453

Browse files
committed
Feat(Close): custom free resource
1 parent 50014db commit 201d453

File tree

3 files changed

+104
-27
lines changed

3 files changed

+104
-27
lines changed

include/xlswriter.h

Lines changed: 64 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -118,22 +118,6 @@ typedef struct _vtiful_validation_object {
118118
zend_object zo;
119119
} validation_object;
120120

121-
static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
122-
return (xls_object *)((char *)(obj) - XtOffsetOf(xls_object, zo));
123-
}
124-
125-
static inline format_object *php_vtiful_format_fetch_object(zend_object *obj) {
126-
return (format_object *)((char *)(obj) - XtOffsetOf(format_object, zo));
127-
}
128-
129-
static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
130-
return (chart_object *)((char *)(obj) - XtOffsetOf(chart_object, zo));
131-
}
132-
133-
static inline validation_object *php_vtiful_validation_fetch_object(zend_object *obj) {
134-
return (validation_object *)((char *)(obj) - XtOffsetOf(validation_object, zo));
135-
}
136-
137121
#define REGISTER_CLASS_CONST_LONG(class_name, const_name, value) \
138122
zend_declare_class_constant_long(class_name, const_name, sizeof(const_name)-1, (zend_long)value);
139123

@@ -223,6 +207,70 @@ static inline validation_object *php_vtiful_validation_fetch_object(zend_object
223207
#define PROP_OBJ(zv) Z_OBJ_P(zv)
224208
#endif
225209

210+
static inline xls_object *php_vtiful_xls_fetch_object(zend_object *obj) {
211+
if (obj == NULL) {
212+
return NULL;
213+
}
214+
215+
return (xls_object *)((char *)(obj) - XtOffsetOf(xls_object, zo));
216+
}
217+
218+
static inline format_object *php_vtiful_format_fetch_object(zend_object *obj) {
219+
if (obj == NULL) {
220+
return NULL;
221+
}
222+
223+
return (format_object *)((char *)(obj) - XtOffsetOf(format_object, zo));
224+
}
225+
226+
static inline chart_object *php_vtiful_chart_fetch_object(zend_object *obj) {
227+
if (obj == NULL) {
228+
return NULL;
229+
}
230+
231+
return (chart_object *)((char *)(obj) - XtOffsetOf(chart_object, zo));
232+
}
233+
234+
static inline validation_object *php_vtiful_validation_fetch_object(zend_object *obj) {
235+
if (obj == NULL) {
236+
return NULL;
237+
}
238+
239+
return (validation_object *)((char *)(obj) - XtOffsetOf(validation_object, zo));
240+
}
241+
242+
static inline void php_vtiful_close_resource(zend_object *obj) {
243+
if (obj == NULL) {
244+
return;
245+
}
246+
247+
xls_object *intern = php_vtiful_xls_fetch_object(obj);
248+
249+
SHEET_LINE_INIT(intern);
250+
251+
if (intern->write_ptr.workbook != NULL) {
252+
lxw_workbook_free(intern->write_ptr.workbook);
253+
intern->write_ptr.workbook = NULL;
254+
}
255+
256+
if (intern->format_ptr.format != NULL) {
257+
intern->format_ptr.format = NULL;
258+
}
259+
260+
#ifdef ENABLE_READER
261+
if (intern->read_ptr.sheet_t != NULL) {
262+
xlsxioread_sheet_close(intern->read_ptr.sheet_t);
263+
intern->read_ptr.sheet_t = NULL;
264+
}
265+
266+
if (intern->read_ptr.file_t != NULL) {
267+
xlsxioread_close(intern->read_ptr.file_t);
268+
intern->read_ptr.file_t = NULL;
269+
}
270+
#endif
271+
272+
intern->read_ptr.data_type_default = READ_TYPE_EMPTY;
273+
}
226274

227275
lxw_format * zval_get_format(zval *handle);
228276
lxw_data_validation * zval_get_validation(zval *resource);

kernel/excel.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ PHP_VTIFUL_API zend_object *vtiful_xls_objects_new(zend_class_entry *ce)
3737

3838
intern->read_ptr.file_t = NULL;
3939
intern->read_ptr.sheet_t = NULL;
40-
intern->format_ptr.format = NULL;
40+
41+
intern->format_ptr.format = NULL;
42+
intern->write_ptr.workbook = NULL;
4143

4244
intern->read_ptr.data_type_default = READ_TYPE_EMPTY;
4345

@@ -51,16 +53,7 @@ static void vtiful_xls_objects_free(zend_object *object)
5153
{
5254
xls_object *intern = php_vtiful_xls_fetch_object(object);
5355

54-
lxw_workbook_free(intern->write_ptr.workbook);
55-
56-
#ifdef ENABLE_READER
57-
xlsxioread_sheet_close(intern->read_ptr.sheet_t);
58-
xlsxioread_close(intern->read_ptr.file_t);
59-
#endif
60-
61-
if (intern->format_ptr.format != NULL) {
62-
intern->format_ptr.format = NULL;
63-
}
56+
php_vtiful_close_resource(object);
6457

6558
zend_object_std_dtor(&intern->zo);
6659
}
@@ -72,6 +65,9 @@ ZEND_BEGIN_ARG_INFO_EX(xls_construct_arginfo, 0, 0, 1)
7265
ZEND_ARG_INFO(0, config)
7366
ZEND_END_ARG_INFO()
7467

68+
ZEND_BEGIN_ARG_INFO_EX(xls_close_arginfo, 0, 0, 0)
69+
ZEND_END_ARG_INFO()
70+
7571
ZEND_BEGIN_ARG_INFO_EX(xls_file_name_arginfo, 0, 0, 1)
7672
ZEND_ARG_INFO(0, file_name)
7773
ZEND_ARG_INFO(0, sheet_name)
@@ -310,6 +306,16 @@ PHP_METHOD(vtiful_xls, __construct)
310306
}
311307
/* }}} */
312308

309+
/** {{{ \Vtiful\Kernel\Excel::close()
310+
*/
311+
PHP_METHOD(vtiful_xls, close)
312+
{
313+
php_vtiful_close_resource(Z_OBJ_P(getThis()));
314+
315+
ZVAL_COPY(return_value, getThis());
316+
}
317+
/* }}} */
318+
313319
/** {{{ \Vtiful\Kernel\Excel::filename(string $fileName [, string $sheetName])
314320
*/
315321
PHP_METHOD(vtiful_xls, fileName)
@@ -1489,6 +1495,7 @@ PHP_METHOD(vtiful_xls, nextCellCallback)
14891495
*/
14901496
zend_function_entry xls_methods[] = {
14911497
PHP_ME(vtiful_xls, __construct, xls_construct_arginfo, ZEND_ACC_PUBLIC)
1498+
PHP_ME(vtiful_xls, close, xls_close_arginfo, ZEND_ACC_PUBLIC)
14921499
PHP_ME(vtiful_xls, fileName, xls_file_name_arginfo, ZEND_ACC_PUBLIC)
14931500
PHP_ME(vtiful_xls, addSheet, xls_file_add_sheet, ZEND_ACC_PUBLIC)
14941501
PHP_ME(vtiful_xls, checkoutSheet, xls_file_checkout_sheet, ZEND_ACC_PUBLIC)

tests/close.phpt

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
--TEST--
2+
Check for vtiful presence
3+
--SKIPIF--
4+
<?php if (!extension_loaded("xlswriter")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
$config = ['path' => './tests'];
8+
$excel = new \Vtiful\Kernel\Excel($config);
9+
$filePath = $excel->fileName('tutorial01.xlsx')
10+
->header(['Item', 'Cost'])
11+
->output();
12+
13+
$excel->close();
14+
15+
var_dump($filePath);
16+
?>
17+
--CLEAN--
18+
<?php
19+
@unlink(__DIR__ . '/tutorial01.xlsx');
20+
?>
21+
--EXPECT--
22+
string(23) "./tests/tutorial01.xlsx"

0 commit comments

Comments
 (0)