[svn:PHP-Sandwich] rev 1026 - in PHP-Sandwich/trunk: . t

[email protected] 28 Apr 2005 06:41:56 -0000
Newsgroups perl.php.sandwich.dev
Message-ID <[email protected]>
Author: shughes
Date: Wed Apr 27 23:41:56 2005
New Revision: 1026

Added:
   PHP-Sandwich/trunk/t/11.t   (contents, props changed)
   PHP-Sandwich/trunk/t/12.t   (contents, props changed)
Modified:
   PHP-Sandwich/trunk/phpfuncs.c
   PHP-Sandwich/trunk/phpinterp.h
Log:
Add Perl class with singleton to get a perl interpreter, add a 
stub PerlObject class which will be the overloaded object that 
provides access into the Perl namespace



Modified: PHP-Sandwich/trunk/phpfuncs.c
==============================================================================
--- PHP-Sandwich/trunk/phpfuncs.c	(original)
+++ PHP-Sandwich/trunk/phpfuncs.c	Wed Apr 27 23:41:56 2005
@@ -1,4 +1,17 @@
+#include <EXTERN.h>
+#include <perl.h>
+#include <perlapi.h>
+
 #include "phpinterp.h"
+#include "phpfuncs.h"
+
+ZEND_DECLARE_MODULE_GLOBALS(sandwich);
+zend_class_entry *pl_ce;
+zend_class_entry *plobj_ce;
+
+static zend_object_handlers plobj_handlers;
+
+static zend_object_value plobj_create_object(zend_class_entry *ce TSRMLS_DC);
 
 
 PHP_MINFO_FUNCTION(sandwich)
@@ -8,6 +21,125 @@
   php_info_print_table_end();
 }
 
