You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by sl...@apache.org on 2007/02/22 13:21:06 UTC

svn commit: r510473 - in /incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php: PHPServiceWrapper.cpp PHPServiceWrapper.h php_sca.h sca.cpp sca.h

Author: slaws
Date: Thu Feb 22 04:21:05 2007
New Revision: 510473

URL: http://svn.apache.org/viewvc?view=rev&rev=510473
Log:
TUSCANY-1133
Make the PHP extension work on windows

Modified:
    incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.cpp
    incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.h
    incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/php_sca.h
    incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.cpp
    incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.h

Modified: incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.cpp?view=diff&rev=510473&r1=510472&r2=510473
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.cpp (original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.cpp Thu Feb 22 04:21:05 2007
@@ -34,7 +34,8 @@
 #include <iostream>
 #include <sstream>
 
-#include <php_embed.h>
+#include <sapi/embed/php_embed.h>
+
 
 #include "tuscany/sca/php/PHPServiceWrapper.h"
 #include "tuscany/sca/php/PHPServiceProxy.h"
@@ -50,6 +51,7 @@
 
 
 #include "sca.h"
+#include "php_sdo_int.h"
 
 using namespace std;
 using namespace tuscany::sca::model;
@@ -63,22 +65,22 @@
       
             int embedSAPIInitializeCount = 0;
             
-            // Global callbacks used by the PHP engine
+			// Global callbacks used by the PHP engine
 
-            // Callback for SAPI error
-            void php_sapi_error(int type, const char *error_msg, ...) 
-            {
-                logwarning("PHP sapi error: %s", error_msg); 
-                //TODO - do something sensible with this output
-                zend_bailout(); 
-            }
-
-            // Callback for log messages
-            void php_log_message(char *message) 
-            {
-                loginfo("PHP log: %s", message); 
-                //TODO - do something sensible with this output                
-            }
+			// Callback for SAPI error
+			void php_sapi_error(int type, const char *error_msg, ...) 
+			{
+				logwarning("PHP sapi error: %s", error_msg); 
+				//TODO - do something sensible with this output
+				zend_bailout(); 
+			}
+
+			// Callback for log messages
+			void php_log_message(char *message) 
+			{
+				loginfo("PHP log: %s", message); 
+				//TODO - do something sensible with this output                
+			}
 
             // Callback for unbuffered writes (echo, print etc.)
             int php_ub_write(const char *str, unsigned int str_length TSRMLS_DC)
@@ -146,153 +148,312 @@
             // ======================================================================
             // invoke: wrapper call to service with setting the component context
             // ======================================================================
-            void PHPServiceWrapper::invoke(Operation& operation)
-            {
-                logentry();
+			void PHPServiceWrapper::invoke(Operation& operation)
+			{
+				zval z_func_name, retval, z_component_name, z_class_name, z_operation_name, z_arg_array;
+				zval *params[5];
+
+				logentry();
 string temp;
 cout << "Press Key\n";
 cin >> temp;
-                // set the current component in the SCA runtime
-                // so that other things can get at it thorugh this
-                // static class
-                SCARuntime* runtime = SCARuntime::getCurrentRuntime();
-                runtime->setCurrentComponent(component);
-
-                try
-                {                
-                    // get the component type information
-                    PHPImplementation* impl = (PHPImplementation*)component->getType();
-                    
-                    // get the directory containing the component type
-                    const string &compositeDir = impl->getComposite()->getRoot();
-
-                    // get some useful information that will be used later
-                    // when we construct the wrapper script 
-                    string componentName = component->getName();
-                    string className     = impl->getClass();
-                    string operationName = operation.getName();
-                    loginfo("Component %s class %s operation: %s", 
-                            componentName.c_str(),
-                            className.c_str(),
-                            operationName.c_str());          
-                
-                    // load the PHP logging and error callback methods
-                    php_embed_module.log_message = php_log_message;
-                    php_embed_module.ub_write    = php_ub_write;
-                    php_embed_module.sapi_error  = php_sapi_error;
-
-// would normally use the following macro before the embeded call
-// but we need to take account of multiple nested calls
-// and ZTS threading safety is not behaving itself
-//                  PHP_EMBED_START_BLOCK(/* argc */ 0, /* argv */ NULL)
-// ===================================================================== 
-                    {   
+
+				// set the current component in the SCA runtime
+				// so that other things can get at it thorugh this
+				// static class
+				SCARuntime* runtime = SCARuntime::getCurrentRuntime();
+				runtime->setCurrentComponent(component);
+
+				try
+				{                
+					// get the component type information
+					PHPImplementation* impl = (PHPImplementation*)component->getType();
+
+					// get the directory containing the component type
+					const string &compositeDir = impl->getComposite()->getRoot();
+
+					// get some useful information that will be used later
+					// when we construct the wrapper script 
+					string componentName = component->getName();
+					string className     = impl->getClass();
+					string operationName = operation.getName();
+					loginfo("Component %s class %s operation: %s", 
+						componentName.c_str(),
+						className.c_str(),
+						operationName.c_str());          
+
+					// load the PHP logging and error callback methods
+					php_embed_module.log_message = php_log_message;
+					php_embed_module.ub_write    = php_ub_write;
+					php_embed_module.sapi_error  = php_sapi_error;
+
+					// would normally use the following macro before the embeded call
+					// but we need to take account of multiple nested calls
+					// and ZTS threading safety is not behaving itself
+					//                  PHP_EMBED_START_BLOCK(/* argc */ 0, /* argv */ NULL)
+					// ===================================================================== 
+					{   
 #ifdef ZTS
-                    static void ***tsrm_ls;
-                    loginfo("ZTS enabled");
+						static void ***tsrm_ls;
+						loginfo("ZTS enabled");
 #endif
-                    if ( embedSAPIInitializeCount == 0 )
-                    {
-                        embedSAPIInitializeCount = 1;
-                                           
-                        php_embed_init(0, NULL PTSRMLS_CC); 
-
-                        // load up the sca module that provides the interface between
-                        // C++ and user space PHP
-                        zend_startup_module(&sca_module_entry);                        
-                    }
-                    else
-                    {
-                        embedSAPIInitializeCount++;
-                    }
-// should really be doing a first try but don't want to 
-// do it more than once                    
-//                    zend_first_try {
-                    zend_try {
-// =====================================================================
-                        // set error handler
-                        zend_error_cb = php_error_cb;
-                    
-                        // create an SCA_Tuscany object and give it the operation
-                        // object so that the script in user space PHP can get 
-                        // at the arguments and return a response. 
-                        long object_id = createSCATuscanyObject(operation TSRMLS_CC);
-                        std::stringstream string_stream;
-                        string object_id_str;
-                        string_stream << object_id;
-                        string_stream >> object_id_str;  
-                    
-                        // create the wrapper script that gets everything up and
-                        // going in userspace PHP
-                        string script;
-                        script += "include 'SCA/SCA.php';";                   
-                        script += "include 'SCA/SCA_TuscanyWrapper.php';";
-                        script += "$wrapper = new SCA_TuscanyWrapper(";
-                        script += object_id_str;
-                        script += ", '" + componentName + "', "; 
-
-                        if( &className != NULL && className.size() > 0)
-                        {           
-                            script += " '" + className + "'";
-                        }
-                        else
-                        {
-                            script += "null";
-                        }
-                  
-                        script += ", '" + operationName + "');";
-                        script += "include '" + compositeDir + "/" + impl->getModule() + ".php';";                         
-                        script += "$response = $wrapper->invoke();";
-                        script += "echo $response;"; 
-                        loginfo("Script: %s", script.c_str());
-                    
-						// call the dynamically created script
-						// I'm not trapping the return value here 
-						// as it seems to prevent any kind of output
-						// being produced. Needs investigation.
-                        //zval retval;
-                        zend_eval_string((char *) script.c_str(), 
-                                         NULL,//&retval, 
-                                         "PHP Component" TSRMLS_CC);
-						
-                        // As retval is not behaving post processing is commented out for now 
-                        //convert_to_string(&retval);
-                        //loginfo("Script returned1: %s", Z_STRVAL(retval));
-                        //zval_dtor(&retval);
-    
-// would normally use the following macro after the embeded call
-// but we need to take account of multiple nested calls                    
-//                  PHP_EMBED_END_BLOCK()
-// =====================================================================
-                    } zend_catch { 
-                       /* int exit_status = EG(exit_status); */ 
-                    } 
-                    zend_end_try(); 
-                    
-                    if ( embedSAPIInitializeCount == 1 )
-                    {
-                        php_embed_shutdown(TSRMLS_C); 
-                        loginfo("Engine shutdown");                        
-                    }
-                    else
-                    {
-                        embedSAPIInitializeCount--;
-                    }
-                    }
-// =====================================================================                    
-      
-                    // The response is set back into 'operation' by the
-                    // PECL extension code when the PHP user sapce wrapper
-                    // calls setResponse()
-                }
-                catch (...)
-                {
-                    runtime->unsetCurrentComponent();
-                    throw;
-                }
-                runtime->unsetCurrentComponent();
+						printf("tsrm_ls = %x, embedSAPIInitializeCount = %d\n",
+							tsrm_ls, embedSAPIInitializeCount);
 
-            }
+						if ( embedSAPIInitializeCount == 0 )
+						{
+							embedSAPIInitializeCount = 1;
+
+							php_embed_init(0, NULL PTSRMLS_CC); 
+
+							// load up the sca module that provides the interface between
+							// C++ and user space PHP
+							zend_first_try {
+								zend_startup_module(&sca_module_entry);     
+							} zend_end_try();
+						} else {
+							embedSAPIInitializeCount++;
+						}
+
+						zend_try {
+							// set error handler
+							zend_error_cb = php_error_cb;
+
+							/*
+							* Open the include files 
+						 */
+							//						includeFile("SCA/SCA.php" TSRMLS_CC);				
+							//						includeFile("SCA/Bindings/tuscany/SCA_TuscanyWrapper.php" TSRMLS_CC);
+
+							/* Instantiate a SCA_Tuscany (mediator) object, and 
+							 * set its operation property
+							 */
+							zval z_tuscany_mediator;
+							zval *pz_mediator = &z_tuscany_mediator;
+							INIT_ZVAL(z_tuscany_mediator);
+							zend_class_entry *ce_mediator = 
+								zend_fetch_class("SCA_Tuscany", sizeof("SCA_Tuscany") - 1, 0 TSRMLS_CC);
+							object_init_ex(pz_mediator, ce_mediator);
+							z_tuscany_mediator.value.obj.handlers->add_ref(pz_mediator TSRMLS_CC);
+
+							zend_update_property_long(ce_mediator, pz_mediator, 
+								"operation", sizeof("operation") - 1, (long)&operation TSRMLS_CC);
+
+							/* get the class info loaded up */
+							zend_eval_string("include 'SCA/SCA.php';", 
+								NULL, "Include SCA" TSRMLS_CC);
+							zend_eval_string("include 'SCA/Bindings/tuscany/SCA_TuscanyWrapper.php';", 
+								NULL, "Include SCA_TuscanyWrapper" TSRMLS_CC);
+
+							/* Now instantiate an SCA_TuscanyWrapper object and call its constructor */
+							zval z_tuscany_wrapper;
+							zval *pz_tuscany_wrapper = &z_tuscany_wrapper;
+							INIT_ZVAL(z_tuscany_wrapper);
+							object_init_ex(pz_tuscany_wrapper, 
+								zend_fetch_class("SCA_TuscanyWrapper", sizeof("SCA_TuscanyWrapper") - 1, 0 TSRMLS_CC));  
+							z_tuscany_wrapper.value.obj.handlers->add_ref(pz_tuscany_wrapper TSRMLS_CC);
+
+							ZVAL_STRING(&z_func_name, "__construct", 1);
+							ZVAL_STRING(&z_component_name, (char *)componentName.c_str(), 1);
+							if( &className != NULL && className.size() > 0) {           
+								ZVAL_STRING(&z_class_name, (char *)className.c_str(), 1);
+							} else {
+								ZVAL_NULL(&z_class_name);
+							}
+							ZVAL_STRING(&z_operation_name, (char *)operationName.c_str(), 1);
+
+							array_init(&z_arg_array);
+
+							// get the parameters from the operation structure
+							for(unsigned int i = 0; i < operation.getNParms(); i++) 
+							{
+								const Operation::Parameter& parm = operation.getParameter(i);
+								//printf("Arg %d type %d\n", i, parm.getType());
+								switch(parm.getType())
+								{
+								case Operation::BOOL: 
+									{
+										add_next_index_bool(&z_arg_array, (int)*(bool*)parm.getValue() );
+										break;
+									}
+								case Operation::SHORT: 
+								case Operation::USHORT: 
+								case Operation::LONG: 
+								case Operation::ULONG: 
+									{
+										add_next_index_long(&z_arg_array, *(long*)parm.getValue() );
+										break;
+									}
+								case Operation::FLOAT: 
+									{
+										add_next_index_double(&z_arg_array,( double)*(float*)parm.getValue() );
+										break;
+									}
+								case Operation::DOUBLE: 
+								case Operation::LONGDOUBLE: 
+									{
+										add_next_index_double(&z_arg_array, *(double*)parm.getValue() );
+										break;
+									}
+								case Operation::CHARS: 
+									{
+										add_next_index_string(&z_arg_array, *(char**)parm.getValue(), 1 );
+										break;
+									}
+								case Operation::STRING: 
+									{
+										add_next_index_string(&z_arg_array, (char*)(*(string*)parm.getValue()).c_str(), 1 );
+										break;
+									}    		
+								case Operation::DATAOBJECT: 
+									{
+										// convert the tuscany SDO into a PHP SDO
+
+										// create the object
+										zval *sdo;
+										ALLOC_INIT_ZVAL(sdo);
+										sdo_do_new(sdo, *(DataObjectPtr*)parm.getValue() TSRMLS_CC);
+
+										// add it to the arg array
+										add_next_index_zval(&z_arg_array, sdo);
+										break;
+									}
+								default: 
+									{
+										char *class_name;
+										char *space;
+										class_name = get_active_class_name(&space TSRMLS_CC);            
+										php_error(E_ERROR, 
+											"%s%s%s(): Argument type %d not supported",
+											class_name, 
+											space, 
+											get_active_function_name(TSRMLS_C), 
+											parm.getType());
+									}
+								}
+							}
+
+							params[0] = &z_tuscany_mediator;
+							params[1] = &z_component_name;
+							params[2] = &z_class_name;
+							params[3] = &z_operation_name;
+							params[4] = &z_arg_array;
+
+							call_user_function(EG(function_table), 
+								&pz_tuscany_wrapper, 
+								&z_func_name,
+								&retval,
+								5,
+								params TSRMLS_CC);
+
+							zval_dtor(&z_func_name);
+							zval_dtor(&z_component_name);
+							zval_dtor(&z_class_name);
+							zval_dtor(&z_operation_name);
+							zval_dtor(&z_arg_array);
+
+							string script;
+							script = "include '" + compositeDir + "/" + impl->getModule() + ".php';";  
+							loginfo("Script: %s", script.c_str());
+
+							// call the dynamically created script
+							// I'm not trapping the return value here 
+							// as it seems to prevent any kind of output
+							// being produced. Needs investigation.
+							//zval retval;
+							zend_eval_string((char *) script.c_str(), 
+								NULL, //&retval, 
+								"Include module" TSRMLS_CC);
+
+							/* Now call SCA_TuscanyWrapper->invoke() ... */
+							ZVAL_STRING(&z_func_name, "invoke", 1);
+							call_user_function(EG(function_table), 
+								&pz_tuscany_wrapper, &z_func_name, 
+								&retval, 0, NULL TSRMLS_CC);
+
+							zval_dtor(&z_func_name);
+
+							/* ... and copy the return value into the operation */
+							switch(Z_TYPE(retval))
+							{
+							case IS_NULL:
+								{
+									//printf("NULL response");
+									break;
+								}
+							case IS_BOOL: 
+								{
+									bool *newBool = new bool;
+									*newBool =  ZEND_TRUTH(Z_BVAL(retval));
+									operation.setReturnValue(newBool);
+									break;        
+								}
+							case IS_LONG: 
+								{
+									long *newLong = new long;
+									*newLong =  Z_LVAL(retval);
+									operation.setReturnValue(newLong);                            
+									break;
+								}
+							case IS_DOUBLE: 
+								{
+									//double *newDouble = new double;
+									float *newDouble = new float;                                
+									*newDouble =  (float)Z_DVAL(retval);
+									operation.setReturnValue(newDouble);                            
+									break;
+								}
+							case IS_STRING: 
+								{   
+									string newString (Z_STRVAL(retval));
+									operation.setReturnValue(&newString);
+									break;
+								}
+							case IS_OBJECT: 
+								{
+									// convert the PHP SDO into a Tuscany SDO
+									DataObjectPtr sdo = sdo_do_get(&retval TSRMLS_CC);
+									operation.setReturnValue(&sdo);  		    
+									break;
+								}
+							default: 
+								{
+									char *class_name;
+									char *space;
+									class_name = get_active_class_name(&space TSRMLS_CC);
+									php_error(E_ERROR, 
+										"%s%s%s(): Input argument type %d not supported on invoke",
+										class_name, 
+										space, 
+										get_active_function_name(TSRMLS_C), 
+										Z_TYPE(retval)); 
+								}                            
+							}
+						} zend_catch { 
+							/* int exit_status = EG(exit_status); */ 
+						} 
+						zend_end_try(); 
+
+						if ( embedSAPIInitializeCount == 1 )
+						{
+							php_embed_shutdown(TSRMLS_C); 
+							loginfo("Engine shutdown");                        
+						}
+						else
+						{
+							embedSAPIInitializeCount--;
+						}
+					}
+					// =====================================================================     
+				}
+				catch (...)
+				{
+					runtime->unsetCurrentComponent();
+					throw;
+				}
+				runtime->unsetCurrentComponent();
+
+			}
             
             // ======================================================================
             // getServiceWrapper: create a wrapper for the target ComponentService
@@ -314,6 +475,28 @@
                 
                 return serviceWrapper;
             }    
