diff options
-rw-r--r-- | include/extension.h | 68 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | src/entry.cpp | 24 | ||||
-rw-r--r-- | src/extension.cpp | 39 | ||||
-rw-r--r-- | src/includes.h | 1 | ||||
-rw-r--r-- | tests/simple/simple.cpp | 31 |
6 files changed, 128 insertions, 36 deletions
diff --git a/include/extension.h b/include/extension.h index 3a852ac..eb7eb4c 100644 --- a/include/extension.h +++ b/include/extension.h @@ -10,6 +10,9 @@ * request is handled by an extension instance, but for others (when PHP runs * as module in a webserver) many requests are handled by the same extension * instance. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV */ /** @@ -72,8 +75,67 @@ public: } /** + * Create a new request + * + * You can override this method if you've created your own request 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* + */ + virtual Request *request() + { + return new Request(this); + } + + /** + * Start a request + * + * This method is called when the zend engine is about to start a new + * request. Internally, it calls the request() method to instantiate + * a new request object, and after that it initializes the request. + * + * @return boolean + */ + bool startRequest() + { + // failure if we already have a request + if (_request) return false; + + // create the request + _request = request(); + + // and initialize it + return _request->initialize(); + } + + /** + * End a request + * + * This method is called when the Zend engine is ready with a request. + * Internally, it destructs the request + * + * @return boolean + */ + bool endRequest() + { + // request must exist + if (!_request) return false; + + // finalize the request + bool result = _request->finalize(); + + // destruct the request object + delete _request; + + // done + return result; + } + + /** * Internal method to get access to the entry * @return zend_module_entry + * @internal */ _zend_module_entry *entry(); @@ -96,7 +158,11 @@ private: */ _zend_module_entry *_entry; - + /** + * The current request being processed + * @var Request + */ + Request *_request; }; @@ -10,6 +10,7 @@ /** * Include all headers files that are related to this library */ +#include <phpcpp/request.h> #include <phpcpp/extension.h> /** diff --git a/src/entry.cpp b/src/entry.cpp deleted file mode 100644 index 66fa244..0000000 --- a/src/entry.cpp +++ /dev/null @@ -1,24 +0,0 @@ -/** - * Entry.cpp - * - * The kickstart function that loads the library. When PHP loads an extension, - * if runs a dlopen() call to open the .so file, and the it locates the - * get_module()' function - which it then calls. - * - * In this file we have implemented this function. - * - * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> - * @documentation private - */ -#include "includes.h" - -/** - * The get_entry function - * @return zend_module_entry_t - */ -DLL_EXPORT zend_module_entry_t *get_entry() -{ - - -} - diff --git a/src/extension.cpp b/src/extension.cpp index 0a7def6..94543ef 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -22,7 +22,7 @@ static Extension *extension; * @param name Name of the extension * @param version Version number */ -Extension::Extension(const char *name, const char *version) : _name(name), _version(version), _entry(NULL) +Extension::Extension(const char *name, const char *version) : _name(name), _version(version), _entry(NULL), _request(NULL) { // store pointer to the one and only extension extension = this; @@ -41,11 +41,11 @@ Extension::~Extension() * Function that is called when the extension initializes * @param type Module type * @param number Module number - * @param tsrm_ls Optional thread safety thing * @return int 0 on success */ static int extension_startup(INIT_FUNC_ARGS) { + // initialize the extension return BOOL2SUCCESS(extension->initialize()); } @@ -57,10 +57,35 @@ static int extension_startup(INIT_FUNC_ARGS) */ static int extension_shutdown(SHUTDOWN_FUNC_ARGS) { + // finalize the extension return BOOL2SUCCESS(extension->finalize()); } /** + * Function that is called when a request starts + * @param type Module type + * @param number Module number + * @return int 0 on success + */ +static int request_startup(INIT_FUNC_ARGS) +{ + // create the request + return BOOL2SUCCESS(extension->startRequest()); +} + +/** + * Function that is called when a request is ended + * @param type Module type + * @param number Module number + * @return int 0 on success + */ +static int request_shutdown(INIT_FUNC_ARGS) +{ + // end the request + return BOOL2SUCCESS(extension->endRequest()); +} + +/** * Retrieve a pointer to the entry * @return zend_module_entry */ @@ -83,16 +108,12 @@ zend_module_entry *Extension::entry() _entry->functions = NULL; // functions supported by this module _entry->module_startup_func = extension_startup; // startup function for the whole extension _entry->module_shutdown_func = extension_shutdown; // shutdown function for the whole extension - _entry->request_startup_func = NULL; // startup function per request - _entry->request_shutdown_func = NULL; // shutdown function per request + _entry->request_startup_func = request_startup; // startup function per request + _entry->request_shutdown_func = request_shutdown; // shutdown function per request _entry->info_func = NULL; // information for retrieving info _entry->version = _version; // version string - _entry->globals_size = 0; // number of global variables -#ifdef ZTS - _entry->globals_id_ptr = NULL; // pointer to the globals, thread safe -#else + _entry->globals_size = 0; // size of the global variables _entry->globals_ptr = NULL; // pointer to the globals -#endif _entry->globals_ctor = NULL; // constructor for global variables _entry->globals_dtor = NULL; // destructor for global variables _entry->post_deactivate_func = NULL; // unknown function diff --git a/src/includes.h b/src/includes.h index 5d8fb8c..1decef0 100644 --- a/src/includes.h +++ b/src/includes.h @@ -25,5 +25,6 @@ /** * Include other files from this library */ +#include "../include/request.h" #include "../include/extension.h" diff --git a/tests/simple/simple.cpp b/tests/simple/simple.cpp index fbfcbf8..34a12ce 100644 --- a/tests/simple/simple.cpp +++ b/tests/simple/simple.cpp @@ -15,6 +15,29 @@ using namespace std; /** + * Override the request class + */ +class SimpleRequest : public PhpCpp::Request +{ +public: + SimpleRequest(PhpCpp::Extension *extension) : PhpCpp::Request(extension) + { + } + + virtual bool initialize() + { + cout << "Request::initialize" << endl; + return true; + } + + virtual bool finalize() + { + cout << "Request::finalize" << endl; + return true; + } +}; + +/** * Override the extension class */ class SimpleExtension : public PhpCpp::Extension @@ -29,16 +52,20 @@ public: virtual bool initialize() { - cout << "initialize" << endl; + cout << "Extension::initialize" << endl; return true; } virtual bool finalize() { - cout << "finalize" << endl; + cout << "Extension::finalize" << endl; return true; } + virtual PhpCpp::Request *request() + { + return new SimpleRequest(this); + } }; // create the object for the PHP extension |