+static void
+sw_initglobals(zend_sandwich_globals *swg)
+{
+  memset(&swg->ps_stats, 0, sizeof(swg->ps_stats));
+}
+
+PHP_METHOD(perl, getinstance)
+{
+  struct plobj *pl;
+
+  if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "") == FAILURE) {
+    return;
+  }
+
+
+  return_value->type = IS_OBJECT;
+  return_value->value.obj = plobj_create_object(plobj_ce TSRMLS_CC);
+}
+
+static function_entry pl_functions[] = {
+  PHP_ME(perl, getinstance, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
+  {NULL, NULL, NULL}
+};
+
+static function_entry plobj_functions[] = {
+  {NULL, NULL, NULL}
+};
+
+static void 
+plobj_free(void *obj TSRMLS_DC)
+{
+  struct plobj *pl;
+
+  pl = (struct plobj *) obj;
+  zend_hash_destroy(pl->zo.properties);
+  FREE_HASHTABLE(pl->zo.properties);
+
+  efree(obj);
+}
+
+static void
+plobj_dtor(void *obj, zend_object_handle handle TSRMLS_DC)
+{
+}
+
+static struct plobj *
+plobj_new(zend_class_entry *ce TSRMLS_DC)
+{
+  struct plobj      *pl;
+  zend_object_value  obj;
+  
+  pl = ecalloc(1, sizeof(*pl));
+  pl->zo.ce = ce;
+
+  ALLOC_HASHTABLE(pl->zo.properties);
+  zend_hash_init(pl->zo.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
+
+  return pl;
+}
+
+static void
+plobj_clone(void *obj, void **cp TSRMLS_DC)
+{
+  struct plobj *pl;
+  struct plobj *clone;
+
+  pl = (struct plobj *) obj;
+  
+  clone = plobj_new(pl->zo.ce TSRMLS_CC);
+  if (!clone) {
+    *cp = NULL;
+    return;
+  }
+
+  *cp = (void *) clone;
+}
+
+static zend_object_value 
+plobj_create_object(zend_class_entry *ce TSRMLS_DC)
+{
+  struct plobj      *pl;
+  zend_object_value  obj;
+  
+  pl = plobj_new(ce TSRMLS_CC);
+  
+  obj.handle = zend_objects_store_put(pl, plobj_dtor,
+      (zend_objects_free_object_storage_t) plobj_free, plobj_clone TSRMLS_CC);
+  obj.handlers = (zend_object_handlers *) &plobj_handlers;
+
+  return obj;
+}
+ 
+
+PHP_MINIT_FUNCTION(sandwich)
+{
+  zend_class_entry pl;
+  zend_class_entry plobj;
+  
+  ZEND_INIT_MODULE_GLOBALS(sandwich, sw_initglobals, NULL);
+
+  INIT_CLASS_ENTRY(pl, "Perl", pl_functions);
+  pl_ce = zend_register_internal_class(&pl TSRMLS_CC);
+  if (!pl_ce) {
+    return FAILURE;
+  }
+
+  INIT_CLASS_ENTRY(plobj, "PerlObject", plobj_functions);
+  plobj.create_object = plobj_create_object;
+  plobj_ce = zend_register_internal_class(&plobj TSRMLS_CC);
+  if (!plobj_ce) {
+    return FAILURE;
+  }
+
+  memcpy(&plobj_handlers, zend_get_std_object_handlers(), 
+      sizeof(plobj_handlers));
+
+  return SUCCESS;
+}
+
 
 PHP_FUNCTION(sandwich_test)
 {
@@ -24,7 +156,7 @@
   STANDARD_MODULE_HEADER,
   "sandwich",
   sandwich_functions,
-  NULL,
+  PHP_MINIT(sandwich),
   NULL,
   NULL,
   NULL,
@@ -35,4 +167,4 @@
 
 
 
-/* vim: set ts=2 sts=2 ai bs=2 expandtab : */
+/* vim: set sw=2 ts=2 sts=2 ai bs=2 expandtab : */

Modified: PHP-Sandwich/trunk/phpinterp.h
==============================================================================
--- PHP-Sandwich/trunk/phpinterp.h	(original)
+++ PHP-Sandwich/trunk/phpinterp.h	Wed Apr 27 23:41:56 2005
@@ -1,3 +1,6 @@
+#ifndef _SANDWICH_PHPINTERP_H
+#define _SANDWICH_PHPINTERP_H
+
 #undef module_name
 #undef API_EXPORT
 #include <main/php_config.h>
@@ -11,7 +14,6 @@
 #include <SAPI.h>
 #include <TSRM.h>
 
-
 typedef struct {
   void *ctx;
   int ref;
@@ -31,3 +33,5 @@
 
 #define sandwich_interp_inc_ref(interp)  interp->ref++
 #define sandwich_interp_dec_ref(interp)  if(--interp->ref == 0) sandwich_per_interp_shutdown(interp)
+
+#endif

Added: PHP-Sandwich/trunk/t/11.t
==============================================================================
--- (empty file)
+++ PHP-Sandwich/trunk/t/11.t	Wed Apr 27 23:41:56 2005
@@ -0,0 +1,13 @@
+#!/opt/ecelerity/3rdParty/bin/perl
+use strict;
+use Test::More tests => 5;
+
+BEGIN {
+    use_ok 'PHP' or die;
+    use_ok 'PHP::Interpreter' or die;
+}
+
+diag "Testing basic function autoloadind";
+ok my $p = PHP::Interpreter->new, "Create new PHP interpreter";
+is $p->sandwich_test(), "mmmm... peanut butter",
+  'We should get the proper return value of the "callme" function';

Added: PHP-Sandwich/trunk/t/12.t
==============================================================================
--- (empty file)
+++ PHP-Sandwich/trunk/t/12.t	Wed Apr 27 23:41:56 2005
@@ -0,0 +1,15 @@
+#!/opt/ecelerity/3rdParty/bin/perl
+use strict;
+use Test::More tests => 5;
+
+BEGIN {
+    use_ok 'PHP' or die;
+    use_ok 'PHP::Interpreter' or die;
+}
+
+diag "Testing basic function autoloadind";
+ok my $p = PHP::Interpreter->new, "Create new PHP interpreter";
+ok $p->eval('function callme() { return Perl::getInstance(); }'),
+  'Add a "callme" Perl::getInstance() wrapper';
+ok $p->callme() =~ m/^PHP::Class::PerlObject/,
+  'We should get a perl object back from Perl::getInstance()';