diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-12-06 07:42:36 -0800 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2013-12-06 07:42:36 -0800 |
commit | c91dc2292f4fd49a3103691cb70fee8336c6aa6c (patch) | |
tree | 578d41f25a0ce7b37ab3b7c9d2aa1992ca91b0c0 /src/globals.cpp | |
parent | 3750e24683f3a1d0b643e9808d9619db40d31cc4 (diff) |
Global variables are now accessible via Php::globals["varname"]
Diffstat (limited to 'src/globals.cpp')
-rw-r--r-- | src/globals.cpp | 314 |
1 files changed, 314 insertions, 0 deletions
diff --git a/src/globals.cpp b/src/globals.cpp new file mode 100644 index 0000000..e8672aa --- /dev/null +++ b/src/globals.cpp @@ -0,0 +1,314 @@ +/** + * Globals.cpp + * + * Implementation of the globals class + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Namespace + */ +namespace Php { + +/** + * Get access to the globals single instance + * @return Globals + */ +Globals &Globals::instance() +{ + static Globals globals; + return globals; +} + +/** + * The one and only instance + * @var Globals + */ +Globals &globals = Globals::instance(); + +/** + * Get access to a global variable + * @param name + * @return Global + */ +Global Globals::operator[](const char *name) +{ + // pointer to a zval + zval **varvalue; + + // check if the variable already exists + if (zend_hash_find(&EG(symbol_table), name, strlen(name)+1, (void**)&varvalue) == FAILURE) + { + // the variable does not already exist, return a global object + // that will automatically set the value when it is updated + return Global(name); + } + else + { + // we are in the happy situation that the variable exists, we turn + // this value into a reference value, and return that + return Global(name, *varvalue); + } +} + +/** + * Get access to a global variable + * @param name + * @return Global + */ +Global Globals::operator[](const std::string &name) +{ + // pointer to a zval + zval **varvalue; + + // check if the variable already exists + if (zend_hash_find(&EG(symbol_table), name.c_str(), name.size()+1, (void**)&varvalue) == FAILURE) + { + // the variable does not already exist, return a global object + // that will automatically set the value when it is updated + return Global(name); + } + else + { + // we are in the happy situation that the variable exists, we turn + // this value into a reference value, and return that + return Global(name, *varvalue); + } +} + +/** + * Call function with a number of parameters + * @param name Function name + * @param argc Number of parameters + * @param argv The parameters + * @return Value + */ +Value Globals::exec(const Value &name, int argc, zval ***params) +{ + // the return zval + zval *retval = nullptr; + + // call the function + if (call_user_function_ex(CG(function_table), NULL, name._val, &retval, argc, params, 1, NULL) != SUCCESS) return nullptr; + + // was a value returned? + if (retval) return Value(retval); + + // no value was returned, return NULL + return nullptr; +} + +/** + * Call a function in PHP that does not have any parameters + * @param name Name of the function + * @return Value + */ +Value Globals::call(const Value &name) +{ + // call with zero parameters + return exec(name, 0, NULL); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0) +{ + // array of parameters + zval **params[1] = { &p0._val }; + + // call the function + return exec(name, 1, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1) +{ + // array of parameters + zval **params[2] = { &p0._val, &p1._val }; + + // call the function + return exec(name, 2, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2) +{ + // array of parameters + zval **params[3] = { &p0._val, &p1._val, &p2._val }; + + // call the function + return exec(name, 3, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3) +{ + // array of parameters + zval **params[4] = { &p0._val, &p1._val, &p2._val, &p3._val }; + + // call the function + return exec(name, 4, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4) +{ + // array of parameters + zval **params[5] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val }; + + // call the function + return exec(name, 5, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5) +{ + // array of parameters + zval **params[6] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val }; + + // call the function + return exec(name, 6, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6) +{ + // array of parameters + zval **params[7] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val }; + + // call the function + return exec(name, 7, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eight parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7) +{ + // array of parameters + zval **params[8] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val }; + + // call the function + return exec(name, 8, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eight parameter + * @param p8 The nineth parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8) +{ + // array of parameters + zval **params[9] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val }; + + // call the function + return exec(name, 9, params); +} + +/** + * Call a function in PHP + * @param name Name of the function + * @param p0 The first parameter + * @param p1 The second parameter + * @param p2 The third parameter + * @param p3 The fourth parameter + * @param p4 The fifth parameter + * @param p5 The sixth parameter + * @param p6 The seventh parameter + * @param p7 The eight parameter + * @param p8 The nineth parameter + * @param p9 The tenth parameter + * @return Value + */ +Value Globals::call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9) +{ + // array of parameters + zval **params[10] = { &p0._val, &p1._val, &p2._val, &p3._val, &p4._val, &p5._val, &p6._val, &p7._val, &p8._val, &p9._val }; + + // call the function + return exec(name, 10, params); +} + +/** + * End of namespace + */ +} + |