diff options
-rw-r--r-- | include/call.h | 3 | ||||
-rw-r--r-- | include/fastcall.h | 13 | ||||
-rw-r--r-- | phpcpp.h | 1 | ||||
-rw-r--r-- | zend/exists.cpp | 68 | ||||
-rw-r--r-- | zend/fastcall.cpp | 43 | ||||
-rw-r--r-- | zend/includes.h | 1 |
6 files changed, 71 insertions, 58 deletions
diff --git a/include/call.h b/include/call.h index 05c5955..80ef6c9 100644 --- a/include/call.h +++ b/include/call.h @@ -15,6 +15,9 @@ namespace Php { /** * List of functions that are available for use in PHP */ +bool class_exists(const char *classname, size_t size, bool autoload = true); +bool class_exists(const char *classname, bool autoload = true) { return class_exists(classname, strlen(classname), autoload); } +bool class_exists(const std::string &classname, bool autoload = true) { return class_exists(classname.c_str(), classname.size(), autoload); } Value eval(const std::string &phpCode); /** diff --git a/include/fastcall.h b/include/fastcall.h deleted file mode 100644 index a5a8e1c..0000000 --- a/include/fastcall.h +++ /dev/null @@ -1,13 +0,0 @@ -/** - * fastcall.h - * - * This file holds some PHP functions implementation in C directly. - * - */ - -namespace Php { - - bool class_exists(const std::string &classname, bool autoload = true); - -} - @@ -60,7 +60,6 @@ #include <phpcpp/namespace.h> #include <phpcpp/extension.h> #include <phpcpp/call.h> -#include <phpcpp/fastcall.h> /** * Macro to export a function diff --git a/zend/exists.cpp b/zend/exists.cpp new file mode 100644 index 0000000..6bab8ba --- /dev/null +++ b/zend/exists.cpp @@ -0,0 +1,68 @@ +/** + * Exists.cpp + * + * This file holds the implementation of all *_exists() functions, + * like class_exists(), et cetera + * + * @author andot <https://github.com/andot> + */ + +/** + * Dependencies + */ +#include "includes.h" + +/** + * Open the PHP namespace + */ +namespace Php { + +/** + * Check whether a class with a certain name exists + * @param classname + * @param len + * @param autoload + * @return bool + */ +bool class_exists(const char *classname, size_t len, bool autoload) +{ + // we need the tsrm_ls variable + TSRMLS_FETCH(); + + // we're going to load a class-entry + zend_class_entry **ce; + + // should we autoload the class? + if (autoload) + { + // starting slashes can be ignored + if (len > 0 && classname[0] == '\\') { classname++; len--; } + + // all classes are in lowercase in the hash, so we make + // a temporary buffer for storing the lowercase class name + // (is this smart? memory allocation is expensive!) + std::unique_ptr<char[]> lc_name(new char[len + 1]); + + // copy the name to lowercase, but ignore the starting slash (if there is one) + zend_str_tolower_copy(lc_name, classname, len); + + // see if there is a class with this name + if (SUCCESS != zend_hash_find(EG(class_table), name, len + 1, (void **) &ce)) return false; + + // the found "class" could also be an interface or trait, which we do no want + return !(((*ce)->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT)) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS); + } + else + { + // no auto-load + if (SUCCESS != zend_lookup_class(str, len, &ce TSRMLS_CC) == SUCCESS) return false; + + // the found "class" could also be an interface or trait, which we do no want + return ((*ce)->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0; + } +} + +/** + * End of namespace + */ +} diff --git a/zend/fastcall.cpp b/zend/fastcall.cpp deleted file mode 100644 index 359c3ad..0000000 --- a/zend/fastcall.cpp +++ /dev/null @@ -1,43 +0,0 @@ -/** - * fastcall.cpp - * - * This file holds some PHP functions implementation in C directly. - * - */ - -#include "includes.h" - -namespace Php { - - bool class_exists(const std::string &classname, bool autoload) { - // we need the tsrm_ls variable - TSRMLS_FETCH(); - - zend_class_entry **ce; - int found; - const char * str = classname.c_str(); - int32_t len = (int32_t)classname.length(); - - - if (autoload) { - char *lc_name = new char[len + 1]; - zend_str_tolower_copy(lc_name, str, len); - - char *name = lc_name; - if (lc_name[0] == '\\') { - name = &lc_name[1]; - --len; - } - - found = zend_hash_find(EG(class_table), name, len + 1, (void **) &ce); - delete [] lc_name; - return (found == SUCCESS && !(((*ce)->ce_flags & (ZEND_ACC_INTERFACE | ZEND_ACC_TRAIT)) > ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)); - } - - if (zend_lookup_class(str, len, &ce TSRMLS_CC) == SUCCESS) { - return (((*ce)->ce_flags & (ZEND_ACC_INTERFACE | (ZEND_ACC_TRAIT - ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))) == 0); - } - return false; - } - -}
\ No newline at end of file diff --git a/zend/includes.h b/zend/includes.h index 8af557c..63b435e 100644 --- a/zend/includes.h +++ b/zend/includes.h @@ -79,7 +79,6 @@ #include "../include/namespace.h" #include "../include/extension.h" #include "../include/call.h" -#include "../include/fastcall.h" /** * Common header files for internal use only |