瀏覽代碼

FIX: in the thread safe build, it will lead to access violations and

viest 7 年之前
父節點
當前提交
6e9d8da057
共有 12 個文件被更改,包括 60 次插入49 次删除
  1. 0 4
      excel_writer.c
  2. 20 24
      kernel/excel.c
  3. 0 2
      kernel/include.h
  4. 1 1
      php_excel_writer.h
  5. 2 6
      tests/002.phpt
  6. 4 4
      tests/003.phpt
  7. 4 4
      tests/004.phpt
  8. 1 1
      tests/006.phpt
  9. 1 1
      tests/007.phpt
  10. 1 1
      tests/008.phpt
  11. 1 1
      tests/009.phpt
  12. 25 0
      tests/010.phpt

+ 0 - 4
excel_writer.c

@@ -18,8 +18,6 @@
 #include "ext/standard/info.h"
 #include "kernel/include.h"
 
-excel_resource_t *excel_res;
-
 int le_excel_writer;
 
 /* {{{ PHP_MINIT_FUNCTION
@@ -30,8 +28,6 @@ PHP_MINIT_FUNCTION(excel_writer)
 	VTIFUL_STARTUP_MODULE(excel);
 	VTIFUL_STARTUP_MODULE(format);
 
-	excel_res = malloc(sizeof(excel_resource_t));
-
     le_excel_writer = zend_register_list_destructors_ex(_php_vtiful_excel_close, NULL, VTIFUL_RESOURCE_NAME, module_number);
 
 	return SUCCESS;

+ 20 - 24
kernel/excel.c

@@ -18,6 +18,8 @@
 
 zend_class_entry *vtiful_excel_ce;
 
+static excel_resource_t *excel_res;
+
 /* {{{ ARG_INFO
  */
 ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
@@ -99,7 +101,7 @@ PHP_METHOD(vtiful_excel, __construct)
         return;
     }
 
-    zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
+    add_property_zval(getThis(), V_EXCEL_COF, config);
 }
 /* }}} */
 
@@ -107,7 +109,7 @@ PHP_METHOD(vtiful_excel, __construct)
  */
 PHP_METHOD(vtiful_excel, fileName)
 {
-    zval rv, file_path, handle, *dir_path;
+    zval file_path, *dir_path;
     zend_string *file_name;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -120,13 +122,13 @@ PHP_METHOD(vtiful_excel, fileName)
 
     excel_file_path(file_name, dir_path, &file_path);
 
+    excel_res = malloc(sizeof(excel_resource_t));
+
     excel_res->workbook  = workbook_new(Z_STRVAL(file_path));
     excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
-    ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
-
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
+    add_property_zval(return_value, V_EXCEL_FIL, &file_path);
+    add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
 
     zval_ptr_dtor(&file_path);
 }
@@ -136,7 +138,7 @@ PHP_METHOD(vtiful_excel, fileName)
  */
 PHP_METHOD(vtiful_excel, constMemory)
 {
-    zval file_path, handle, *dir_path;
+    zval file_path, *dir_path;
     zend_string *file_name;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -151,13 +153,13 @@ PHP_METHOD(vtiful_excel, constMemory)
 
     lxw_workbook_options options = {.constant_memory = LXW_TRUE, .tmpdir = NULL};
 
+    excel_res = malloc(sizeof(excel_resource_t));
+
     excel_res->workbook  = workbook_new_opt(Z_STRVAL(file_path), &options);
     excel_res->worksheet = workbook_add_worksheet(excel_res->workbook, NULL);
 
-    ZVAL_RES(&handle, zend_register_resource(excel_res, le_excel_writer));
-
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &handle);
+    add_property_zval(return_value, V_EXCEL_FIL, &file_path);
+    add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
 
     zval_ptr_dtor(&file_path);
 }
@@ -168,7 +170,7 @@ PHP_METHOD(vtiful_excel, constMemory)
  */
 PHP_METHOD(vtiful_excel, header)
 {
-    zval res_handle, *header, *header_value;
+    zval *header, *header_value;
     zend_long header_l_key;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -182,8 +184,7 @@ PHP_METHOD(vtiful_excel, header)
          zval_ptr_dtor(header_value);
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
+    add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
 }
 /* }}} */
 
@@ -191,7 +192,7 @@ PHP_METHOD(vtiful_excel, header)
  */
 PHP_METHOD(vtiful_excel, data)
 {
-    zval *data, res_handle, *data_r_value, *data_l_value;
+    zval *data, *data_r_value, *data_l_value;
     zend_long data_r_key, data_l_key;
 
     ZEND_PARSE_PARAMETERS_START(1, 1)
@@ -209,8 +210,7 @@ PHP_METHOD(vtiful_excel, data)
         }
     } ZEND_HASH_FOREACH_END();
 
-    ZVAL_RES(&res_handle, zend_register_resource(excel_res, le_excel_writer));
-    zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_HANDLE), &res_handle);
+    add_property_resource(return_value, V_EXCEL_HANDLE, zend_register_resource(excel_res, le_excel_writer));
 }
 /* }}} */
 
@@ -218,7 +218,7 @@ PHP_METHOD(vtiful_excel, data)
  */
 PHP_METHOD(vtiful_excel, output)
 {
-    zval rv, null_handle, *handle, *file_path;
+    zval rv, *handle, *file_path;
 
     handle    = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), 0, &rv TSRMLS_DC);
     file_path = zend_read_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_FIL), 0, &rv TSRMLS_DC);
@@ -227,8 +227,8 @@ PHP_METHOD(vtiful_excel, output)
 
     free(excel_res);
 
-    ZVAL_NULL(&null_handle);
-    zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_HANDLE), &null_handle);
+    add_property_null(getThis(), V_EXCEL_HANDLE);
+    add_property_null(getThis(), V_EXCEL_PAT);
 
     ZVAL_COPY(return_value, file_path);
 }
@@ -450,10 +450,6 @@ VTIFUL_STARTUP_FUNCTION(excel) {
 
     vtiful_excel_ce = zend_register_internal_class(&ce);
 
-    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_COF,    ZEND_ACC_PRIVATE);
-    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_FIL,    ZEND_ACC_PRIVATE);
-    REGISTER_CLASS_PROPERTY_NULL(vtiful_excel_ce, V_EXCEL_HANDLE, ZEND_ACC_PRIVATE);
-
     return SUCCESS;
 }
 /* }}} */

+ 0 - 2
kernel/include.h

@@ -31,8 +31,6 @@ typedef struct {
     lxw_worksheet *worksheet;
 } excel_resource_t;
 
-extern excel_resource_t *excel_res;
-
 excel_resource_t * zval_get_resource(zval *handle);
 lxw_format       * zval_get_format(zval *handle);
 

+ 1 - 1
php_excel_writer.h

@@ -32,7 +32,7 @@ extern zend_module_entry excel_writer_module_entry;
 #include "TSRM.h"
 #endif
 
-#define VTIFUL_RESOURCE_NAME "vtiful"
+#define VTIFUL_RESOURCE_NAME "excel"
 
 extern int le_excel_writer;
 

+ 2 - 6
tests/002.phpt

@@ -9,14 +9,10 @@ $excel = new \Vtiful\Kernel\Excel($config);
 var_dump($excel);
 ?>
 --EXPECT--
-object(Vtiful\Kernel\Excel)#1 (3) {
-  ["config":"Vtiful\Kernel\Excel":private]=>
+object(Vtiful\Kernel\Excel)#1 (1) {
+  ["config"]=>
   array(1) {
     ["path"]=>
     string(7) "./tests"
   }
-  ["fileName":"Vtiful\Kernel\Excel":private]=>
-  NULL
-  ["handle":"Vtiful\Kernel\Excel":private]=>
-  NULL
 }

+ 4 - 4
tests/003.phpt

@@ -11,13 +11,13 @@ var_dump($fileFd);
 ?>
 --EXPECT--
 object(Vtiful\Kernel\Excel)#1 (3) {
-  ["config":"Vtiful\Kernel\Excel":private]=>
+  ["config"]=>
   array(1) {
     ["path"]=>
     string(7) "./tests"
   }
-  ["fileName":"Vtiful\Kernel\Excel":private]=>
+  ["fileName"]=>
   string(23) "./tests/tutorial01.xlsx"
-  ["handle":"Vtiful\Kernel\Excel":private]=>
-  resource(4) of type (vtiful)
+  ["handle"]=>
+  resource(4) of type (excel)
 }

+ 4 - 4
tests/004.phpt

@@ -12,13 +12,13 @@ var_dump($setHeader);
 ?>
 --EXPECT--
 object(Vtiful\Kernel\Excel)#1 (3) {
-  ["config":"Vtiful\Kernel\Excel":private]=>
+  ["config"]=>
   array(1) {
     ["path"]=>
     string(7) "./tests"
   }
-  ["fileName":"Vtiful\Kernel\Excel":private]=>
+  ["fileName"]=>
   string(23) "./tests/tutorial01.xlsx"
-  ["handle":"Vtiful\Kernel\Excel":private]=>
-  resource(5) of type (vtiful)
+  ["handle"]=>
+  resource(5) of type (excel)
 }

+ 1 - 1
tests/006.phpt

@@ -11,4 +11,4 @@ $handle = $excel->fileName('tutorial01.xlsx')
 var_dump($handle);
 ?>
 --EXPECT--
-resource(4) of type (vtiful)
+resource(4) of type (excel)

+ 1 - 1
tests/007.phpt

@@ -12,4 +12,4 @@ $boldStyle = \Vtiful\Kernel\Format::bold($handle);
 var_dump($boldStyle);
 ?>
 --EXPECT--
-resource(5) of type (vtiful)
+resource(5) of type (excel)

+ 1 - 1
tests/008.phpt

@@ -12,4 +12,4 @@ $italicStyle = \Vtiful\Kernel\Format::italic($handle);
 var_dump($italicStyle);
 ?>
 --EXPECT--
-resource(5) of type (vtiful)
+resource(5) of type (excel)

+ 1 - 1
tests/009.phpt

@@ -12,4 +12,4 @@ $underlineStyle = \Vtiful\Kernel\Format::underline($handle, \Vtiful\Kernel\Forma
 var_dump($underlineStyle);
 ?>
 --EXPECT--
-resource(5) of type (vtiful)
+resource(5) of type (excel)

+ 25 - 0
tests/010.phpt

@@ -0,0 +1,25 @@
+--TEST--
+Check for vtiful presence
+--SKIPIF--
+<?php if (!extension_loaded("excel_writer")) print "skip"; ?>
+--FILE--
+<?php 
+$config = ['path' => './tests'];
+$excel  = new \Vtiful\Kernel\Excel($config);
+$fileOne = $excel->fileName('tutorial01.xlsx')
+    ->header(['test1'])
+    ->data([
+      ['data1'],
+    ])
+    ->output();
+$fileTwo = $excel->fileName('tutorial02.xlsx')
+    ->header(['test2'])
+    ->data([
+        ['data2'],
+    ])
+    ->output();
+var_dump($fileOne,$fileTwo);
+?>
+--EXPECT--
+string(23) "./tests/tutorial01.xlsx"
+string(23) "./tests/tutorial02.xlsx"