[mapserver-commits] r10253 - in trunk/mapserver: . mapscript/php

svn at osgeo.org svn at osgeo.org
Fri Jun 25 16:11:51 EDT 2010


Author: aboudreault
Date: 2010-06-25 20:11:51 +0000 (Fri, 25 Jun 2010)
New Revision: 10253

Modified:
   trunk/mapserver/HISTORY.TXT
   trunk/mapserver/mapscript/php/class.c
   trunk/mapserver/mapscript/php/map.c
   trunk/mapserver/mapscript/php/php_mapscript.h
   trunk/mapserver/mapscript/php/php_mapscript_util.c
   trunk/mapserver/mapscript/php/style.c
Log:
PHP/MapScript: ability to use the php clone keyword (#3472)

Modified: trunk/mapserver/HISTORY.TXT
===================================================================
--- trunk/mapserver/HISTORY.TXT	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/HISTORY.TXT	2010-06-25 20:11:51 UTC (rev 10253)
@@ -14,6 +14,8 @@
 Current Version (SVN trunk):
 ----------------------------
 
+- PHP/MapScript: ability to use the php clone keyword (#3472)
+
 - Modified mapserver units enum order to fix some problems with external softwares (#3173)
 
 - Fixed configure does not detect libGD version dependencies (#3478)

Modified: trunk/mapserver/mapscript/php/class.c
===================================================================
--- trunk/mapserver/mapscript/php/class.c	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/mapscript/php/class.c	2010-06-25 20:11:51 UTC (rev 10253)
@@ -32,6 +32,7 @@
 #include "php_mapscript.h"
 
 zend_class_entry *mapscript_ce_class;
+zend_object_handlers mapscript_class_object_handlers;
 
 ZEND_BEGIN_ARG_INFO_EX(class___construct_args, 0, 0, 1)
   ZEND_ARG_OBJ_INFO(0, layer, layerObj, 0)
@@ -217,35 +218,6 @@
     }
 }
 
-PHP_METHOD(classObj, __clone)
-{
-    zval *zobj = getThis();
-    php_class_object *php_class;
-    php_layer_object *php_layer;
-    classObj *class = NULL;
-
-    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
-    if (zend_parse_parameters_none() == FAILURE) {
-        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-        return;
-    }
-    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-
-    php_class = (php_class_object *) zend_object_store_get_object(zobj TSRMLS_CC);
-    php_layer = (php_layer_object *) zend_object_store_get_object(php_class->parent.val TSRMLS_CC);
-
-    class = classObj_clone(php_class->class, php_layer->layer);
-    
-    if (class == NULL)
-    {
-        mapscript_report_mapserver_error(E_WARNING TSRMLS_CC);
-        RETURN_NULL();
-    }
-    
-    mapscript_create_class(class, php_class->parent, return_value TSRMLS_CC);
-}
-/* }}} */
-
 /* {{{ proto int updateFromString(string snippet)
    Update a class from a string snippet.  Returns MS_SUCCESS/MS_FAILURE */
 PHP_METHOD(classObj, updateFromString)
@@ -727,8 +699,6 @@
     PHP_ME(classObj, __get, class___get_args, ZEND_ACC_PUBLIC)
     PHP_ME(classObj, __set, class___set_args, ZEND_ACC_PUBLIC)
     PHP_MALIAS(classObj, set, __set, NULL, ZEND_ACC_PUBLIC)
-    PHP_ME(classObj, __clone, NULL, ZEND_ACC_PUBLIC)
-    PHP_MALIAS(classObj, clone, __clone, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(classObj, updateFromString, class_updateFromString_args, ZEND_ACC_PUBLIC)
     PHP_ME(classObj, setExpression, class_setExpression_args, ZEND_ACC_PUBLIC)
     PHP_ME(classObj, getExpressionString, NULL, ZEND_ACC_PUBLIC)
@@ -774,16 +744,20 @@
     efree(object);
 }
 
-static zend_object_value mapscript_class_object_new(zend_class_entry *ce TSRMLS_DC)
+static zend_object_value mapscript_class_object_new_ex(zend_class_entry *ce, php_class_object **ptr TSRMLS_DC)
 {
     zend_object_value retval;
     php_class_object *php_class;
 
     MAPSCRIPT_ALLOC_OBJECT(php_class, php_class_object);
 
-    retval = mapscript_object_new(&php_class->std, ce,
-                                  &mapscript_class_object_destroy TSRMLS_CC);
+    retval = mapscript_object_new_ex(&php_class->std, ce,
+                                     &mapscript_class_object_destroy,
+                                     &mapscript_class_object_handlers TSRMLS_CC);
 
+    if (ptr)
+        *ptr = php_class;
+
     MAPSCRIPT_INIT_PARENT(php_class->parent);
 
     php_class->label = NULL;
@@ -792,10 +766,35 @@
     return retval;
 }
 
+static zend_object_value mapscript_class_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+    return mapscript_class_object_new_ex(ce, NULL TSRMLS_CC);
+}
+
+static zend_object_value mapscript_class_object_clone(zval *zobj TSRMLS_DC)
+{
+    php_class_object *php_class_old, *php_class_new;
+    php_layer_object *php_layer;
+    zend_object_value new_ov;
+
+    php_class_old = (php_class_object *) zend_object_store_get_object(zobj TSRMLS_CC);
+    php_layer = (php_layer_object *) zend_object_store_get_object(php_class_old->parent.val TSRMLS_CC);
+
+    new_ov = mapscript_class_object_new_ex(mapscript_ce_class, &php_class_new TSRMLS_CC); 
+    zend_objects_clone_members(&php_class_new->std, new_ov, &php_class_old->std, Z_OBJ_HANDLE_P(zobj) TSRMLS_CC);
+
+    php_class_new->class = classObj_clone(php_class_old->class, php_layer->layer);
+
+    return new_ov;
+}
+
 PHP_MINIT_FUNCTION(class)
 {
     zend_class_entry ce;
 
+    memcpy(&mapscript_class_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+    mapscript_class_object_handlers.clone_obj = mapscript_class_object_clone;
+
     MAPSCRIPT_REGISTER_CLASS("classObj", 
                              class_functions,
                              mapscript_ce_class,

Modified: trunk/mapserver/mapscript/php/map.c
===================================================================
--- trunk/mapserver/mapscript/php/map.c	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/mapscript/php/map.c	2010-06-25 20:11:51 UTC (rev 10253)
@@ -32,6 +32,7 @@
 #include "php_mapscript.h"
 
 zend_class_entry *mapscript_ce_map;
+zend_object_handlers mapscript_map_object_handlers;
 
 static int mapscript_map_setProjection(int isWKTProj, php_map_object *php_map,
                                        char *projString, int setUnitsAndExtents TSRMLS_DC);
@@ -443,35 +444,6 @@
     }         
 }
 
-/* {{{ proto int map.__clone()
-   Make a copy of this mapObj and returne a refrence to it. Returns NULL on error. */
-PHP_METHOD(mapObj, __clone)
-{
-    zval *zobj = getThis();
-    mapObj *map = NULL;
-    php_map_object *php_map;
-
-    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
-    if (zend_parse_parameters_none() == FAILURE) {
-        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-        return;
-    }
-    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-    
-    php_map = (php_map_object *) zend_object_store_get_object(zobj TSRMLS_CC);
-
-    /* Make a copy of current map object */
-    if ((map = mapObj_clone(php_map->map)) == NULL)
-    {
-        mapscript_report_mapserver_error(E_WARNING TSRMLS_CC);
-        RETURN_NULL();
-    }
-
-    /* Return new map object */
-    mapscript_create_map(map, return_value TSRMLS_CC);
-}
-/* }}} */
-
 /* {{{ proto int map.getSymbolByName(string symbolName)
    Returns the symbol index using the name. */
 PHP_METHOD(mapObj, getSymbolByName)
@@ -3394,8 +3366,6 @@
     PHP_ME(mapObj, __get, map___get_args, ZEND_ACC_PUBLIC)
     PHP_ME(mapObj, __set, map___set_args, ZEND_ACC_PUBLIC)
     PHP_MALIAS(mapObj, set, __set, NULL, ZEND_ACC_PUBLIC)
-    PHP_ME(mapObj, __clone, NULL, ZEND_ACC_PUBLIC)
-    PHP_MALIAS(mapObj, clone, __clone, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(mapObj, getSymbolByName, map_getSymbolByName_args, ZEND_ACC_PUBLIC)
     PHP_ME(mapObj, getSymbolObjectById, map_getSymbolObjectById_args, ZEND_ACC_PUBLIC)
     PHP_ME(mapObj, prepareQuery, NULL, ZEND_ACC_PUBLIC)
@@ -3575,16 +3545,20 @@
     efree(object);
 }
 
-static zend_object_value mapscript_map_object_new(zend_class_entry *ce TSRMLS_DC)
+static zend_object_value mapscript_map_object_new_ex(zend_class_entry *ce, php_map_object **ptr TSRMLS_DC)
 {
     zend_object_value retval;
     php_map_object *php_map;
 
     MAPSCRIPT_ALLOC_OBJECT(php_map, php_map_object);
 
-    retval = mapscript_object_new(&php_map->std, ce,
-                                  &mapscript_map_object_destroy TSRMLS_CC);
+    retval = mapscript_object_new_ex(&php_map->std, ce,
+                                     &mapscript_map_object_destroy,
+                                     &mapscript_map_object_handlers TSRMLS_CC);
 
+    if (ptr)
+        *ptr = php_map;
+
     php_map->outputformat = NULL;
     php_map->extent = NULL;
     php_map->web = NULL;
@@ -3600,10 +3574,33 @@
     return retval;
 }
 
+static zend_object_value mapscript_map_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+    return mapscript_map_object_new_ex(ce, NULL TSRMLS_CC);
+}
+
+static zend_object_value mapscript_map_object_clone(zval *zobj TSRMLS_DC)
+{
+    php_map_object *php_map_old, *php_map_new;
+    zend_object_value new_ov;
+
+    php_map_old = (php_map_object *) zend_object_store_get_object(zobj TSRMLS_CC);
+
+    new_ov = mapscript_map_object_new_ex(mapscript_ce_map, &php_map_new TSRMLS_CC); 
+    zend_objects_clone_members(&php_map_new->std, new_ov, &php_map_old->std, Z_OBJ_HANDLE_P(zobj) TSRMLS_CC);
+
+    php_map_new->map = mapObj_clone(php_map_old->map);
+
+    return new_ov;
+}
+
 PHP_MINIT_FUNCTION(map)
 {
     zend_class_entry ce;
 
+    memcpy(&mapscript_map_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+    mapscript_map_object_handlers.clone_obj = mapscript_map_object_clone;
+
     MAPSCRIPT_REGISTER_CLASS("mapObj", 
                              map_functions,
                              mapscript_ce_map,

Modified: trunk/mapserver/mapscript/php/php_mapscript.h
===================================================================
--- trunk/mapserver/mapscript/php/php_mapscript.h	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/mapscript/php/php_mapscript.h	2010-06-25 20:11:51 UTC (rev 10253)
@@ -427,6 +427,9 @@
 /* PHP Object constructors */
 extern zend_object_value mapscript_object_new(zend_object *zobj, zend_class_entry *ce,
                                               void (*zend_objects_free_object) TSRMLS_DC);
+extern zend_object_value mapscript_object_new_ex(zend_object *zobj, zend_class_entry *ce,
+                                                 void (*zend_objects_free_object),
+                                                 zend_object_handlers *object_handlers TSRMLS_DC);
 extern void mapscript_fetch_object(zend_class_entry *ce, zval* zval_parent, php_layer_object* layer, 
                                    void *internal_object, zval **php_object_storage, 
                                    zval ***return_value_ptr TSRMLS_DC);

Modified: trunk/mapserver/mapscript/php/php_mapscript_util.c
===================================================================
--- trunk/mapserver/mapscript/php/php_mapscript_util.c	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/mapscript/php/php_mapscript_util.c	2010-06-25 20:11:51 UTC (rev 10253)
@@ -48,6 +48,23 @@
     return retval;
 }
 
+zend_object_value mapscript_object_new_ex(zend_object *zobj,
+                                          zend_class_entry *ce,
+                                          void (*zend_objects_free_object),
+                                          zend_object_handlers *object_handlers TSRMLS_DC)
+{
+    zend_object_value retval;
+    zval *temp;
+    
+    zobj->ce = ce;
+    ALLOC_HASHTABLE(zobj->properties);
+    zend_hash_init(zobj->properties, 0, NULL, ZVAL_PTR_DTOR, 0);
+    zend_hash_copy(zobj->properties, &ce->default_properties, (copy_ctor_func_t) zval_add_ref,(void *) &temp, sizeof(zval *));
+    retval.handle = zend_objects_store_put(zobj, NULL, (zend_objects_free_object_storage_t)zend_objects_free_object, NULL TSRMLS_CC);
+    retval.handlers = object_handlers;
+    return retval;
+}
+
 int mapscript_extract_associative_array(HashTable *php, char **array)
 {
     zval **value;

Modified: trunk/mapserver/mapscript/php/style.c
===================================================================
--- trunk/mapserver/mapscript/php/style.c	2010-06-25 17:57:06 UTC (rev 10252)
+++ trunk/mapserver/mapscript/php/style.c	2010-06-25 20:11:51 UTC (rev 10253)
@@ -32,6 +32,7 @@
 #include "php_mapscript.h"
 
 zend_class_entry *mapscript_ce_style;
+zend_object_handlers mapscript_style_object_handlers;
 
 ZEND_BEGIN_ARG_INFO_EX(style___construct_args, 0, 0, 1)
   ZEND_ARG_OBJ_INFO(0, class, classObj, 0)
@@ -142,17 +143,6 @@
     {
         mapscript_throw_exception("Property '%s' does not exist in this object." TSRMLS_CC, property);
     }
-/*
-    MAKE_STD_ZVAL(php_style->color);
-    mapscript_create_color(&(style->color), return_value, php_style->color TSRMLS_CC);
-
-    MAKE_STD_ZVAL(php_style->outlinecolor);
-    mapscript_create_color(&(style->outlinecolor), return_value, php_style->outlinecolor TSRMLS_CC);
-
-    MAKE_STD_ZVAL(php_style->backgroundcolor);
-    mapscript_create_color(&(style->backgroundcolor), return_value, php_style->backgroundcolor TSRMLS_CC);
-
- */
 }
 
 PHP_METHOD(styleObj, __set)
@@ -237,31 +227,6 @@
          
 }
 
-PHP_METHOD(styleObj, __clone)
-{
-    zval *zobj = getThis();
-    styleObj *style;
-    php_style_object *php_style;
-
-    PHP_MAPSCRIPT_ERROR_HANDLING(TRUE);
-    if (zend_parse_parameters_none() == FAILURE) {
-        PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-        return;
-    }
-    PHP_MAPSCRIPT_RESTORE_ERRORS(TRUE);
-
-    php_style = (php_style_object *) zend_object_store_get_object(zobj TSRMLS_CC);
-    
-    if ((style = styleObj_clone(php_style->style)) == NULL)
-    {
-        mapscript_report_mapserver_error(E_WARNING TSRMLS_CC);
-        RETURN_NULL();
-    }
-    
-    /* setting the class as parent .... but should not */
-    mapscript_create_style(style, php_style->parent, return_value TSRMLS_CC);
-}
-
 /* {{{ proto int style.updateFromString(string snippet)
    Update a style from a string snippet.  Returns MS_SUCCESS/MS_FAILURE */
 PHP_METHOD(styleObj, updateFromString)
@@ -439,8 +404,6 @@
     PHP_ME(styleObj, __get, style___get_args, ZEND_ACC_PUBLIC)
     PHP_ME(styleObj, __set, style___set_args, ZEND_ACC_PUBLIC)
     PHP_MALIAS(styleObj, set, __set, NULL, ZEND_ACC_PUBLIC)
-    PHP_ME(styleObj, __clone, NULL, ZEND_ACC_PUBLIC)
-    PHP_MALIAS(styleObj, clone, __clone, NULL, ZEND_ACC_PUBLIC)
     PHP_ME(styleObj, updateFromString, style_updateFromString_args, ZEND_ACC_PUBLIC)
     PHP_ME(styleObj, setBinding, style_setBinding_args, ZEND_ACC_PUBLIC)
     PHP_ME(styleObj, getBinding, style_getBinding_args, ZEND_ACC_PUBLIC)
@@ -477,16 +440,20 @@
     efree(object);
 }
 
-static zend_object_value mapscript_style_object_new(zend_class_entry *ce TSRMLS_DC)
+static zend_object_value mapscript_style_object_new_ex(zend_class_entry *ce, php_style_object **ptr TSRMLS_DC)
 {
     zend_object_value retval;
     php_style_object *php_style;
 
     MAPSCRIPT_ALLOC_OBJECT(php_style, php_style_object);
 
-    retval = mapscript_object_new(&php_style->std, ce,
-                                  &mapscript_style_object_destroy TSRMLS_CC);
+    retval = mapscript_object_new_ex(&php_style->std, ce,
+                                     &mapscript_style_object_destroy,
+                                     &mapscript_style_object_handlers TSRMLS_CC);
 
+    if (ptr)
+        *ptr = php_style;
+
     MAPSCRIPT_INIT_PARENT(php_style->parent);
     php_style->color = NULL;
     php_style->outlinecolor = NULL;
@@ -495,10 +462,33 @@
     return retval;
 }
 
+static zend_object_value mapscript_style_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+    return mapscript_style_object_new_ex(ce, NULL TSRMLS_CC);
+}
+
+static zend_object_value mapscript_style_object_clone(zval *zobj TSRMLS_DC)
+{
+    php_style_object *php_style_old, *php_style_new;
+    zend_object_value new_ov;
+
+    php_style_old = (php_style_object *) zend_object_store_get_object(zobj TSRMLS_CC);
+
+    new_ov = mapscript_style_object_new_ex(mapscript_ce_style, &php_style_new TSRMLS_CC); 
+    zend_objects_clone_members(&php_style_new->std, new_ov, &php_style_old->std, Z_OBJ_HANDLE_P(zobj) TSRMLS_CC);
+
+    php_style_new->style = styleObj_clone(php_style_old->style);
+
+    return new_ov;
+}
+
 PHP_MINIT_FUNCTION(style)
 {
     zend_class_entry ce;
 
+    memcpy(&mapscript_style_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+    mapscript_style_object_handlers.clone_obj = mapscript_style_object_clone;
+
     MAPSCRIPT_REGISTER_CLASS("styleObj", 
                              style_functions,
                              mapscript_ce_style,



More information about the mapserver-commits mailing list