diff options
author | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2015-01-10 21:13:13 +0100 |
---|---|---|
committer | Emiel Bruijntjes <emiel.bruijntjes@copernica.com> | 2015-01-10 21:13:13 +0100 |
commit | 8edf958a8ddf6c13b8bba9b87f3d5b1296be1376 (patch) | |
tree | a3fdeeb1b82db662df95f3fbfc14e2e6f8e199d2 /include | |
parent | fb80367d418f9a9fb059c98d5aec80febeb3bd23 (diff) |
added Script class to simplify parsing and executing php scripts (the Php::eval() call both compiles and executes a script, while the Script class splits these two steps, which allows you to run the same opcodes multiple times)
Diffstat (limited to 'include')
-rw-r--r-- | include/script.h | 95 | ||||
-rw-r--r-- | include/value.h | 1 |
2 files changed, 96 insertions, 0 deletions
diff --git a/include/script.h b/include/script.h new file mode 100644 index 0000000..d2eb575 --- /dev/null +++ b/include/script.h @@ -0,0 +1,95 @@ +/** + * Script.h + * + * Class that can be used to evaluate a PHP script in the current PHP context. + * + * The difference between directly calling eval() is that the script object + * will first evaluate the string, and then it can be executed multiple times. + * + * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com> + * @copyright 2014 Copernica BV + */ + +/** + * Forward declarations + */ +struct _zend_op_array; + +/** + * Set up namespace + */ +namespace Php { + +/** + * Class definition + */ +class Script +{ +public: + /** + * Constructor + * + * The constructor will not throw any exceptions, even when invalid + * PHP code is passed to it that can not be evaluated. You should call + * the valid() to find out if the script was valid (could be parsed). + * + * @param name Name of the PHP script + * @param source PHP source code to be evaluated + * @param size Length of the source code + */ + Script(const char *name, const char *source, size_t size) noexcept; + + /** + * Alternative constructor without a size + * @param name Name of the PHP script + * @param source PHP source code to be evaluated + */ + Script(const char *name, const char *source) noexcept : Script(name, source, ::strlen(source)) {} + + /** + * Alternative constructor without a name + * @param source PHP source code to be evaluated + * @param size Length of the source code + */ + Script(const char *source, size_t size) noexcept : Script("Unknown", source, size) {} + + /** + * Alternative constructor without a name and without a size + * @param source PHP source code to be evaluated + */ + Script(const char *source) noexcept : Script("Unknown", source, ::strlen(source)) {} + + /** + * Destructor + */ + virtual ~Script(); + + /** + * Is the script a valid PHP script without syntax errors? + * @return bool + */ + bool valid() const + { + return _opcodes != nullptr; + } + + /** + * Execute the script + * The return value of the script is returned + * @return Value + */ + Value execute() const; + +private: + /** + * The opcodes + * @var zend_op_array + */ + struct _zend_op_array *_opcodes; + +}; + +/** + * End of namespace + */ +} diff --git a/include/value.h b/include/value.h index f0b7345..9447fc8 100644 --- a/include/value.h +++ b/include/value.h @@ -1170,6 +1170,7 @@ protected: friend class HashMember<int>; friend class HashMember<std::string>; friend class Callable; + friend class Script; }; /** |