diff options
Diffstat (limited to 'common')
-rw-r--r-- | common/extensionbase.h | 138 | ||||
-rw-r--r-- | common/includes.h | 23 | ||||
-rw-r--r-- | common/modifiers.cpp | 39 | ||||
-rw-r--r-- | common/streambuf.cpp | 60 | ||||
-rw-r--r-- | common/streambuf.h | 80 |
5 files changed, 340 insertions, 0 deletions
diff --git a/common/extensionbase.h b/common/extensionbase.h new file mode 100644 index 0000000..bf7f128 --- /dev/null +++ b/common/extensionbase.h @@ -0,0 +1,138 @@ +/** + * ExtensionBase.h + * + * Base class for ExtensionImpl objects. Common code used by both the Zend + * and HHVM engine. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013, 2014 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class ExtensionBase +{ +protected: + /** + * Pointer to the extension object that is filled by the extension programmer + * @var Extension + */ + Extension *_data; + + /** + * Callback that is called after the engine is initialized and before the + * pageviews are going to be handled + * @var Callback + */ + Callback _onStartup; + + /** + * Callback that is called in front of each request + * @var Callback + */ + Callback _onRequest; + + /** + * Callback that is called right after each request + * @var Callback + */ + Callback _onIdle; + + /** + * Callback that is called right before the engine is closing down + * @var Callback + */ + Callback _onShutdown; + +public: + /** + * Constructor + * @param data Extension object created by the extension programmer + */ + ExtensionBase(Extension *data) : _data(data) {} + + /** + * No copy'ing and no moving + */ + ExtensionBase(const ExtensionImpl &extension) = delete; + ExtensionBase(ExtensionImpl &&extension) = delete; + + /** + * Destructor + */ + virtual ~ExtensionBase() {} + + /** + * Register a function to be called when the PHP engine is ready + * + * The callback will be called after all extensions are loaded, and all + * functions and classes are available, but before the first pageview/request + * is handled. You can register this callback if you want to be notified + * when the engine is ready, for example to initialize certain things. + * + * @param callback + */ + void onStartup(const Callback &callback) + { + // copy callback + _onStartup = callback; + } + + /** + * Register a function to be called when the PHP engine is going to stop + * + * The callback will be called right before the process is going to stop. + * You can register a function if you want to clean up certain things. + * + * @param callback + */ + void onShutdown(const Callback &callback) + { + // copy callback + _onShutdown = callback; + } + + /** + * Register a callback that is called at the beginning of each pageview/request + * + * You can register a callback if you want to initialize certain things + * at the beginning of each request. Remember that the extension can handle + * multiple requests after each other, and you may want to set back certain + * global variables to their initial variables in front of each request + * + * @param callback + */ + void onRequest(const Callback &callback) + { + // copy callback + _onRequest = callback; + } + + /** + * Register a callback that is called to cleanup things after a pageview/request + * + * The callback will be called after _each_ request, so that you can clean up + * certain things and make your extension ready to handle the next request. + * This method is called onIdle because the extension is idle in between + * requests. + * + * @param callback + */ + void onIdle(const Callback &callback) + { + // copy callback + _onIdle = callback; + } +}; + +/** + * End of namespace + */ +} + diff --git a/common/includes.h b/common/includes.h new file mode 100644 index 0000000..db29f10 --- /dev/null +++ b/common/includes.h @@ -0,0 +1,23 @@ +/** + * Includes.h + * + * All includes for compiling the common module files of PHP-CPP library + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Standard C and C++ libraries + */ +#include <sstream> + +/** + * Public include files + */ +#include "../include/modifiers.h" + +/** + * Generic implementation header files + */ +#include "streambuf.h" diff --git a/common/modifiers.cpp b/common/modifiers.cpp new file mode 100644 index 0000000..3720730 --- /dev/null +++ b/common/modifiers.cpp @@ -0,0 +1,39 @@ +/** + * Modifiers.cpp + * + * In this file an enumeration type is with the possible + * member modifiers + * + * @author Martijn Otto <martijn.otto@copernica.com> + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * + * @copyright 2014 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * The modifiers are constants + */ +const int Static = 0x01; +const int Abstract = 0x02; +const int Final = 0x04; +const int Public = 0x100; +const int Protected = 0x200; +const int Private = 0x400; +const int Const = 0; + +/** + * Modifiers that are supported for methods and properties + */ +const int MethodModifiers = Final | Public | Protected | Private; +const int PropertyModifiers = Final | Public | Protected | Private | Const | Static; + +/** + * End namespace + */ +} diff --git a/common/streambuf.cpp b/common/streambuf.cpp new file mode 100644 index 0000000..2d87f9a --- /dev/null +++ b/common/streambuf.cpp @@ -0,0 +1,60 @@ +/** + * StreamBuf.cpp + * + * Implementation file for the StreamBuf class + * + * @see http://www.mr-edd.co.uk/blog/beginners_guide_streambuf + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Constructor + * @param error + */ +StreamBuf::StreamBuf(int error) : _error(error) +{ + // we reserve one byte, so that when overflow is called, we still have one + // byte extra in the buffer to put the overflowed byte int + setp(_buffer, _buffer+1024-1); +} + + +/** + * Method that is called when the internal buffer overflows + * @param c + * @return int + */ +int StreamBuf::overflow(int c) +{ + // for error buffers, overflow is simply discarded + if (_error) return c; + + // end-of-file has not output, we call EOF directly, and by using the + // comma operator we ensure that EOF is returned + if (c == EOF) return sync(), EOF; + + // because we lied the underlying buffer about the size of the buffer + // by one byte, there is no real overflow, and we can still add the byte + // to the end of the buffer + *pptr() = c; + + // increment buffer size + pbump(1); + + // and now we're going to syn the buffer + return sync() == -1 ? EOF : c; +} + +/** + * End namespace + */ +} +
\ No newline at end of file diff --git a/common/streambuf.h b/common/streambuf.h new file mode 100644 index 0000000..ed1506a --- /dev/null +++ b/common/streambuf.h @@ -0,0 +1,80 @@ +/** + * StreamBuf.h + * + * PHP output stream buffer which is used by the Php::out object to + * have an output stream just like the regular std::ostream buffers, + * but that sends all output to PHP output + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class StreamBuf : public std::streambuf +{ +public: + /** + * Constructor + * @param error the error type, or 0 for regular output + */ + StreamBuf(int error); + + /** + * No copying or moving + * @param that + */ + StreamBuf(const StreamBuf &that) = delete; + StreamBuf(StreamBuf &&that) = delete; + + /** + * Destructor + */ + virtual ~StreamBuf() {} + + /** + * No copying or moving + * @param that + */ + StreamBuf &operator=(const StreamBuf &that) = delete; + StreamBuf &operator=(StreamBuf &&that) = delete; + +protected: + /** + * Method that is called when the internal buffer overflows + * @param c + * @return int + */ + virtual int overflow(int c = EOF) override; + + /** + * Called when the internal buffer should be synchronized + * @return int + */ + virtual int sync() override; + +private: + /** + * The error type, or 0 for regular output + * @var int + */ + int _error; + + /** + * The internal buffer + * @var char[] + */ + char _buffer[1024]; +}; + +/** + * End namespace + */ +} + |