php_vm 1.2.5 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. data/ext/php_vm/php_vm.c +143 -1
  2. metadata +1 -1
data/ext/php_vm/php_vm.c CHANGED
@@ -13,6 +13,40 @@ VALUE rb_cPHPObject;
13
13
  VALUE rb_ePHPError;
14
14
  VALUE rb_ePHPExceptionObject;
15
15
  VALUE rb_ePHPSyntaxError;
16
+ VALUE rb_ePHPErrorReporting;
17
+
18
+
19
+ // PHP Embed
20
+ static int php_embed_output_handler(const char *str, unsigned int str_length TSRMLS_DC)
21
+ {
22
+ VALUE proc = rb_cv_get(rb_mPHPVM, "@@output_handler");
23
+ if (rb_obj_is_proc(proc)) {
24
+ VALUE args = rb_ary_new();
25
+ rb_ary_push(args, rb_str_new(str, str_length));
26
+ rb_proc_call(proc, args);
27
+ return str_length;
28
+ } else {
29
+ printf("%s", str);
30
+ }
31
+ return str_length;
32
+ }
33
+
34
+ static void php_embed_error_handler(char *message)
35
+ {
36
+ VALUE proc = rb_cv_get(rb_mPHPVM, "@@error_handler");
37
+ VALUE report = rb_exc_new2(rb_ePHPErrorReporting, message);
38
+ if (rb_obj_is_proc(proc)) {
39
+ VALUE args = rb_ary_new();
40
+ rb_ary_push(args, report);
41
+ rb_proc_call(proc, args);
42
+ } else {
43
+ if (rb_iv_get(report, "error_level")==ID2SYM(rb_intern("Fatal"))) {
44
+ rb_exc_raise(report);
45
+ } else {
46
+ printf("%s\n", message);
47
+ }
48
+ }
49
+ }
16
50
 
17
51
 
18
52
  // PHP
@@ -472,6 +506,34 @@ zval* get_zval(VALUE self)
472
506
 
473
507
  // module PHPVM
474
508
 
509
+ VALUE rb_php_vm_get_output_handler(VALUE cls)
510
+ {
511
+ return rb_cv_get(rb_mPHPVM, "@@output_handler");
512
+ }
513
+
514
+ VALUE rb_php_vm_set_output_handler(VALUE cls, VALUE proc)
515
+ {
516
+ if (proc!=Qnil && !rb_obj_is_proc(proc)) {
517
+ rb_raise(rb_eArgError, "proc is not proc object");
518
+ }
519
+ rb_cv_set(rb_mPHPVM, "@@output_handler", proc);
520
+ return Qnil;
521
+ }
522
+
523
+ VALUE rb_php_vm_get_error_handler(VALUE cls)
524
+ {
525
+ return rb_cv_get(rb_mPHPVM, "@@error_handler");
526
+ }
527
+
528
+ VALUE rb_php_vm_set_error_handler(VALUE cls, VALUE proc)
529
+ {
530
+ if (proc!=Qnil && !rb_obj_is_proc(proc)) {
531
+ rb_raise(rb_eArgError, "proc is not proc object");
532
+ }
533
+ rb_cv_set(rb_mPHPVM, "@@error_handler", proc);
534
+ return Qnil;
535
+ }
536
+
475
537
  void php_vm_require(char *token, VALUE filepath)
476
538
  {
477
539
  StringValue(filepath);
@@ -910,6 +972,60 @@ VALUE rb_php_exception_object_initialize(int argc, VALUE *argv, VALUE self)
910
972
  return self;
911
973
  }
912
974
 
975
+ // class PHPVM::PHPErrorReporting
976
+
977
+ VALUE rb_php_error_reporting_initialize(int argc, VALUE *argv, VALUE self)
978
+ {
979
+ VALUE log_message = Qnil;
980
+ VALUE error_level = Qnil;
981
+ VALUE message = Qnil;
982
+ VALUE file = Qnil;
983
+ VALUE line = Qnil;
984
+
985
+ if (argc==1 &&TYPE( argv[0])==T_STRING) {
986
+ log_message = argv[0];
987
+ VALUE report_re = rb_funcall(rb_cRegexp, rb_intern("new"), 1, rb_str_new2("^(?:(?:PHP )?([^:]+?)(?: error)?: {0,2})?(.+) in (.+) on line (\\d+)$"));
988
+ VALUE m = rb_funcall(argv[0], rb_intern("match"), 1, report_re);
989
+ if (m!=Qnil) {
990
+ error_level = rb_funcall(m, rb_intern("[]"), 1, INT2NUM(1));
991
+ error_level = ID2SYM(rb_intern(RSTRING_PTR(error_level)));
992
+ message = rb_funcall(m, rb_intern("[]"), 1, INT2NUM(2));
993
+ file = rb_funcall(m, rb_intern("[]"), 1, INT2NUM(3));
994
+ line = rb_funcall(m, rb_intern("[]"), 1, INT2NUM(4));
995
+ line = rb_funcall(line, rb_intern("to_i"), 0);
996
+ argv[0] = message;
997
+ }
998
+ }
999
+ rb_call_super(argc, argv);
1000
+
1001
+ rb_iv_set(self, "log_message", log_message);
1002
+ rb_iv_set(self, "error_level", error_level);
1003
+ rb_iv_set(self, "file", file);
1004
+ rb_iv_set(self, "line", line);
1005
+
1006
+ return self;
1007
+ }
1008
+
1009
+ VALUE rb_php_error_reporting_log_message(VALUE self)
1010
+ {
1011
+ return rb_iv_get(self, "log_message");
1012
+ }
1013
+
1014
+ VALUE rb_php_error_reporting_error_level(VALUE self)
1015
+ {
1016
+ return rb_iv_get(self, "error_level");
1017
+ }
1018
+
1019
+ VALUE rb_php_error_reporting_file(VALUE self)
1020
+ {
1021
+ return rb_iv_get(self, "file");
1022
+ }
1023
+
1024
+ VALUE rb_php_error_reporting_line(VALUE self)
1025
+ {
1026
+ return rb_iv_get(self, "line");
1027
+ }
1028
+
913
1029
 
