From 5fd8b29a1981d2d4f7c4e9925729fbe9f1c558bb Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sat, 17 Jan 2015 22:19:08 +0100 Subject: added initial implementation for registering constants --- include/class.h | 14 +++++---- include/constant.h | 67 +++++++++++++++++++++++++++++++++++++++++ include/namespace.h | 50 +++++++++++++++++++++++++++++++ include/value.h | 1 + zend/constant.cpp | 28 +++++++++++++++++ zend/constantimpl.h | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++ zend/extensionimpl.cpp | 14 +++++++-- zend/extensionimpl.h | 3 +- zend/includes.h | 2 ++ 9 files changed, 251 insertions(+), 9 deletions(-) create mode 100644 include/constant.h create mode 100644 zend/constant.cpp create mode 100644 zend/constantimpl.h diff --git a/include/class.h b/include/class.h index e11074c..2c875fa 100644 --- a/include/class.h +++ b/include/class.h @@ -91,12 +91,16 @@ public: /** * Add a static method to a class * - * In C++ a static method is just a plain function, that only at compile - * time has access to the private variables. You can therefore also supply - * global functions as static method, and real static methods (that do not - * even have to come from the same class. + * In C++ a static method is in reality just a plain function, that at + * compile time has access to private properties of the class that it is a + * static member of. * - * In PHP scripts, the function will only be callable as real static method + * Because a C++ static method is not a real method with a 'this' pointer, + * it has the same signature as a normal C++ (non-method) function. Therefore, + * you can register real static member functions (&MyClass::myMethod) as well + * as normal functions (myFunction) as class methods. + * + * In PHP scripts, such functions will be callable as static class methods * * @param name Name of the method * @param method The actual method diff --git a/include/constant.h b/include/constant.h new file mode 100644 index 0000000..c6be099 --- /dev/null +++ b/include/constant.h @@ -0,0 +1,67 @@ +/** + * Constant.h + * + * If you want to define global PHP constants, or class constants you can + * use this constant class for it. Wrap the constant you'd like to create + * in a Php::Constant object and add it to the extension or the class: + * + * extension.add(Php::Constant("CONSTANT_NAME", "value")); + * myclass.add(Php::Constant("CLASS_CONSTANT", "value")); + * + * @author Emiel Bruijntjes + * @copyright 2015 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Forward declarations + */ +class ConstantImpl; + +/** + * Class definition + */ +class Constant +{ +public: + /** + * Constructor + * @param name Constant name + * @param value Constant value + */ + Constant(const char *name, const Value &value); + + /** + * Destructor + */ + virtual ~Constant() {} + +private: + /** + * Pointer to the actual implementation of the constant + * @var std::shared_ptr + */ + std::shared_ptr _impl; + + /** + * Get access to the implementation object + * @return std::shared_ptr + */ + const std::shared_ptr &implementation() const { return _impl; } + + /** + * The extension object has access to privates + */ + friend class ExtensionImpl; + +}; + +/** + * End of namespace + */ +} + diff --git a/include/namespace.h b/include/namespace.h index 84f0740..4bb23db 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -42,6 +42,12 @@ protected: */ std::list> _classes; + /** + * Constants defined in the namespace + * @var list + */ + std::list> _constants; + /** * Namespaces defined inside the namespace * @var list @@ -162,6 +168,40 @@ public: return *this; } + /** + * Add a constant to the namespace + * @param constant The constant to add + * @return Namespace Same object to allow chaining + */ + Namespace &add(Constant &&constant) + { + // skip when locked + if (locked()) return *this; + + // and add it to the list of constants + _constants.push_back(std::unique_ptr(new Constant(std::move(constant)))); + + // allow chaining + return *this; + } + + /** + * Add a constant to the namespace + * @param constant The constant to add + * @return Namespace Same object to allow chaining + */ + Namespace &add(const Constant &constant) + { + // skip when locked + if (locked()) return *this; + + // and add it to the list of constants + _constants.push_back(std::unique_ptr(new Constant(constant))); + + // allow chaining + return *this; + } + /** * Add a namespace to the namespace by moving it * @param ns The namespace @@ -231,6 +271,16 @@ public: * @param callback */ void classes(const std::function &callback); + + /** + * Apply a callback to each registered constant + * + * The callback will be called with the name of the namespace, and + * a reference to the registered constant + * + * @param callback + */ + void constants(const std::function &callback); }; diff --git a/include/value.h b/include/value.h index c3de4d5..a450755 100644 --- a/include/value.h +++ b/include/value.h @@ -1171,6 +1171,7 @@ protected: friend class HashMember; friend class Callable; friend class Script; + friend class ConstantImpl; }; /** diff --git a/zend/constant.cpp b/zend/constant.cpp new file mode 100644 index 0000000..a5358c6 --- /dev/null +++ b/zend/constant.cpp @@ -0,0 +1,28 @@ +/** + * Constant.cpp + * + * Implementation file for the constant class + * + * @author Emiel Bruijntjes + * @copyright 2015 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Constructor + * @param name Constant name + * @param value Constant value + */ +Constant::Constant(const char *name, const Value &value) : + _impl(new ConstantImpl(name, value)) {} + +/** + * End of namespace + */ +} + diff --git a/zend/constantimpl.h b/zend/constantimpl.h new file mode 100644 index 0000000..0545253 --- /dev/null +++ b/zend/constantimpl.h @@ -0,0 +1,81 @@ +/** + * ConstantImpl.h + * + * Implementation file for the constant class + * + * @author Emiel Bruijntjes + * @copyright 2015 Copernica BV + */ + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class ConstantImpl +{ +public: + /** + * Constructor + * @param name + * @param value + */ + ConstantImpl(const char *name, const Value &value) : + _name(name), _value(value) {} + + /** + * Destructor + */ + virtual ~ConstantImpl() {} + + /** + * Initialize the constant + * @param prefix Namespace prefix + * @param module_number The module number + * @param tsrmls Optional parameter when running in multi-threading context + */ + void initialize(const std::string &prefix, int module_number TSRMLS_DC) const + { + // create constant structure + zend_constant constant; + + // copy zval + INIT_PZVAL_COPY(&constant.value, _value._val); + + // we have to call the copy constructor to copy the entire other zval + zval_copy_ctor(&constant.value); + + // @todo include prefix + + // set all the other constant properties + constant.flags = CONST_CS | CONST_PERSISTENT; + constant.name_len = ::strlen(_name); + constant.name = zend_strndup(_name, constant.name_len); + constant.module_number = module_number; + + // register the zval + zend_register_constant(&constant TSRMLS_CC); + } + +private: + /** + * The name of the constant + * @var const char * + */ + const char *_name; + + /** + * The value of the constant + * @var Value + */ + Value _value; +}; + +/** + * End of namespace + */ +} + diff --git a/zend/extensionimpl.cpp b/zend/extensionimpl.cpp index 262ecdb..7e1f1ce 100644 --- a/zend/extensionimpl.cpp +++ b/zend/extensionimpl.cpp @@ -140,7 +140,7 @@ int ExtensionImpl::processStartup(int type, int module_number TSRMLS_DC) zend_register_ini_entries(entries, module_number TSRMLS_CC); // initialize the extension - extension->initialize(TSRMLS_C); + extension->initialize(module_number TSRMLS_CC); // remember that we're initialized (when you use "apache reload" it is // possible that the processStartup() method is called more than once) @@ -322,11 +322,19 @@ zend_module_entry *ExtensionImpl::module() /** * Initialize the extension after it was started + * @param module_number * @param tsrm_ls */ -void ExtensionImpl::initialize(TSRMLS_D) +void ExtensionImpl::initialize(int module_number TSRMLS_DC) { - // we need to register each class, find out all classes + // the constants are registered after the module is ready + _data->constants([module_number TSRMLS_CC](const std::string &prefix, Constant &c) { + + // forward to implementation class + c.implementation()->initialize(prefix, module_number TSRMLS_CC); + }); + + // we also need to register each class, find out all classes _data->classes([TSRMLS_C](const std::string &prefix, ClassBase &c) { // forward to implementation class diff --git a/zend/extensionimpl.h b/zend/extensionimpl.h index 0e9a289..a2a3a44 100644 --- a/zend/extensionimpl.h +++ b/zend/extensionimpl.h @@ -91,9 +91,10 @@ public: private: /** * Initialize the namespace after it was registered + * @param module_number * @param tsrm_ls */ - void initialize(TSRMLS_D); + void initialize(int module_number TSRMLS_DC); /** * Function that is called when the extension initializes diff --git a/zend/includes.h b/zend/includes.h index 83b1086..d013774 100644 --- a/zend/includes.h +++ b/zend/includes.h @@ -77,6 +77,7 @@ #include "../include/classbase.h" #include "../include/interface.h" #include "../include/class.h" +#include "../include/constant.h" #include "../include/namespace.h" #include "../include/extension.h" #include "../include/call.h" @@ -120,6 +121,7 @@ #include "executestate.h" #include "opcodes.h" #include "functor.h" +#include "constantimpl.h" #ifndef ZVAL_COPY_VALUE #define ZVAL_COPY_VALUE(z, v) \ -- cgit v1.2.3