excel.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #ifdef HAVE_CONFIG_H
  2. #include "config.h"
  3. #endif
  4. #include "zend.h"
  5. #include "zend_API.h"
  6. #include "zend_exceptions.h"
  7. #include "php.h"
  8. #include "xlsxwriter.h"
  9. #include "php_vtiful.h"
  10. #include "excel.h"
  11. #include "exception.h"
  12. #include "ext/standard/php_var.h"
  13. typedef struct {
  14. lxw_workbook *workbook;
  15. lxw_worksheet *worksheet;
  16. } excel_resource_t;
  17. zend_class_entry *vtiful_excel_ce;
  18. ZEND_BEGIN_ARG_INFO_EX(excel_construct_arginfo, 0, 0, 1)
  19. ZEND_ARG_INFO(0, config)
  20. ZEND_END_ARG_INFO()
  21. ZEND_BEGIN_ARG_INFO_EX(excel_file_name_arginfo, 0, 0, 1)
  22. ZEND_ARG_INFO(0, file_name)
  23. ZEND_END_ARG_INFO()
  24. /* {{{ \Vtiful\Kernel\Excel::__construct(array $config)
  25. */
  26. PHP_METHOD(vtiful_excel, __construct)
  27. {
  28. zval *config;
  29. zend_string *key;
  30. ZEND_PARSE_PARAMETERS_START(1, 1)
  31. Z_PARAM_ARRAY(config)
  32. ZEND_PARSE_PARAMETERS_END();
  33. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  34. if(zend_hash_find(Z_ARRVAL_P(config), key) == NULL)
  35. {
  36. zend_throw_exception(vtiful_exception_ce, "Lack of 'path' configuration", 110);
  37. }
  38. zend_update_property(vtiful_excel_ce, getThis(), ZEND_STRL(V_EXCEL_COF), config);
  39. zend_string_release(key);
  40. }
  41. /* }}} */
  42. /* {{{ \Vtiful\Kernel\Excel::filename(string $fileName)
  43. */
  44. PHP_METHOD(vtiful_excel, fileName)
  45. {
  46. zval rv, tmp_file_name, *config, *tmp_path, file_path;
  47. zend_string *file_name, *key;
  48. return_value = getThis();
  49. ZEND_PARSE_PARAMETERS_START(1, 1)
  50. Z_PARAM_STR(file_name)
  51. ZEND_PARSE_PARAMETERS_END();
  52. key = zend_string_init(V_EXCEL_PAT, sizeof(V_EXCEL_PAT)-1, 0);
  53. config = zend_read_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_COF), 0, &rv TSRMLS_DC);
  54. tmp_path = zend_hash_find(Z_ARRVAL_P(config), key);
  55. zend_string_release(key);
  56. if(!tmp_path && Z_TYPE_P(tmp_path) != IS_STRING)
  57. {
  58. zend_throw_exception(vtiful_exception_ce, "Configure 'path' must be a string type", 120);
  59. }
  60. ZVAL_STR(&tmp_file_name, file_name);
  61. concat_function(&file_path, tmp_path, &tmp_file_name);
  62. zend_update_property(vtiful_excel_ce, return_value, ZEND_STRL(V_EXCEL_FIL), &file_path);
  63. zval_ptr_dtor(&file_path);
  64. }
  65. /* }}} */
  66. zend_function_entry excel_methods[] = {
  67. PHP_ME(vtiful_excel, __construct, excel_construct_arginfo, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
  68. PHP_ME(vtiful_excel, fileName, excel_file_name_arginfo, ZEND_ACC_PUBLIC)
  69. PHP_FE_END
  70. };
  71. VTIFUL_STARTUP_FUNCTION(excel) {
  72. zend_class_entry ce;
  73. INIT_NS_CLASS_ENTRY(ce, "Vtiful\\Kernel", "Excel", excel_methods);
  74. vtiful_excel_ce = zend_register_internal_class(&ce);
  75. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_FIL), ZEND_ACC_PRIVATE);
  76. zend_declare_property_null(vtiful_excel_ce, ZEND_STRL(V_EXCEL_COF), ZEND_ACC_PRIVATE);
  77. return SUCCESS;
  78. }