+
+			/* Not used at the moment
+			 * Can find the include file and execute it,
+			 * but doesn't make the contents available
+			 */
+			void PHPServiceWrapper::includeFile(char *includeFileName TSRMLS_DC) 
+			{
+                zend_file_handle include_file_handle;
+
+				include_file_handle.type = ZEND_HANDLE_FP;
+				include_file_handle.filename = includeFileName;
+				include_file_handle.opened_path = NULL;
+				include_file_handle.free_filename = 0;
+				if (!(include_file_handle.handle.fp = php_fopen_with_path(
+					  include_file_handle.filename, "rb", PG(include_path), 
+					  &include_file_handle.opened_path TSRMLS_CC))) {
+					php_error(E_ERROR,
+						"Could not open %s", include_file_handle.filename);
+					return;
+				}
+                php_execute_script(&include_file_handle TSRMLS_CC);
+			}
 
         } // End namespace php        
     } // End namespace sca

Modified: incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.h?view=diff&rev=510473&r1=510472&r2=510473
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.h (original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/PHPServiceWrapper.h Thu Feb 22 04:21:05 2007
@@ -29,6 +29,7 @@
 #include "tuscany/sca/model/Component.h"
 #include "tuscany/sca/model/Interface.h"
 
+#include <TSRM/tsrm.h>
 
 namespace tuscany
 {
@@ -82,6 +83,10 @@
     
             private:
 
+                /**
+                 * Utility method to execute a script on the include_path.     
+                 */
+                static void includeFile(char *includeFileName TSRMLS_DC);
 
                 /**
                  * The component to which this wrapper refers.

Modified: incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/php_sca.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/php_sca.h?view=diff&rev=510473&r1=510472&r2=510473
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/php_sca.h (original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/php_sca.h Thu Feb 22 04:21:05 2007
@@ -29,15 +29,15 @@
 #endif
 
 #ifdef HAVE_CONFIG_H
-//#include "config.h"
+#include "config.h"
 #endif
 
-#include <php.h>
+#include <main/php.h>
 
 #ifdef HAVE_SCA
 
-#include <php_ini.h>
-#include <SAPI.h>
+#include <main/php_ini.h>
+#include <main/SAPI.h>
 #include <ext/standard/info.h>
 #include <Zend/zend_extensions.h>
 #ifdef  __cplusplus
@@ -59,12 +59,10 @@
 
 PHP_MINIT_FUNCTION(sca);
 PHP_MSHUTDOWN_FUNCTION(sca);
-PHP_RINIT_FUNCTION(sca);
-PHP_RSHUTDOWN_FUNCTION(sca);
 PHP_MINFO_FUNCTION(sca);
 
 #ifdef ZTS
-#include "TSRM.h"
+#include "TSRM/TSRM.h"
 #endif
 
 #define FREE_RESOURCE(resource) zend_list_delete(Z_LVAL_P(resource))
@@ -81,18 +79,9 @@
 #define PROP_SET_STRINGL(name, s, l) zend_update_property_string(_this_ce, _this_zval, #name, strlen(#name), s, l TSRMLS_CC)
 
 
-PHP_METHOD(SCA_Tuscany, getSCATuscanyObject);
-#if (PHP_MAJOR_VERSION >= 5)
-ZEND_BEGIN_ARG_INFO_EX(SCA_Tuscany_getSCATuscanyObject_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
-  ZEND_ARG_INFO(0, object_id)
-ZEND_END_ARG_INFO()
-#else /* PHP 4.x */
-#define SCA_Tuscany_getSCATuscanyObject_args NULL
-#endif
-
 PHP_METHOD(SCA_Tuscany, __construct);
 #if (PHP_MAJOR_VERSION >= 5)
-ZEND_BEGIN_ARG_INFO_EX(SCA_Tuscany____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 0)
+ZEND_BEGIN_ARG_INFO_EX(SCA_Tuscany____construct_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
 ZEND_END_ARG_INFO()
 #else /* PHP 4.x */
 #define SCA_Tuscany____construct_args NULL
@@ -121,16 +110,6 @@
 #else /* PHP 4.x */
 #define SCA_Tuscany__getArgArray_args NULL
 #endif
-
-PHP_METHOD(SCA_Tuscany, setResponse);
-#if (PHP_MAJOR_VERSION >= 5)
-ZEND_BEGIN_ARG_INFO_EX(SCA_Tuscany__setResponse_args, ZEND_SEND_BY_VAL, ZEND_RETURN_VALUE, 1)
-  ZEND_ARG_INFO(0, response)
-ZEND_END_ARG_INFO()
-#else /* PHP 4.x */
-#define SCA_Tuscany__setResponse_args NULL
-#endif
-
 
 #ifdef  __cplusplus
 } // extern "C" 

Modified: incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.cpp
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.cpp?view=diff&rev=510473&r1=510472&r2=510473
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.cpp (original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.cpp Thu Feb 22 04:21:05 2007
@@ -52,53 +52,27 @@
 /* {{{ Class SCA_Tuscany */
 
 static zend_class_entry * SCA_Tuscany_ce_ptr = NULL;
-static zend_object_handlers SCA_Tuscany_object_handlers;
 
 /* {{{ Methods */
 
-/* {{{ proto object getSCATuscanyObject(long object_id)
-  return an SCA_Tuscany object based on the object_id
-  the object_id 
-  */
-PHP_METHOD(SCA_Tuscany, getSCATuscanyObject)
-{
-	long  object_id = 0;
-
-    printf("IN getSCATuscanyObject\n");	
-    
-	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "l", &object_id) == FAILURE) 
-	{
-		return;
-	}
-	
-    Z_TYPE_P(return_value) = IS_OBJECT;
-    
-	// poke the object id that has been passed in into the zval
-	// to fake the creation of the object
-    return_value->value.obj.handle = (zend_object_handle) object_id;
-	return_value->value.obj.handlers = &SCA_Tuscany_object_handlers;
-	return_value->value.obj.handlers->add_ref(return_value TSRMLS_CC);
-}
-/* }}} getSCATuscanyObject */
-
 /* {{{ proto void __construct(int operation_handle)
    */
 PHP_METHOD(SCA_Tuscany, __construct)
 {
-	zend_class_entry * _this_ce;
-	zval * _this_zval;
+	zval *_this_zval = getThis();
+	zend_class_entry *_this_ce = Z_OBJCE_P(_this_zval);
+
 	long operation_handle = 0;
-	
-    printf("IN constructor\n");	
 
 	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &operation_handle) == FAILURE) {
 		return;
 	}
 