914
1030
  // module
915
1031
 
@@ -931,12 +1047,27 @@ void php_vm_module_exit()
931
1047
  void Init_php_vm()
932
1048
  {
933
1049
  // initialize php_vm
1050
+ php_embed_module.ub_write = php_embed_output_handler;
1051
+ php_embed_module.log_message = php_embed_error_handler;
1052
+
934
1053
  php_vm_module_init();
935
1054
  atexit(php_vm_module_exit);
936
1055
 
1056
+ // ini
1057
+ zend_try {
1058
+ zend_alter_ini_entry("display_errors", sizeof("display_errors"), "0", sizeof("0")-1, PHP_INI_SYSTEM, PHP_INI_STAGE_RUNTIME);
1059
+ zend_alter_ini_entry("log_errors", sizeof("log_errors"), "1", sizeof("1")-1, PHP_INI_SYSTEM, PHP_INI_STAGE_RUNTIME);
1060
+ } zend_catch {
1061
+ } zend_end_try();
1062
+
937
1063
  // module PHPVM
938
1064
  rb_mPHPVM = rb_define_module("PHPVM");
939
1065
 
1066
+ rb_define_singleton_method(rb_mPHPVM, "output_handler", rb_php_vm_get_output_handler, 0);
1067
+ rb_define_singleton_method(rb_mPHPVM, "output_handler=", rb_php_vm_set_output_handler, 1);
1068
+ rb_define_singleton_method(rb_mPHPVM, "error_handler", rb_php_vm_get_error_handler, 0);
1069
+ rb_define_singleton_method(rb_mPHPVM, "error_handler=", rb_php_vm_set_error_handler, 1);
1070
+
940
1071
  rb_define_singleton_method(rb_mPHPVM, "require", rb_php_vm_require, 1);
941
1072
  rb_define_singleton_method(rb_mPHPVM, "require_once", rb_php_vm_require_once, 1);
942
1073
  rb_define_singleton_method(rb_mPHPVM, "include", rb_php_vm_include, 1);
@@ -946,7 +1077,10 @@ void Init_php_vm()
946
1077
  rb_define_singleton_method(rb_mPHPVM, "get_class", rb_php_vm_get_class, 1);
947
1078
  rb_define_singleton_method(rb_mPHPVM, "define_global", rb_php_vm_define_global, 0);
948
1079
 
949
- rb_define_const(rb_mPHPVM, "VERSION", rb_str_new2("1.2.5"));
1080
+ rb_define_const(rb_mPHPVM, "VERSION", rb_str_new2("1.3.0"));
1081
+
1082
+ rb_cv_set(rb_mPHPVM, "@@output_handler", Qnil);
1083
+ rb_cv_set(rb_mPHPVM, "@@error_handler", Qnil);
950
1084
 
951
1085
  // module PHPVM::PHPGlobal
952
1086
  rb_mPHPGlobal = rb_define_module_under(rb_mPHPVM, "PHPGlobal");
@@ -985,4 +1119,12 @@ void Init_php_vm()
985
1119
 
986
1120
  // class PHPVM::PHPSyntaxError < PHPVM::PHPError
987
1121
  rb_ePHPSyntaxError = rb_define_class_under(rb_mPHPVM, "PHPSyntaxError", rb_ePHPError);
1122
+
1123
+ // class PHPVM::PHPErrorReporting < PHPVM::PHPError
1124
+ rb_ePHPErrorReporting = rb_define_class_under(rb_mPHPVM, "PHPErrorReporting", rb_ePHPError);
1125
+ rb_define_method(rb_ePHPErrorReporting, "initialize", rb_php_error_reporting_initialize, -1);
1126
+ rb_define_method(rb_ePHPErrorReporting, "log_message", rb_php_error_reporting_log_message, 0);
1127
+ rb_define_method(rb_ePHPErrorReporting, "error_level", rb_php_error_reporting_error_level, 0);
1128
+ rb_define_method(rb_ePHPErrorReporting, "file", rb_php_error_reporting_file, 0);
1129
+ rb_define_method(rb_ePHPErrorReporting, "line", rb_php_error_reporting_line, 0);
988
1130
  }
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: php_vm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.5
4
+ version: 1.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: