diff options
-rw-r--r-- | include/environment.h (renamed from include/request.h) | 50 | ||||
-rw-r--r-- | include/extension.h | 55 | ||||
-rw-r--r-- | include/function.h | 4 | ||||
-rw-r--r-- | phpcpp.h | 2 | ||||
-rw-r--r-- | src/extension.cpp | 29 | ||||
-rw-r--r-- | src/function.cpp | 4 | ||||
-rw-r--r-- | src/includes.h | 2 | ||||
-rw-r--r-- | src/nativefunction.h | 12 |
8 files changed, 89 insertions, 69 deletions
diff --git a/include/request.h b/include/environment.h index d204e85..b8b4f74 100644 --- a/include/request.h +++ b/include/environment.h @@ -1,14 +1,14 @@ /** - * Request.h + * Environment.h * * During the lifetime of the extension, multiple requests can be handled - * by it. For every request that is handled, a request object is created. + * by it. For every request that is handled, an environment object is created. * - * The base class for the request is implemented in this file. If you'd like - * to add state variables to the request you can override this class and + * The base class for the environment is defined in this file. If you'd like + * to add state variables to the environment you can override this class and * add the extra features you'd like. If you override this method, you should - * also override Extension::request() to return an instance of a different - * class. + * also override Extension::createEnvironment() to return an instance of a + * different class. * * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> * @copyright 2013 Copernica BV @@ -27,19 +27,25 @@ class Extension; /** * Class definition */ -class Request +class Environment { public: /** * Constructor * @param extension */ - Request(Extension *extension) : _extension(extension) {} + Environment(Extension *extension) : _extension(extension) {} + + /** + * Disable copy and move operations + */ + Environment(const Environment &environment) = delete; + Environment(Environment &&environment) = delete; /** * Destructor */ - virtual ~Request() {} + virtual ~Environment() {} /** * Initialize the request @@ -67,19 +73,37 @@ public: { return true; } + + /** + * Get access to the user supplied data + * @return void* + */ + virtual void *data() + { + return _data; + } + + /** + * Change the user supplied data + * @param data + */ + virtual void setData(void *data) + { + _data = data; + } protected: /** - * The extension that this request belongs to + * The extension that this environment belongs to * @var Extension* */ Extension *_extension; /** - * Optional extra data - * @var Type + * Pointer to user supplied data + * @var void* */ - Type _data; + void *_data = NULL; }; /** diff --git a/include/extension.h b/include/extension.h index f5987e2..b322533 100644 --- a/include/extension.h +++ b/include/extension.h @@ -35,12 +35,12 @@ namespace Php { */ typedef void (*native_callback_0)(); typedef void (*native_callback_1)(Parameters &); -typedef void (*native_callback_2)(Request &); -typedef void (*native_callback_3)(Request &, Parameters &); +typedef void (*native_callback_2)(Environment &); +typedef void (*native_callback_3)(Environment &, Parameters &); typedef Value (*native_callback_4)(); typedef Value (*native_callback_5)(Parameters &); -typedef Value (*native_callback_6)(Request &); -typedef Value (*native_callback_7)(Request &, Parameters &); +typedef Value (*native_callback_6)(Environment &); +typedef Value (*native_callback_7)(Environment &, Parameters &); /** * Class definition @@ -99,17 +99,29 @@ public: } /** - * Create a new request + * Create a new environment * - * You can override this method if you've created your own request class, + * You can override this method if you've created your own environment class, * and you'd like to use an instance of that class instead. The returned * object must have been created on the heap. * - * @return Request* + * @return Environment* */ - virtual Request *request() + virtual Environment *createEnvironment() { - return new Request(this); + return new Environment(this); + } + + /** + * Destruct an environment + * + * This is the counterpart of the createEnvironment method. + * + * @param Environment + */ + virtual void deleteEnvironment(Environment *environment) + { + delete environment; } /** @@ -121,17 +133,8 @@ public: * * @return boolean */ - bool startRequest() + bool startRequest(Environment &environment) { -// // failure if we already have a request -// if (_request) return false; -// -// // create the request -// _request = request(); -// -// // and initialize it -// return _request->initialize(); - return true; } @@ -143,20 +146,8 @@ public: * * @return boolean */ - bool endRequest() + bool endRequest(Environment &environment) { -// // request must exist -// if (!_request) return false; -// -// // finalize the request -// bool result = _request->finalize(); -// -// // destruct the request object -// delete _request; -// -// // done -// return result; - return true; } diff --git a/include/function.h b/include/function.h index ff4396c..c78fdd6 100644 --- a/include/function.h +++ b/include/function.h @@ -94,11 +94,11 @@ public: /** * Method that gets called every time the function is executed - * @param request Request object + * @param environment Environment object * @param params The parameters that were passed * @return Variable Return value */ - virtual Value invoke(Request &request, Parameters ¶ms) + virtual Value invoke(Environment &environment, Parameters ¶ms) { return nullptr; } @@ -23,7 +23,7 @@ */ #include <phpcpp/hiddenpointer.h> #include <phpcpp/type.h> -#include <phpcpp/request.h> +#include <phpcpp/environment.h> #include <phpcpp/argument.h> #include <phpcpp/byval.h> #include <phpcpp/byref.h> diff --git a/src/extension.cpp b/src/extension.cpp index 9323bd3..37082b2 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -40,7 +40,7 @@ static Extension *extension = nullptr; * one and only global variable */ ZEND_BEGIN_MODULE_GLOBALS(phpcpp) - Request *request; + Environment *environment; ZEND_END_MODULE_GLOBALS(phpcpp) /** @@ -49,9 +49,9 @@ ZEND_END_MODULE_GLOBALS(phpcpp) * structure above. */ #ifdef ZTS -#define REQUEST_G(v) TSRMG(phpcpp_globals_id, zend_phpcpp_globals *, v) +#define PHPCPP_G(v) TSRMG(phpcpp_globals_id, zend_phpcpp_globals *, v) #else -#define REQUEST_G(v) (phpcpp_globals.v) +#define PHPCPP_G(v) (phpcpp_globals.v) #endif /** @@ -69,7 +69,7 @@ static ZEND_DECLARE_MODULE_GLOBALS(phpcpp) * method (crazy) * @param globals */ -static void php_phpcpp_init_globals(zend_phpcpp_globals *globals) {} +static void init_globals(zend_phpcpp_globals *globals) {} @@ -81,11 +81,8 @@ static void php_phpcpp_init_globals(zend_phpcpp_globals *globals) {} */ static int extension_startup(INIT_FUNC_ARGS) { - - - // initialize and allocate the "global" variables -// ZEND_INIT_MODULE_GLOBALS(hello, php_phpcpp_init_globals, NULL); + ZEND_INIT_MODULE_GLOBALS(phpcpp, init_globals, NULL); // initialize the extension return BOOL2SUCCESS(extension->initialize()); @@ -111,8 +108,11 @@ static int extension_shutdown(SHUTDOWN_FUNC_ARGS) */ static int request_startup(INIT_FUNC_ARGS) { - // create the request - return BOOL2SUCCESS(extension->startRequest()); + // create the environment + PHPCPP_G(environment) = extension->createEnvironment(); + + // start the request + return BOOL2SUCCESS(extension->startRequest(*(PHPCPP_G(environment)))); } /** @@ -124,10 +124,15 @@ static int request_startup(INIT_FUNC_ARGS) static int request_shutdown(INIT_FUNC_ARGS) { // end the request - return BOOL2SUCCESS(extension->endRequest()); + bool success = extension->endRequest(*(PHPCPP_G(environment))); + + // deallocate the environment + extension->deleteEnvironment(PHPCPP_G(environment)); + + // done + return BOOL2SUCCESS(success); } - /** * Constructor * @param name Name of the extension diff --git a/src/function.cpp b/src/function.cpp index 755bb48..10b47b9 100644 --- a/src/function.cpp +++ b/src/function.cpp @@ -38,10 +38,10 @@ void invoke_function(INTERNAL_FUNCTION_PARAMETERS) Parameters params(ZEND_NUM_ARGS()); // @todo get the appropriate request (or environment) - Request request(NULL); + Environment environment(NULL); // get the result - result = function->invoke(request, params); + result = function->invoke(environment, params); } /** diff --git a/src/includes.h b/src/includes.h index 22f2891..cfcff2a 100644 --- a/src/includes.h +++ b/src/includes.h @@ -36,7 +36,7 @@ */ #include "../include/hiddenpointer.h" #include "../include/type.h" -#include "../include/request.h" +#include "../include/environment.h" #include "../include/argument.h" #include "../include/byval.h" #include "../include/byref.h" diff --git a/src/nativefunction.h b/src/nativefunction.h index e8281ce..88d629f 100644 --- a/src/nativefunction.h +++ b/src/nativefunction.h @@ -40,21 +40,21 @@ public: /** * Method that gets called every time the function is executed - * @param request Request environment + * @param environment Environment * @param params The parameters that were passed * @return Variable Return value */ - virtual Value invoke(Request &request, Parameters ¶ms) override + virtual Value invoke(Environment &environment, Parameters ¶ms) override { switch (_type) { case 0: _function.f0(); return Value(); case 1: _function.f1(params); return Value(); - case 2: _function.f2(request); return Value(); - case 3: _function.f3(request, params); return Value(); + case 2: _function.f2(environment); return Value(); + case 3: _function.f3(environment, params); return Value(); case 4: return _function.f4(); case 5: return _function.f5(params); - case 6: return _function.f6(request); - case 7: return _function.f7(request, params); + case 6: return _function.f6(environment); + case 7: return _function.f7(environment, params); default: return Value(); } } |