-	_this_zval = getThis();
-	_this_ce = Z_OBJCE_P(_this_zval);
+	if (!operation_handle) {
+		php_error(E_ERROR, "SCA_Tuscany ctor called with NULL operation");
+	}
 
-	PROP_SET_LONG("operation", operation_handle);
+	PROP_SET_LONG(operation, operation_handle);
 }
 /* }}} __construct */
 
@@ -106,8 +80,7 @@
   Invoke a Tuscany component */
 PHP_METHOD(SCA_Tuscany, invoke)
 {
-	zend_class_entry * _this_ce;
-	zval * _this_zval = NULL;
+	//zval * _this_zval = NULL;
 	const char * component_name = NULL;
 	int component_name_len = 0;
 	const char * reference_name = NULL;
@@ -117,9 +90,11 @@
 	zval * arguments = NULL;
 	HashTable * arguments_hash = NULL;
 
-    printf("IN invoke\n");	
-
-	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Osssa/", &_this_zval, SCA_Tuscany_ce_ptr, &component_name, &component_name_len, &reference_name, &reference_name_len, &method_name, &method_name_len, &arguments) == FAILURE) {
+	if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sssa/", 
+		&component_name, &component_name_len, 
+		&reference_name, &reference_name_len,
+		&method_name, &method_name_len, 
+		&arguments) == FAILURE) {
 		return;
 	}
 
