From 110f379c083a9e061d2337efabb86f12ce0fa750 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sun, 25 Jan 2015 19:52:06 +0100 Subject: added functions Php::constant() to retrieve the value of a constant, and Php::defined() to find out if a constant is defined --- include/call.h | 6 +++ include/constant.h | 2 +- include/value.h | 5 +++ zend/constant.cpp | 4 +- zend/constantfuncs.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ zend/value.cpp | 3 +- 6 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 zend/constantfuncs.cpp diff --git a/include/call.h b/include/call.h index 279cbac..950cc3e 100644 --- a/include/call.h +++ b/include/call.h @@ -18,6 +18,12 @@ namespace Php { extern bool class_exists(const char *classname, size_t size, bool autoload = true); inline bool class_exists(const char *classname, bool autoload = true) { return class_exists(classname, strlen(classname), autoload); } inline bool class_exists(const std::string &classname, bool autoload = true) { return class_exists(classname.c_str(), classname.size(), autoload); } +extern Value constant(const char *constant); +extern Value constant(const char *constant, size_t size); +extern Value constant(const std::string &constant); +extern bool defined(const char *constant); +extern bool defined(const char *constant, size_t size); +extern bool defined(const std::string &constant); extern Value eval(const std::string &phpCode); extern Value include(const std::string &filename); extern Value include_once(const std::string &filename); diff --git a/include/constant.h b/include/constant.h index 36b3417..5e18792 100644 --- a/include/constant.h +++ b/include/constant.h @@ -66,7 +66,7 @@ public: * @param clss Class to which the constant is added * @internal */ - void addTo(ClassBase &clss); + void addTo(ClassBase &clss) const; private: /** diff --git a/include/value.h b/include/value.h index a450755..dbd3788 100644 --- a/include/value.h +++ b/include/value.h @@ -1157,6 +1157,11 @@ protected: */ struct _zend_class_entry *classEntry(bool allowString = true) const; + /** + * Functions that need access to the privates + */ + friend Value constant(const char *name, size_t size); + /** * The Globals and Member classes can access the zval directly */ diff --git a/zend/constant.cpp b/zend/constant.cpp index 102e4f7..4123bba 100644 --- a/zend/constant.cpp +++ b/zend/constant.cpp @@ -96,14 +96,12 @@ Constant::Constant(const char *name, const std::string &value) : * @param clss Class to which the constant is added * @internal */ -void Constant::addTo(ClassBase &clss) +void Constant::addTo(ClassBase &clss) const { // pass on to the implementation _impl->addTo(clss); } - - /** * End of namespace */ diff --git a/zend/constantfuncs.cpp b/zend/constantfuncs.cpp new file mode 100644 index 0000000..c350f91 --- /dev/null +++ b/zend/constantfuncs.cpp @@ -0,0 +1,113 @@ +/** + * ConstantFuncs.cpp + * + * C++ implementation of PHP functions to retrieve and set constants. + * + * @author Emiel Bruijntjes + * @copyright 2014 Copernica BV + */ + +/** + * Dependencies + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + +/** + * Retrieve the value of a constant by its name + * @param name Name of the constant + * @return Value Actual constant value + */ +Value constant(const char *name) +{ + // pass on to other implementation + return constant(name, ::strlen(name)); +} + +/** + * Retrieve a constant by its name, and the size of the name + * @param constant Name of the constant + * @param size Size of the name + * @return Value + */ +Value constant(const char *constant, size_t size) +{ + // we need the tsrm_ls variable + TSRMLS_FETCH(); + + // the value that holds the result + Value result; + + // retrieve the constant + if (!zend_get_constant(constant, size, result._val TSRMLS_CC)) return nullptr; + + // zval was correctly retrieved, wrap in value + return result; +} + +/** + * Retrieve the value of a constant by its name + * @param name Name of the constant + * @return Value Actual constant value + */ +Value constant(const std::string &name) +{ + // pass on to other implementation + return constant(name.c_str(), name.size()); +} + +/** + * Check whether a constant exists + * @param name + * @param size + * @return bool + */ +bool defined(const char *name, size_t size) +{ + // we need the tsrm_ls variable + TSRMLS_FETCH(); + + // result variable + zval c; + + // retrieve the constant + if (!zend_get_constant_ex(name, size, &c, NULL, ZEND_FETCH_CLASS_SILENT TSRMLS_CC)) return false; + + // constant exists, but the returned zval should first be destructed + zval_dtor(&c); + + // done + return true; +} + +/** + * Check whether a constant exists + * @param name + * @return bool + */ +bool defined(const char *name) +{ + // pass on + return defined(name, ::strlen(name)); +} + +/** + * Check whether a constant exists + * @param name + * @return bool + */ +bool defined(const std::string &name) +{ + // pass on + return defined(name.c_str(), name.size()); +} + +/** + * End namespace + */ +} + diff --git a/zend/value.cpp b/zend/value.cpp index 4a59d97..8c8cc9a 100644 --- a/zend/value.cpp +++ b/zend/value.cpp @@ -156,8 +156,7 @@ Value::Value(double value) * @param zval Value to wrap * @param ref Force this to be a reference */ -Value::Value(struct _zval_struct *val, bool ref) -: _val(val) +Value::Value(struct _zval_struct *val, bool ref) : _val(val) { // if the variable is not already a reference, and it has more than one // variable pointing to it, we should seperate it so that any changes -- cgit v1.2.3