summaryrefslogtreecommitdiff
path: root/zend
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-25 19:52:06 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-25 19:52:06 +0100
commit110f379c083a9e061d2337efabb86f12ce0fa750 (patch)
tree3c5e541532607246feeecd8d9dea3f36f803071f /zend
parentb42532f1f49d9f404b100fe1a441de0c131673ee (diff)
added functions Php::constant() to retrieve the value of a constant, and Php::defined() to find out if a constant is defined
Diffstat (limited to 'zend')
-rw-r--r--zend/constant.cpp4
-rw-r--r--zend/constantfuncs.cpp113
-rw-r--r--zend/value.cpp3
3 files changed, 115 insertions, 5 deletions
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 <emiel.bruijntjes@copernica.com>
+ * @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