@@ -129,8 +104,7 @@
     SCARuntime* runtime = SCARuntime::getCurrentRuntime();
     Component* component = runtime->getCurrentComponent();    
     Reference* ref = component->findReference(reference_name);
-    if(!ref)
-    {
+    if(!ref) {
     	char *class_name;
     	char *space;
 	    class_name = get_active_class_name(&space TSRMLS_CC);
@@ -178,13 +152,14 @@
         {
             case IS_NULL:
             {
-                printf("NULL agument");
+                //printf("NULL argument");
+				break;
             }
             case IS_BOOL: 
             {
                 convert_to_boolean(*data);
                 bool *newBool = new bool;
-                *newBool =  Z_BVAL_PP(data);
+                *newBool =  ZEND_TRUTH(Z_BVAL_PP(data));
                 operation.addParameter(newBool);
                 break;
             }
@@ -273,6 +248,7 @@
         case Operation::VOID_TYPE: 
         {
             // do nothing
+			break;
         }    	
     	case Operation::DATAOBJECT: 
     	{
@@ -297,37 +273,27 @@
 }
 /* }}} invoke */
 
-
-
 /* {{{ proto array getArgArray()
   return the arguments for the operation as an array */
 PHP_METHOD(SCA_Tuscany, getArgArray)
 {
-	zend_class_entry * _this_ce;
-	zval * _this_zval = NULL;
+	zval * _this_zval = getThis();
+	zend_class_entry *_this_ce = Z_OBJCE_P(_this_zval);
 
-	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, SCA_Tuscany_ce_ptr) == FAILURE) {
-		return;
+	if (ZEND_NUM_ARGS() > 0) {
+		WRONG_PARAM_COUNT;
 	}
 
-	_this_zval = getThis();
-	_this_ce = Z_OBJCE_P(_this_zval);
-
-	array_init(return_value);
-
-    printf("IN getArgArray\n");
-
     array_init(return_value);
     
-    // get the operaton object from the object properties
-    
-    Operation *operation = (Operation *)(long)PROP_GET_LONG("operation");
+    // get the operation object from the object properties    
+	Operation *operation = (Operation *)PROP_GET_LONG(operation);
 
     // get the parameters from the operation structure
     for(unsigned int i = 0; i < operation->getNParms(); i++) 
 	{
         const Operation::Parameter& parm = operation->getParameter(i);
-        printf("Arg %d type %d\n", i, parm.getType());
+        //printf("Arg %d type %d\n", i, parm.getType());
         switch(parm.getType())
     	{
     	    case Operation::BOOL: 
@@ -369,7 +335,8 @@
     		    // convert the tuscany SDO into a PHP SDO
     		    
     		    // create the object
-    		    zval * sdo;
+    		    zval *sdo;
+				ALLOC_INIT_ZVAL(sdo);
                 sdo_do_new(sdo, *(DataObjectPtr*)parm.getValue() TSRMLS_CC);
                 
                 // add it to the arg array
@@ -393,188 +360,10 @@
 }
 /* }}} getArgArray */
 
-
-/* {{{ proto void setResponse(mixed response)
-  set the results from the operation */
-PHP_METHOD(SCA_Tuscany, setResponse)
-{
-	zend_class_entry * _this_ce;
-	zval * _this_zval = NULL;
-	zval * response = NULL;
-
-	if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Oz/", &_this_zval, SCA_Tuscany_ce_ptr, &response) == FAILURE) {
-		return;
-	}
-
-	_this_zval = getThis();
-	_this_ce = Z_OBJCE_P(_this_zval);
-
-    printf("IN setResponse\n");	
-
-    // get the operaton object from the object properties
-    Operation *operation = (Operation *)(long)PROP_GET_LONG("operation");
-
-    switch(Z_TYPE_P(response))
-    {
-        case IS_NULL:
-        {
-            printf("NULL response");
-        }
-        case IS_BOOL: 
-        {
-            bool *newBool = new bool;
-            *newBool =  Z_BVAL_P(response);
-            operation->setReturnValue(newBool);
-            break;        
-        }
-        case IS_LONG: 
-        {
-            long *newLong = new long;
-            *newLong =  Z_LVAL_P(response);
-            operation->setReturnValue(newLong);                            
-            break;
-        }
-        case IS_DOUBLE: 
-        {
-            //double *newDouble = new double;
-            float *newDouble = new float;                                
-            *newDouble =  (float)Z_DVAL_P(response);
-            operation->setReturnValue(newDouble);                            
-            break;
-        }
-        case IS_STRING: 
-        {   
-            string newString (Z_STRVAL_P(response));
-            operation->setReturnValue(&newString);
-            break;
-        }
-        case IS_OBJECT: 
-  		{
-  		    // convert the PHP SDO into a Tuscany SDO
-    		DataObjectPtr sdo = sdo_do_get(response TSRMLS_CC);
-    		operation->setReturnValue(&sdo);  		    
-   			break;
-   		}
-        default: 
-        {
- 	        char *class_name;
-  	        char *space;
-            class_name = get_active_class_name(&space TSRMLS_CC);
-	        php_error(E_ERROR, 
-	                  "%s%s%s(): Input argument type %d not supported on invoke",
-			          class_name, 
-			          space, 
-			          get_active_function_name(TSRMLS_C), 
-			          Z_TYPE_P(response)); 
-        }                            
-  
-    }
-
-/*
-The following code interprets the return based on what the operation
-structure is expecting. In the case of a call from another dynamic script 
-component this will be not be set correct so I have changed the code above 
-to rely on the type returned from the script instead. Hanging on to the
-following switch just in case  
-    switch(operation->getReturnType())
-    {
-        case Operation::BOOL: 
-        {
-            convert_to_boolean(response);
-            *(bool*)operation->getReturnValue() = Z_BVAL_P(response);
-            break;
-        }
-        case Operation::SHORT: 
-        {
-            convert_to_long(response);
-            *(short*)operation->getReturnValue() = (short) Z_LVAL_P(response);
-            break;
-        }
-        case Operation::LONG: 
-        {
-            convert_to_long(response);
-            *(long*)operation->getReturnValue() = (long) Z_LVAL_P(response);                                
-            break;
-        }
-        case Operation::USHORT: 
-        {
-            convert_to_long(response);
-            *(unsigned short*)operation->getReturnValue() = (unsigned short) Z_LVAL_P(response);
-            break;
-        }
-        case Operation::ULONG: 
-        {
-            convert_to_long(response);
-            *(unsigned long*)operation->getReturnValue() = (unsigned long) Z_LVAL_P(response);
-            break;
-        }
-        case Operation::FLOAT: 
-        {
-            convert_to_double(response);                           
-            *(float*)operation->getReturnValue() = (float) Z_DVAL_P(response);
-            break;
-        }
-        case Operation::DOUBLE: 
-        {
-            convert_to_double(response);
-            *(double*)operation->getReturnValue() = (double) Z_DVAL_P(response);
-            break;
-        }
-        case Operation::LONGDOUBLE: 
-        {
-            convert_to_double(response);
-            *(long double*)operation->getReturnValue() = (long double) Z_DVAL_P(response);
-            break;
-        }
-        case Operation::CHARS: 
-        {
-            convert_to_string(response);
-            // need to copy the string out of PHP memory into SCA memory
-            char * strCopy = strdup((char *)Z_STRVAL_P(response));
-            *(char**)operation->getReturnValue() = strCopy;
-            break;
-        }
-        case Operation::STRING: 
-        {
-            convert_to_string(response);   
-            string reponseString ( Z_STRVAL_P(response) );                        
-            *(string*)operation->getReturnValue() = reponseString;
-            break;
-        }
-        case Operation::VOID_TYPE: 
-        {
-            // do nothing
-        }
-    	case Operation::DATAOBJECT: 
-    	{
-    		// need to convert the PHP SDO into a Tuscanu SDO?
-    		printf("SDOs are not supported yet");
-    		break;
-    	}
-        default: 
-        {
-    	    char *class_name;
-    	    char *space;
-	        class_name = get_active_class_name(&space TSRMLS_CC);
-		    php_error(E_ERROR, 
-		          "%s%s%s(): Response type %d not supported",
-				  class_name, 
-				  space, 
-				  get_active_function_name(TSRMLS_C), 
-				  operation->getReturnType());
-        }                            
-    }
-*/
-}
-/* }}} setResponse */
-
-
 static zend_function_entry SCA_Tuscany_methods[] = {
-	PHP_ME(SCA_Tuscany, getSCATuscanyObject, SCA_Tuscany_getSCATuscanyObject_args, /**/ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
-	PHP_ME(SCA_Tuscany, __construct, NULL, /**/ZEND_ACC_PUBLIC)
-	PHP_ME(SCA_Tuscany, invoke, SCA_Tuscany__invoke_args, /**/ZEND_ACC_PUBLIC)
-	PHP_ME(SCA_Tuscany, getArgArray, NULL, /**/ZEND_ACC_PUBLIC)
-	PHP_ME(SCA_Tuscany, setResponse, SCA_Tuscany__setResponse_args, /**/ZEND_ACC_PUBLIC)
+	PHP_ME(SCA_Tuscany, __construct, SCA_Tuscany____construct_args, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
+	PHP_ME(SCA_Tuscany, invoke, SCA_Tuscany__invoke_args, ZEND_ACC_PUBLIC | ZEND_ACC_STATIC)
+	PHP_ME(SCA_Tuscany, getArgArray, SCA_Tuscany__getArgArray_args, ZEND_ACC_PUBLIC)
 	{ NULL, NULL, NULL }
 };
 
@@ -589,9 +378,9 @@
 
 	/* {{{ Property registration */
 
-	zend_declare_property_long(SCA_Tuscany_ce_ptr, 
-		"operation", 9, -1, 
-		ZEND_ACC_PRIVATE TSRMLS_CC);
+	zend_declare_property_null(SCA_Tuscany_ce_ptr, 
+		"operation", sizeof("operation") -1,
+		ZEND_ACC_PUBLIC TSRMLS_CC);
 
 	/* }}} Property registration */
 
@@ -630,8 +419,8 @@
 	sca_functions,
 	PHP_MINIT(sca),     /* Replace with NULL if there is nothing to do at php startup   */ 
 	PHP_MSHUTDOWN(sca), /* Replace with NULL if there is nothing to do at php shutdown  */
-	PHP_RINIT(sca),     /* Replace with NULL if there is nothing to do at request start */
-	PHP_RSHUTDOWN(sca), /* Replace with NULL if there is nothing to do at request end   */
+	NULL,               /* Replace with NULL if there is nothing to do at request start */
+	NULL,               /* Replace with NULL if there is nothing to do at request end   */
 	PHP_MINFO(sca),
 	"0.0.1", 
 	STANDARD_MODULE_PROPERTIES
@@ -648,9 +437,29 @@
 /* {{{ PHP_MINIT_FUNCTION */
 PHP_MINIT_FUNCTION(sca)
 {
-	class_init_SCA_Tuscany(TSRMLS_C);
+	/* We use the SDO extension for reference because it is a prereq */
+	zend_module_entry *req_module_entry;
+	char *req_module_name = "libxml";
+	if (zend_hash_find(&module_registry, req_module_name, strlen(req_module_name)+1, (void**)&req_module_entry) == SUCCESS) {
+		if (req_module_entry->zend_debug != ZEND_DEBUG ||
+		       req_module_entry->zts != USING_ZTS ||
+		       req_module_entry->zend_api != ZEND_MODULE_API_NO) {
+			php_error(E_ERROR, 
+				"Cannot initialize module sca.\nModule sca compiled with module API=%d, debug=%d, thread-safety=%d\nModule %n compiled with module API=%d, debug=%d, thread-safety=%d\nThese options need to match",
+				req_module_name,
+				ZEND_MODULE_API_NO, ZEND_DEBUG, USING_ZTS,
+				req_module_entry->zend_api, req_module_entry->zend_debug, req_module_entry->zts);
+			return FAILURE;
+		}
+	} else {
+		/* The dependency checker should already have found this, but to be on the safe side ... */
+		php_error (E_ERROR, 
+			"Cannot load module sca because required module %n is not loaded", 
+			req_module_name);
+		return FAILURE;
+	}
 
-	memcpy(&SCA_Tuscany_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+	class_init_SCA_Tuscany(TSRMLS_C);
 
 	return SUCCESS;
 }
@@ -660,34 +469,20 @@
 /* {{{ PHP_MSHUTDOWN_FUNCTION */
 PHP_MSHUTDOWN_FUNCTION(sca)
 {
-
-	/* add your stuff here */
+	/*
+	 * There is some corruption going on at shutdown.
+	 * The following hack eliminates the symptom, but there's probably still
+	 * an underlying problem.
+	 */
+	free(SCA_Tuscany_ce_ptr->name);
+	SCA_Tuscany_ce_ptr->name = NULL;
+	destroy_zend_class(&SCA_Tuscany_ce_ptr);
+	SCA_Tuscany_ce_ptr = NULL;
 
 	return SUCCESS;
 }
 /* }}} */
 
-
-/* {{{ PHP_RINIT_FUNCTION */
-PHP_RINIT_FUNCTION(sca)
-{
-	/* add your stuff here */
-
-	return SUCCESS;
-}
-/* }}} */
-
-
-/* {{{ PHP_RSHUTDOWN_FUNCTION */
-PHP_RSHUTDOWN_FUNCTION(sca)
-{
-	/* add your stuff here */
-
-	return SUCCESS;
-}
-/* }}} */
-
-
 /* {{{ PHP_MINFO_FUNCTION */
 PHP_MINFO_FUNCTION(sca)
 {
@@ -708,44 +503,7 @@
 
 /* Other functions not directly related to implementing the 
    SCA_Tuscany extension */
-  
-long createSCATuscanyObject(Operation& operation TSRMLS_DC) 
-{
-    printf("IN createSCATuscanyObject\n");	
-    
-    // create an object of type SCA_Tuscany
-    zval *_this_zval;
-    MAKE_STD_ZVAL(_this_zval);
-    Z_TYPE_P(_this_zval) = IS_OBJECT;
-    if ( object_init_ex(_this_zval, SCA_Tuscany_ce_ptr) == FAILURE) {
-    	char *class_name;
-    	char *space;
-	    class_name = get_active_class_name(&space TSRMLS_CC);
-		php_error(E_ERROR, 
-		          "%s%s%s(): internal error (%i) - failed to instantiate SCA_Tuscany object",
-				  class_name, 
-				  space, 
-				  get_active_function_name(TSRMLS_C), 
-				  __LINE__);
-	    return 0;
-    }
-    
-  	zend_class_entry * _this_ce;
-    _this_ce = Z_OBJCE_P(_this_zval);
-    	
-    // Set the address of the C++ operation object as a four byte value
-    // in the operation property
-   	PROP_SET_LONG("operation", (long) &operation);
-   	
-   	// get the four byte value of the address of the object
-   	// we have just created and return it
-    long object_id = (long) Z_OBJ_HANDLE_P(_this_zval);  
-    
-    printf("IN object_id = %d\n", object_id);
-    
-    return object_id;
-}
-
+ 
 #endif /* HAVE_SCA */
 
 

Modified: incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.h
URL: http://svn.apache.org/viewvc/incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.h?view=diff&rev=510473&r1=510472&r2=510473
==============================================================================
--- incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.h (original)
+++ incubator/tuscany/cpp/sca/runtime/extensions/php/src/tuscany/sca/php/sca.h Thu Feb 22 04:21:05 2007
@@ -24,17 +24,11 @@
 
 #include "tuscany/sca/core/Operation.h"
 
-#include <php.h>
+#include <main/php.h>
 
 #ifdef  __cplusplus
 extern "C" {
 #endif
-
-// creates an instance of SCA_Tuscany and popoulates
-// the operation property. It returns the four byte
-// representation of the object id from the objects
-// zval. 
-long createSCATuscanyObject(tuscany::sca::Operation& operation TSRMLS_DC);
 
 extern zend_module_entry sca_module_entry;
 



---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org