summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-11 00:08:41 +0100
committerEmiel Bruijntjes <emiel.bruijntjes@copernica.com>2015-01-11 00:08:41 +0100
commit1efade1a7667e4e1a34265fb3cf310faf8c80bb6 (patch)
treebd311ac3de5500daae633fbfdb585faf737607ba /include
parent825049026dd2b2e906cdb46279faa39580d931d1 (diff)
refactored script class to have a seperate opcodes class, added file class that uses this same opcodes class
Diffstat (limited to 'include')
-rw-r--r--include/file.h76
-rw-r--r--include/opcodes.h69
-rw-r--r--include/script.h22
3 files changed, 162 insertions, 5 deletions
diff --git a/include/file.h b/include/file.h
new file mode 100644
index 0000000..093431e
--- /dev/null
+++ b/include/file.h
@@ -0,0 +1,76 @@
+/**
+ * File.h
+ *
+ * Extended script, a PHP source file name can be passed to a Php::File object
+ * to have it evaluated.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2014 Copernica BV
+ */
+
+/**
+ * Set up namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class File
+{
+public:
+ /**
+ * Constructor
+ *
+ * The constructor receives a filename as parameter. It uses the normal
+ * PHP include path resolve algorithms to find the location of the file.
+ *
+ * @param name the filename
+ * @param size size of the filename
+ */
+ File(const char *name, size_t size);
+
+ /**
+ * Alternative constructor with just a filename
+ *
+ * @param name the filename
+ */
+ File(const char *name) : File(name, ::strlen(name)) {}
+
+ /**
+ * Destructor
+ */
+ virtual ~File();
+
+ /**
+ * Include the file once
+ * @return Php::Value
+ */
+ Value once();
+
+ /**
+ * Execute the file
+ * @return Php::Value
+ */
+ Value execute();
+
+private:
+ /**
+ * The full resolved path name
+ * @var const char *
+ */
+ char *_path = nullptr;
+
+ /**
+ * The opcodes of this file
+ * @var Opcodes
+ */
+ Opcodes *_opcodes = nullptr;
+
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/opcodes.h b/include/opcodes.h
new file mode 100644
index 0000000..5a40d05
--- /dev/null
+++ b/include/opcodes.h
@@ -0,0 +1,69 @@
+/**
+ * Opcodes.h
+ *
+ * Class represents a set of opcodes of a PHP script that can be executed. This
+ * is an internal file that you normally do not have to instantiate yourself.
+ * Better use the Php::Script of Php::File classes.
+ *
+ * @author Emiel Bruijntjes <emiel.bruijntjes@copernica.com>
+ * @copyright 2014 Copernica BV
+ */
+
+/**
+ * Forward declarations
+ */
+struct _zend_op_array;
+
+/**
+ * Namespace
+ */
+namespace Php {
+
+/**
+ * Class definition
+ */
+class Opcodes
+{
+public:
+ /**
+ * Constructor
+ * @param opcodes
+ */
+ Opcodes(struct _zend_op_array *opcodes) : _opcodes(opcodes)
+ {
+ // no other initialisation is necessary
+ }
+
+ /**
+ * Destructor
+ */
+ virtual ~Opcodes();
+
+ /**
+ * Are the opcodes valid?
+ * @return bool
+ */
+ bool valid() const
+ {
+ return _opcodes != nullptr;
+ }
+
+ /**
+ * Execute the opcodes
+ * @return Value
+ */
+ Value execute() const;
+
+private:
+ /**
+ * The opcodes
+ * @var zend_op_array
+ */
+ struct _zend_op_array *_opcodes;
+};
+
+/**
+ * End of namespace
+ */
+}
+
diff --git a/include/script.h b/include/script.h
index d2eb575..a9c2921 100644
--- a/include/script.h
+++ b/include/script.h
@@ -62,7 +62,7 @@ public:
/**
* Destructor
*/
- virtual ~Script();
+ virtual ~Script() {}
/**
* Is the script a valid PHP script without syntax errors?
@@ -70,7 +70,7 @@ public:
*/
bool valid() const
{
- return _opcodes != nullptr;
+ return _opcodes.valid();
}
/**
@@ -78,14 +78,26 @@ public:
* The return value of the script is returned
* @return Value
*/
- Value execute() const;
+ Value execute() const
+ {
+ return _opcodes.execute();
+ }
private:
/**
* The opcodes
- * @var zend_op_array
+ * @var Opcodes
+ */
+ Opcodes _opcodes;
+
+ /**
+ * Helper function to compile the source code
+ * @param name name of the script
+ * @param script actual PHP code
+ * @param size length of the string
+ * @return opcodes
*/
- struct _zend_op_array *_opcodes;
+ static struct _zend_op_array *compile(const char *name, const char *phpcode, size_t size);
};