├── .gitignore
├── Makefile
├── README.md
├── src
├── database.cpp
├── database.h
├── environment.cpp
├── environment.h
├── error_messages.h
├── include
│ ├── phpcpp
│ │ ├── argument.h
│ │ ├── base.h
│ │ ├── byref.h
│ │ ├── byval.h
│ │ ├── class.h
│ │ ├── classinfo.h
│ │ ├── environment.h
│ │ ├── error.h
│ │ ├── extension.h
│ │ ├── function.h
│ │ ├── global.h
│ │ ├── globals.h
│ │ ├── hashmember.h
│ │ ├── hiddenpointer.h
│ │ ├── member.h
│ │ ├── members.h
│ │ ├── method.h
│ │ ├── parameters.h
│ │ ├── phpcpp.h
│ │ ├── properties.h
│ │ ├── protected.h
│ │ ├── public.h
│ │ ├── type.h
│ │ └── value.h
│ ├── rocksdb
│ │ ├── arena.h
│ │ ├── c.h
│ │ ├── cache.h
│ │ ├── compaction_filter.h
│ │ ├── comparator.h
│ │ ├── db.h
│ │ ├── env.h
│ │ ├── filter_policy.h
│ │ ├── flush_block_policy.h
│ │ ├── iterator.h
│ │ ├── ldb_tool.h
│ │ ├── memtablerep.h
│ │ ├── merge_operator.h
│ │ ├── options.h
│ │ ├── perf_context.h
│ │ ├── slice.h
│ │ ├── slice_transform.h
│ │ ├── statistics.h
│ │ ├── status.h
│ │ ├── table.h
│ │ ├── table_properties.h
│ │ ├── transaction_log.h
│ │ ├── types.h
│ │ ├── universal_compaction.h
│ │ └── write_batch.h
│ └── utilities
│ │ ├── stackable_db.h
│ │ └── utility_db.h
├── includes.h
└── main.cpp
├── test.sh
├── test
└── test.php
└── test_valgrind.sh
/.gitignore:
--------------------------------------------------------------------------------
1 | *~
2 | bin
3 | my_rock_db
4 | .settings
5 | .cproject
6 | .project
7 |
--------------------------------------------------------------------------------
/Makefile:
--------------------------------------------------------------------------------
1 | SOURCE_DIR=src
2 | CC=g++
3 | OPTIONS=-std=c++11 -fPIC -shared
4 | LIB=-lrocksdb -lpthread -lrt -lsnappy -lgflags -lz -lbz2 -lphpcpp
5 | INC=-I./$(SOURCE_DIR)/include
6 | INPUT=$(SOURCE_DIR)/*.cpp
7 | OUTPUT_DIR=bin
8 | OUTPUT_FILE=rocksdb_php.so
9 |
10 | all:
11 | mkdir -p $(OUTPUT_DIR)
12 | $(CC) $(OPTIONS) $(INPUT) $(LIB) $(INC) -o $(OUTPUT_DIR)/$(OUTPUT_FILE)
13 |
14 | clean:
15 | rm -rf $(OUTPUT_DIR)/$(OUTPUT_FILE)
16 | mkdir -p $(OUTPUT_DIR)
17 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 |
3 | RocksDB PHP Extension
4 | ===
5 | RocksDB is a new embedded database for storing key-value pairs developed by Facebook. This project hosts a PHP extension for RocksDB. More information on RocksDB can be found here:
6 |
7 | https://github.com/facebook/rocksdb
8 |
9 | ###Simple Example
10 | Open(true);
13 | if($result)
14 | die("Unable to open rocksdb!");
15 |
16 | $rocksdb->Put("my_awesome_key", "my_awesome_value");
17 | $value = $rocksdb->Get("my_awesome_key");
18 |
19 | echo $value . "\n";
20 | ?>
21 |
22 | Check `test/test.php` for more examples.
23 |
24 | ##Platforms
25 | RocksDB is said to compile on Linux and Mac OS X. So far, I haven't seen anyone getting it to build on Windows, but it should work.
26 | My build environment is:
27 |
28 | * Ubuntu 13.10 x64
29 | * GCC 4.8.1
30 |
31 | All documentation, instructions and guides assume my configuration. If you were able to get it working on another platform, please let me know.
32 |
33 | ## Notes
34 | I use a custom version of PHP-CPP, because I added basic exception support. Please checkout the fork, until my
35 | pull request has been accepted:
36 |
37 | git clone https://github.com/Photonios/PHP-CPP
38 |
39 | ## Build
40 | #### Building RocksDB PHP Extension
41 | Before trying to build the RocksDB PHP extension, make sure you have installed all dependencies listed below. After that, you can simply checkout the code:
42 |
43 | git clone https://github.com/Photonios/rocksdb-php.git
44 |
45 | Then, run make:
46 |
47 | make
48 |
49 | The PHP extension, which is a shared/dynamiclly linked library can be found in the `bin` directory.
50 |
51 | #### Building PHP
52 | The RocksDB PHP extension is being written against PHP 5.5.3. To be able to build the RocksDB PHP extension, you need to install the following packages:
53 |
54 | sudo apt-get install php5-dev
55 | sudo apt-get install php5-cli
56 |
57 | #### Building PHP-CPP
58 | PHP-CPP is a C++ library which makes developing PHP extensions from C++ possible. It's also, way easier then using the native PHP C API. PHP-CPP can be found here:
59 |
60 | https://github.com/EmielBruijntjes/PHP-CPP
61 |
62 | To build, clone the repository:
63 |
64 | git clone https://github.com/EmielBruijntjes/PHP-CPP.git php-cpp
65 |
66 | Go into the 'php-cpp' directory and run:
67 |
68 | make
69 |
70 | After that, 'libphpcpp.so' should be present in the 'src' directory. To install the library, do one of the following thins:
71 |
72 | * Add the path where you cloned PHP-CPP to LD_LIBRARY_PATH (`export LD_LIBRARY_PATH+=/path/to/php-cpp/src`)
73 | * Create a new file in `/etc/ld.so.conf.d` with the `.conf` extension, where the path to the PHP-CPP src directory is on a single line
74 | * Copy the file `libphpcpp.so` to `/usr/lib`
75 |
76 | #### Building RocksDB
77 | To build the RocksDB PHP Extension, you first need to build RocksDB. Clone the RocksDB git repository using:
78 |
79 | git clone https://github.com/facebook/rocksdb.git
80 |
81 | To be able to build RocksDB, you first need to install all dependencies, this can be done using apt-get:
82 |
83 | sudo apt-get install libsnappy-dev
84 | sudo apt-get install zlib1g-dev
85 | sudo apt-get install libbz2-dev
86 | sudo apt-get install libgflags2
87 | sudo apt-get install libgflags-dev
88 |
89 | Or run:
90 |
91 | sudo apt-get install libsnappy-dev zlib1g-dev libbz2-dev libgflags2 libgflags-dev
92 |
93 | After you've install all dependencies, you can simply run:
94 |
95 | make clean
96 | CFLAGS=-fPIC CXXFLAGS=-fPIC make
97 |
98 | To build and run all unit tests:
99 |
100 | make check
101 |
102 | To install the RocksDB library you can do either of these three things:
103 |
104 | * Add the path where you cloned RocksDB to LD_LIBRARY_PATH (`export LD_LIBRARY_PATH+=/path/to/rocksdb`)
105 | * Create a new file in `/etc/ld.so.conf.d` with the `.conf` extension, where the path to RocksDB is on a single line
106 | * Copy the files `librocksdb.a` and `libmemenv.a` to `/usr/lib`
107 |
108 | Choose whatever you like :)
109 |
110 | ## Installation
111 | After you build the RocksDB PHP extension, you should have a `.so` file in the `bin` directory. Execute:
112 |
113 | php --ini
114 |
115 | To find out where your PHP configuration file is located. It should output something liek this:
116 |
117 | Loaded Configuration File: /etc/php5/cli/php.ini
118 |
119 | Find the line that starts with `extension=`, if it's commented out, uncomment it and set the path to your extension, like this:
120 |
121 | extension=/iam_awesome/cookies/dance/in/the/rain/rocksdb-php.so
122 |
123 | Then, enjoy :D
124 |
--------------------------------------------------------------------------------
/src/database.cpp:
--------------------------------------------------------------------------------
1 | #include "includes.h"
2 | #include "database.h"
3 | #include "error_messages.h"
4 |
5 | namespace RocksDB
6 | {
7 |
8 | Database::Database() :
9 | m_db_path (),
10 | m_rdb (NULL),
11 | m_last_error (),
12 | m_is_open (false)
13 | {
14 |
15 | }
16 |
17 | void Database::__construct(Php::Parameters ¶ms)
18 | {
19 | if(params.empty() || !params[0].isString())
20 | Php::ThrowError(ErrorMessages::InvalidParamExpectedString);
21 |
22 | m_db_path = params[0].stringValue();
23 | }
24 |
25 | void Database::__destruct()
26 | {
27 | Close();
28 | }
29 |
30 | Php::Value Database::Open(Php::Parameters ¶ms)
31 | {
32 | if(IsOpen())
33 | {
34 | m_last_error = ErrorMessages::AlreadyActiveConnection;
35 | return false;
36 | }
37 |
38 | bool create_if_missing = false;
39 |
40 | if(params.empty() || !params[0].isBool())
41 | create_if_missing = false;
42 | else
43 | create_if_missing = params[0].boolValue();
44 |
45 | rocksdb::Options options;
46 | options.create_if_missing = create_if_missing;
47 |
48 | rocksdb::Status status = rocksdb::DB::Open(options, m_db_path, &m_rdb);
49 |
50 | if(!status.ok())
51 | {
52 | m_last_error = status.ToString();
53 | m_is_open = false;
54 | return false;
55 | }
56 |
57 | m_is_open = true;
58 |
59 | return true;
60 | }
61 |
62 | Php::Value Database::GetLastError()
63 | {
64 | return m_last_error;
65 | }
66 |
67 | Php::Value Database::IsOpen()
68 | {
69 | if(m_rdb == NULL)
70 | return false;
71 |
72 | return m_is_open;
73 | }
74 |
75 | Php::Value Database::GetDbPath()
76 | {
77 | return m_db_path;
78 | }
79 |
80 | Php::Value Database::Close()
81 | {
82 | if(m_rdb == NULL)
83 | {
84 | m_last_error = ErrorMessages::NoActiveConnection;
85 | return false;
86 | }
87 |
88 | delete m_rdb;
89 | return true;
90 | }
91 |
92 | Php::Value Database::Get(Php::Parameters ¶ms)
93 | {
94 | if(params.empty())
95 | Php::ThrowError(ErrorMessages::ExpectedOneParameter);
96 |
97 | if(!IsOpen())
98 | {
99 | m_last_error = ErrorMessages::NoActiveConnection;
100 | return false;
101 | }
102 |
103 | std::string key = params[0].stringValue();
104 | std::string value;
105 |
106 | rocksdb::Status status = m_rdb->Get(rocksdb::ReadOptions(), key, &value);
107 |
108 | if(!status.ok())
109 | {
110 | m_last_error = status.ToString();
111 | return false;
112 | }
113 |
114 | return value;
115 | }
116 |
117 | Php::Value Database::Put(Php::Parameters ¶ms)
118 | {
119 | if(params.empty() || params.size() < 2)
120 | Php::ThrowError(ErrorMessages::ExpectedTwoParamters);
121 |
122 | if(!IsOpen())
123 | {
124 | m_last_error = ErrorMessages::NoActiveConnection;
125 | return false;
126 | }
127 |
128 | std::string key = params[0].stringValue();
129 | std::string value = params[1].stringValue();
130 |
131 | m_rdb->Put(rocksdb::WriteOptions(), key, value);
132 |
133 | return true;
134 | }
135 |
136 | } // namespace RocksDB
137 |
--------------------------------------------------------------------------------
/src/database.h:
--------------------------------------------------------------------------------
1 | #ifndef ROCKSDB_DATABSE_H_
2 | #define ROCKSDB_DATABSE_H_
3 |
4 | namespace RocksDB
5 | {
6 |
7 | /*!
8 | * \brief The PHP wrapper for the rocksdb::DB class. Represents a single RocksDB database.
9 | * \author Swen Kooij
10 | */
11 | class Database : public Php::Base
12 | {
13 | public:
14 | /*!
15 | * \brief Initializes a new instance of the Database class.
16 | * \note This is NOT the PHP constructor.
17 | */
18 | Database();
19 |
20 | /*!
21 | * \brief The PHP constructor, the method that gets called when
22 | * a new instance of this object is being created.
23 | * \param db_path The path to a RocksDB file that needs to be opened.
24 | */
25 | void __construct(Php::Parameters ¶ms);
26 |
27 | /*!
28 | * \brief The PHP destructor, the method that gets called
29 | * when this instance is destroyed.
30 | */
31 | void __destruct();
32 |
33 | /*!
34 | * \brief Opens the 'connection' with the earlier specified RocksDB file.
35 | * \param create_if_missing A boolean indicating whether the database should be created it it does not exists.
36 | * \returns A boolean indicating whether opening the database successful.
37 | */
38 | Php::Value Open(Php::Parameters ¶ms);
39 |
40 | /*!
41 | * \brief Gets the last error that was thrown, as a string. A textual message
42 | * describing the error.
43 | * \returns A string, describing the error. If no error occurred, an empty string will be returned.
44 | */
45 | Php::Value GetLastError();
46 |
47 | /*!
48 | * \brief Checks whether there's an active connection with a database.
49 | * \returns A boolean indicating whether there's an active connection with a database.
50 | */
51 | Php::Value IsOpen();
52 |
53 | /*!
54 | * \brief Gets the path to the currently opened database.
55 | */
56 | Php::Value GetDbPath();
57 |
58 | /*!
59 | * \brief Closes the current connection to the database.
60 | * \returns A boolean indicating whether closing the connection was sucessful, it
61 | * returns false when there's no active connection.
62 | */
63 | Php::Value Close();
64 |
65 | /*!
66 | * \brief Gets the value of the specified key.
67 | * \param key The name of the key to get the value for, this has to be a string.
68 | * \returns The textual value stored under the specified key, if no key exists with
69 | * the specified name,false will be returned. If there are any
70 | * other reasons why you're getting false, check GetLastError();
71 | */
72 | Php::Value Get(Php::Parameters ¶ms);
73 |
74 | /*!
75 | * \brief Sets the value of the specified key.
76 | * \param key The name of the key to put the value of.
77 | * \param value The new value for this key.
78 | * \returns A boolean indicating whether the put was sucessful, when it failed,
79 | * there's probably no active db connection, check GetLastError();
80 | */
81 | Php::Value Put(Php::Parameters ¶ms);
82 |
83 | private:
84 | /*!
85 | * \brief The path to the RocksDB database this instance represents.
86 | */
87 | std::string m_db_path;
88 |
89 | /*!
90 | * \brief Holds the textual representation of the error that was last thrown.
91 | */
92 | std::string m_last_error;
93 |
94 | /*!
95 | * \brief Represents the 'connection' with the selected RocksDB file.
96 | */
97 | rocksdb::DB* m_rdb;
98 |
99 | /*!
100 | * \brief Indicates whether there's an active 'connection' with a database.
101 | */
102 | bool m_is_open;
103 | };
104 |
105 | } // namespace RocksDB
106 |
107 | #endif /* ROCKSDB_DATABSE_H_ */
108 |
--------------------------------------------------------------------------------
/src/environment.cpp:
--------------------------------------------------------------------------------
1 | #include "includes.h"
2 | #include "environment.h"
3 |
4 |
--------------------------------------------------------------------------------
/src/environment.h:
--------------------------------------------------------------------------------
1 | #ifndef ROCKSDB_ENVIRONMENT_H_
2 | #define ROCKSDB_ENVIRONMENT_H_
3 |
4 | namespace RocksDB
5 | {
6 |
7 | /*!
8 | * \brief Implements the 'Php::Environment' class, allows access to the environment,
9 | * and thus allows us to set global variables/constants.
10 | * \author Swen Kooij
11 | */
12 | class Environment : public Php::Environment
13 | {
14 | };
15 |
16 | } // namespace RocksDB
17 |
18 | #endif /* ROCKSDB_ENVIRONMENT_H_ */
19 |
--------------------------------------------------------------------------------
/src/error_messages.h:
--------------------------------------------------------------------------------
1 | #ifndef ROCKSDB_ERROR_MESSAGES_H_
2 | #define ROCKSDB_ERROR_MESSAGES_H_
3 |
4 | namespace RocksDB
5 | {
6 |
7 | namespace ErrorMessages
8 | {
9 | const std::string InvalidParamExpectedString = "Invalid parameter 0, expected string.";
10 | const std::string AlreadyActiveConnection = "There already is an active connection with a database.";
11 | const std::string NoActiveConnection = "There is no active database connection to close.";
12 | const std::string ExpectedOneParameter = "Expected at least one parameter.";
13 | const std::string ExpectedTwoParamters = "Expected at least two parameters.";
14 | }
15 |
16 | } // namespace RocksDB
17 |
18 | #endif /* ROCKSDB_ERROR_MESSAGES_H_ */
19 |
--------------------------------------------------------------------------------
/src/include/phpcpp/argument.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Argument.h
3 | *
4 | * Class holds information about an argument that is passed to a function.
5 | * You'll need this class when you're defining your own functions.
6 | *
7 | * The constructor of the argument is protected. Use the ByVal or ByRef
8 | * classes instead.
9 | *
10 | * @author Emiel Bruijntjes
11 | * @copyright 2013 Copernica BV
12 | */
13 |
14 | /**
15 | * Forward declaration
16 | */
17 | struct _zend_arg_info;
18 |
19 | /**
20 | * Set up namespace
21 | */
22 | namespace Php {
23 |
24 | /**
25 | * Class definition
26 | */
27 | class Argument
28 | {
29 | public:
30 | /**
31 | * Copy constructor
32 | * @param argument
33 | */
34 | Argument(const Argument &argument);
35 |
36 | /**
37 | * Move constructor
38 | * @param argument
39 | */
40 | Argument(Argument &&argument);
41 |
42 | /**
43 | * Destructor
44 | */
45 | virtual ~Argument();
46 |
47 | protected:
48 | /**
49 | * Constructor
50 | * @param name Name of the argument
51 | * @param type Argument type
52 | * @param required Is this argument required?
53 | * @param byref Is this a reference argument
54 | */
55 | Argument(const char *name, Type type, bool required = true, bool byref = false);
56 |
57 | /**
58 | * Constructor
59 | * @param name Name of the argument
60 | * @param classname Name of the class
61 | * @param nullable Can it be null?
62 | * @param required Is this argument required?
63 | * @param byref Is this a reference argument?
64 | */
65 | Argument(const char *name, const char *classname, bool nullable = true, bool required = true, bool byref = false);
66 |
67 | public:
68 | /**
69 | * Fill an arg_info structure with data
70 | * @param info
71 | * @internal
72 | */
73 | void fill(struct _zend_arg_info *info) const;
74 |
75 | private:
76 | /**
77 | * The argument info
78 | * @var zend_arg_info
79 | */
80 | struct _zend_arg_info *_info;
81 |
82 | /**
83 | * Is this a required argument
84 | * @var bool
85 | */
86 | bool _required;
87 | };
88 |
89 | /**
90 | * End of namespace
91 | */
92 | }
93 |
94 |
--------------------------------------------------------------------------------
/src/include/phpcpp/base.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Base class for defining your own objects
3 | *
4 | * @author Emiel Bruijntjes
5 | * @copyright 2013 Copernica BV
6 | */
7 |
8 | /**
9 | * Namespace
10 | */
11 | namespace Php {
12 |
13 | /**
14 | * Class definition
15 | */
16 | class Base
17 | {
18 | public:
19 | /**
20 | * Constructor
21 | */
22 | Base() {}
23 |
24 | /**
25 | * Virtual destructor
26 | */
27 | virtual ~Base() {}
28 |
29 | /**
30 | * The pseudo constructor that is called from PHP after the object is constructed
31 | * @param environment
32 | * @param parameters
33 | */
34 | virtual void __construct(Environment &environment, const Parameters ¶meters)
35 | {
36 | // call all other possible implementations
37 | __construct(environment);
38 | __construct(parameters);
39 | __construct();
40 | }
41 |
42 | /**
43 | * The pseudo constructor that is called from PHP after the object is constructed
44 | * @param environment
45 | */
46 | virtual void __construct(Environment &environment) {}
47 |
48 | /**
49 | * The pseudo constructor that is called from PHP after the object is constructed
50 | * @param parameters
51 | */
52 | virtual void __construct(const Parameters ¶meters) {}
53 |
54 | /**
55 | * The pseudo constructor that is called from PHP after the object is constructed
56 | */
57 | virtual void __construct() {}
58 |
59 | /**
60 | * The pseudo destructor that is called from PHP right before the object is destructed
61 | * @param environment
62 | */
63 | virtual void __destruct(Environment &environment)
64 | {
65 | // call the other implementation
66 | __destruct();
67 | }
68 |
69 | /**
70 | * The pseudo destructor that is called from PHP right before the object is destructed
71 | */
72 | virtual void __destruct() {}
73 |
74 | /**
75 | * Get access to a property by name
76 | * @param string
77 | * @return Property
78 | */
79 | // Property operator[](const char *name);
80 |
81 | /**
82 | * Alternative way to access a property
83 | * @param string
84 | * @return Property
85 | */
86 | // Property operator[](const std::string &name);
87 |
88 | protected:
89 | /**
90 | * All properties of the object
91 | * @var Properties
92 | */
93 | // Properties _properties;
94 |
95 | private:
96 | };
97 |
98 | /**
99 | * Definition of a method
100 | */
101 | typedef void (Base::*method_callback_0)();
102 | typedef void (Base::*method_callback_1)(Parameters &);
103 | typedef void (Base::*method_callback_2)(Environment &);
104 | typedef void (Base::*method_callback_3)(Environment &, Parameters &);
105 | typedef Value (Base::*method_callback_4)();
106 | typedef Value (Base::*method_callback_5)(Parameters &);
107 | typedef Value (Base::*method_callback_6)(Environment &);
108 | typedef Value (Base::*method_callback_7)(Environment &, Parameters &);
109 |
110 | /**
111 | * End of namespace
112 | */
113 | }
114 |
115 |
--------------------------------------------------------------------------------
/src/include/phpcpp/byref.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ByRef.h
3 | *
4 | * Overridden Argument class to specify by-reference function arguments
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Class definition
17 | */
18 | class ByRef : public Argument
19 | {
20 | public:
21 | /**
22 | * Constructor
23 | * @param name Name of the argument
24 | * @param type Argument type
25 | * @param required Is this argument required?
26 | */
27 | ByRef(const char *name, Type type, bool required = true) : Argument(name, type, required, true) {}
28 |
29 | /**
30 | * Constructor
31 | * @param name Name of the argument
32 | * @param classname Name of the class
33 | * @param nullable Can it be null?
34 | * @param required Is this argument required?
35 | */
36 | ByRef(const char *name, const char *classname, bool nullable = true, bool required = true) : Argument(name, classname, nullable, required, true) {}
37 |
38 | /**
39 | * Copy constructor
40 | * @param argument
41 | */
42 | ByRef(const ByRef &argument) : Argument(argument) {}
43 |
44 | /**
45 | * Move constructor
46 | * @param argument
47 | */
48 | ByRef(ByRef &&argument) : Argument(argument) {}
49 |
50 | /**
51 | * Destructor
52 | */
53 | virtual ~ByRef() {}
54 | };
55 |
56 | /**
57 | * End of namespace
58 | */
59 | }
60 |
61 |
--------------------------------------------------------------------------------
/src/include/phpcpp/byval.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ByVal.h
3 | *
4 | * Overridden Argument class to specify by-value function arguments
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Class definition
17 | */
18 | class ByVal : public Argument
19 | {
20 | public:
21 | /**
22 | * Constructor
23 | * @param name Name of the argument
24 | * @param type Argument type
25 | * @param required Is this argument required?
26 | */
27 | ByVal(const char *name, Type type, bool required = true) : Argument(name, type, required, false) {}
28 |
29 | /**
30 | * Constructor
31 | * @param name Name of the argument
32 | * @param classname Name of the class
33 | * @param nullable Can it be null?
34 | * @param required Is this argument required?
35 | */
36 | ByVal(const char *name, const char *classname, bool nullable = true, bool required = true) : Argument(name, classname, nullable, required, false) {}
37 |
38 | /**
39 | * Copy constructor
40 | * @param argument
41 | */
42 | ByVal(const ByVal &argument) : Argument(argument) {}
43 |
44 | /**
45 | * Move constructor
46 | * @param argument
47 | */
48 | ByVal(ByVal &&argument) : Argument(argument) {}
49 |
50 | /**
51 | * Destructor
52 | */
53 | virtual ~ByVal() {}
54 | };
55 |
56 | /**
57 | * End of namespace
58 | */
59 | }
60 |
61 |
--------------------------------------------------------------------------------
/src/include/phpcpp/class.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Class.h
3 | *
4 | * When a class is registered in the extension, you need this helper class
5 | * for that.
6 | *
7 | * The use of it is simple:
8 | *
9 | * Extension::add(Class);
10 | *
11 | * Note that YourClass must extend from Php::Object
12 | *
13 | * @author Emiel Bruijntjes
14 | * @copyright 2013 Copernica BV
15 | */
16 |
17 | /**
18 | * Forward declarations
19 | */
20 | struct _zend_class_entry;
21 |
22 | /**
23 | * Set up namespace
24 | */
25 | namespace Php {
26 |
27 | /**
28 | * Class definition of the class
29 | */
30 | template
31 | class Class
32 | {
33 | public:
34 | /**
35 | * Constructor
36 | */
37 | Class() {}
38 |
39 | /**
40 | * Constructor with initializer list to define the properties
41 | * @param members
42 | */
43 | Class(const std::initializer_list &members) : _members(members) {}
44 |
45 | /**
46 | * Move constructor
47 | * @param that
48 | */
49 | Class(Class &&that) : _members(std::move(that._members)) {}
50 |
51 | /**
52 | * Copy constructor
53 | */
54 | Class(const Class &that) : _members(that._members) {}
55 |
56 | /**
57 | * Destructor
58 | */
59 | virtual ~Class() {}
60 |
61 | /**
62 | * Construct an instance
63 | * @return Base
64 | */
65 | Base* construct()
66 | {
67 | // allocate the object
68 | return new T();
69 | }
70 |
71 | /**
72 | * Initialize the class
73 | * This will declare all members
74 | * @param entry
75 | */
76 | void initialize(struct _zend_class_entry *entry)
77 | {
78 | // loop through the members
79 | for (auto iter = _members.begin(); iter != _members.end(); iter++)
80 | {
81 | iter->declare(entry);
82 | }
83 | }
84 |
85 | /**
86 | * Retrieve the functions
87 | * @param classname
88 | * @return zend_function_entry*
89 | */
90 | struct _zend_function_entry *methods(const char *classname)
91 | {
92 | return _members.methods(classname);
93 | }
94 |
95 | protected:
96 | /**
97 | * The initial arguments
98 | * @var Members
99 | */
100 | Members _members;
101 |
102 | };
103 |
104 | /**
105 | * End of namespace
106 | */
107 | }
108 |
109 |
--------------------------------------------------------------------------------
/src/include/phpcpp/classinfo.h:
--------------------------------------------------------------------------------
1 | /**
2 | * ClassInfo.h
3 | *
4 | * Internal class that is constructed by the library and that contains
5 | * the information about a class, including its name.
6 | *
7 | * Users of the PHP-CPP libraries are not supposed to interact with
8 | * this class, or instantiate objects of this class.
9 | *
10 | * @author Emiel Bruijntjes
11 | * @copyright 2013 Copernica BV
12 | */
13 |
14 | /**
15 | * Forward declarations
16 | */
17 | struct _zend_class_entry;
18 |
19 | /**
20 | * Namespace
21 | */
22 | namespace Php {
23 |
24 | /**
25 | * Forward declarations
26 | */
27 | class InternalFunction;
28 |
29 | /**
30 | * Virtual base class of the classInfo
31 | *
32 | * We need this virtual base class to store pointers to class objects,
33 | * without knowing in advance what sort of object they will hold
34 | */
35 | class _ClassInfo
36 | {
37 | public:
38 | /**
39 | * Constructor
40 | * @param name
41 | */
42 | _ClassInfo(const char *name);
43 |
44 | /**
45 | * Destructor
46 | */
47 | virtual ~_ClassInfo();
48 |
49 | /**
50 | * Initialize the class
51 | */
52 | void initialize();
53 |
54 | /**
55 | * Construct the C++ object
56 | * @return Base
57 | */
58 | virtual Base *construct() = 0;
59 |
60 | /**
61 | * Initialize the class
62 | * @param entry
63 | */
64 | virtual void initialize(struct _zend_class_entry *entry) = 0;
65 |
66 | /**
67 | * Retrieve the methods
68 | * @return zend_function_entry[]
69 | */
70 | virtual struct _zend_function_entry *methods() = 0;
71 |
72 | protected:
73 | /**
74 | * The class entry
75 | * @var zend_class_entry
76 | */
77 | struct _zend_class_entry *_entry;
78 |
79 | /**
80 | * The name
81 | * @var string
82 | */
83 | std::string _name;
84 |
85 | /**
86 | * Constructor function
87 | * @var InternalFunction
88 | */
89 | InternalFunction *_constructor;
90 |
91 | /**
92 | * Destructor function
93 | * @var InternalFunction
94 | */
95 | InternalFunction *_destructor;
96 |
97 | };
98 |
99 | /**
100 | * Class definition
101 | */
102 | template
103 | class ClassInfo : public _ClassInfo
104 | {
105 | public:
106 | /**
107 | * Constructor
108 | * @param name Name of the class
109 | * @param type The class type
110 | */
111 | ClassInfo(const char *name, const Class &type) : _ClassInfo(name), _type(type)
112 | {
113 | }
114 |
115 | /**
116 | * Destructor
117 | */
118 | virtual ~ClassInfo() {}
119 |
120 | /**
121 | * Construct the object
122 | * @return Base
123 | */
124 | virtual Base *construct()
125 | {
126 | return _type.construct();
127 | }
128 |
129 | /**
130 | * Initialize the class
131 | * @param entry
132 | */
133 | virtual void initialize(struct _zend_class_entry *entry)
134 | {
135 | // pass to the entry
136 | _type.initialize(entry);
137 | }
138 |
139 | /**
140 | * Retrieve the methods
141 | * @return zend_function_entry[]
142 | */
143 | virtual struct _zend_function_entry *methods()
144 | {
145 | // ask class object
146 | return _type.methods(_name.c_str());
147 | }
148 |
149 | private:
150 | /**
151 | * The class object
152 | * @var Class
153 | */
154 | Class _type;
155 |
156 | };
157 |
158 | /**
159 | * End of namespace
160 | */
161 | }
162 |
163 |
--------------------------------------------------------------------------------
/src/include/phpcpp/environment.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Environment.h
3 | *
4 | * During the lifetime of the extension, multiple requests can be handled
5 | * by it. For every request that is handled, an environment object is created.
6 | *
7 | * The base class for the environment is defined in this file. If you'd like
8 | * to add state variables to the environment you can override this class and
9 | * add the extra features you'd like. If you override this method, you should
10 | * also override Extension::createEnvironment() to return an instance of a
11 | * different class.
12 | *
13 | * @author Emiel Bruijntjes
14 | * @copyright 2013 Copernica BV
15 | */
16 |
17 | /**
18 | * Set up namespace
19 | */
20 | namespace Php {
21 |
22 | /**
23 | * Forward definitions
24 | */
25 | class Extension;
26 | class Global;
27 |
28 | /**
29 | * Class definition
30 | */
31 | class Environment
32 | {
33 | public:
34 | /**
35 | * Constructor
36 | * @param extension
37 | */
38 | Environment(Extension *extension) : _extension(extension) {}
39 |
40 | /**
41 | * Disable copy and move operations
42 | */
43 | Environment(const Environment &environment) = delete;
44 | Environment(Environment &&environment) = delete;
45 |
46 | /**
47 | * Destructor
48 | */
49 | virtual ~Environment() {}
50 |
51 | /**
52 | * Initialize the request
53 | *
54 | * This method is called directly after the object was destructed. You can
55 | * override this method to add your own initialization code.
56 | *
57 | * @return bool
58 | */
59 | virtual bool initialize()
60 | {
61 | return true;
62 | }
63 |
64 | /**
65 | * Finalize the request
66 | *
67 | * This method is called right before the object is destructed. Note that
68 | * the object is going to be destructed anyway, even if this method returns
69 | * false
70 | *
71 | * @return bool
72 | */
73 | virtual bool finalize()
74 | {
75 | return true;
76 | }
77 |
78 | /**
79 | * Get access to the user supplied data
80 | * @return void*
81 | */
82 | virtual void *data()
83 | {
84 | return _data;
85 | }
86 |
87 | /**
88 | * Change the user supplied data
89 | * @param data
90 | */
91 | virtual void setData(void *data)
92 | {
93 | _data = data;
94 | }
95 |
96 | /**
97 | * Get access to a global variable
98 | * @param name
99 | * @return Global
100 | */
101 | Global operator[](const char *name);
102 |
103 | /**
104 | * Get access to a global variable
105 | * @param name
106 | * @return Global
107 | */
108 | Global operator[](const std::string &name);
109 |
110 | /**
111 | * Call a function in PHP
112 | * We have ten variants of this function, depending on the number of parameters
113 | * @param name Name of the function
114 | * @return Value
115 | */
116 | Value call(const Value &name);
117 | Value call(const Value &name, Value p0);
118 | Value call(const Value &name, Value p0, Value p1);
119 | Value call(const Value &name, Value p0, Value p1, Value p2);
120 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3);
121 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4);
122 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5);
123 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6);
124 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7);
125 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8);
126 | Value call(const Value &name, Value p0, Value p1, Value p2, Value p3, Value p4, Value p5, Value p6, Value p7, Value p8, Value p9);
127 |
128 | private:
129 | /**
130 | * Call function with a number of parameters
131 | * @param name Function name
132 | * @param argc Number of parameters
133 | * @param argv The parameters
134 | * @return Value
135 | */
136 | Value exec(const Value &name, int argc, struct _zval_struct ***params);
137 |
138 | protected:
139 | /**
140 | * The extension that this environment belongs to
141 | * @var Extension*
142 | */
143 | Extension *_extension;
144 |
145 | /**
146 | * Pointer to user supplied data
147 | * @var void*
148 | */
149 | void *_data = NULL;
150 | };
151 |
152 | /**
153 | * End of namespace
154 | */
155 | }
156 |
157 |
--------------------------------------------------------------------------------
/src/include/phpcpp/error.h:
--------------------------------------------------------------------------------
1 | /**
2 | * error.h
3 | *
4 | * Contains 'static' function that can be used to report errors. For now throwing an Exception (zend default)
5 | * is enough. In the future, we might want to implement extensive exception support.
6 | *
7 | * @author Swen Kooij
8 | * @copyright 2013 Swen Kooij
9 | */
10 |
11 | /**
12 | * Setup namespace
13 | */
14 | namespace Php
15 | {
16 | /**
17 | * Throws an Exception (Zend Default) with the specified message.
18 | * @param error_msg A string, describing the reason of failure.
19 | */
20 | void ThrowError(const std::string &error_msg);
21 | }
22 |
--------------------------------------------------------------------------------
/src/include/phpcpp/extension.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Extension.h
3 | *
4 | * The extension class is the starting point of your PHP extension. This class
5 | * is instantiated the moment the PHP engine starts - for example when the
6 | * apache process starts - and will be used for all subsequent requests that
7 | * are handled by Apache.
8 | *
9 | * For some environments (for example CLI scripts and FastCGI calls) only one
10 | * request is handled by an extension instance, but for others (when PHP runs
11 | * as module in a webserver) many requests are handled by the same extension
12 | * instance.
13 | *
14 | * This is a template class. You need to pass in the type of an object
15 | * that you use for storing request specific state information.
16 | *
17 | * @author Emiel Bruijntjes
18 | * @copyright 2013 Copernica BV
19 | */
20 |
21 | /**
22 | * Structures referenced in this class
23 | */
24 | struct _zend_module_entry;
25 |
26 | /**
27 | * Set up namespace
28 | */
29 | namespace Php {
30 |
31 | /**
32 | * Optional callback types for starting and stopping the request
33 | */
34 | typedef bool (*request_callback)(Environment &);
35 |
36 | /**
37 | * A couple of predefined native callback functions that can be registered.
38 | * These are functions that optional accept a Request and/or Parameters object,
39 | * and that either return void or a Value object.
40 | */
41 | typedef void (*native_callback_0)();
42 | typedef void (*native_callback_1)(Parameters &);
43 | typedef void (*native_callback_2)(Environment &);
44 | typedef void (*native_callback_3)(Environment &, Parameters &);
45 | typedef Value (*native_callback_4)();
46 | typedef Value (*native_callback_5)(Parameters &);
47 | typedef Value (*native_callback_6)(Environment &);
48 | typedef Value (*native_callback_7)(Environment &, Parameters &);
49 |
50 | /**
51 | * Class definition
52 | */
53 | class Extension
54 | {
55 | public:
56 | /**
57 | * Constructor that defines a number of functions right away
58 | * @param name Extension name
59 | * @param version Extension version string
60 | * @param callback Function that is called when request starts
61 | * @param callback Function that is called when request ends
62 | */
63 | Extension(const char *name = NULL, const char *version = NULL, request_callback start = NULL, request_callback stop = NULL);
64 |
65 | /**
66 | * No copy'ing and no moving
67 | */
68 | Extension(const Extension &extension) = delete;
69 | Extension(Extension &&extension) = delete;
70 |
71 | /**
72 | * Destructor
73 | */
74 | virtual ~Extension();
75 |
76 | /**
77 | * Initialize the extension.
78 | *
79 | * This method is called after the extension has been loaded, constructed
80 | * and after the compatibility has been checked, but before the requests
81 | * are handled. You can override this method to add your own initialization.
82 | *
83 | * The default behavior of this function is to enable all classes that are
84 | * defined in this extension, so that they are also available in PHP.
85 | *
86 | * The method should return true on success, and false on failure (in which
87 | * case the extension will not be used)
88 | *
89 | * @return bool
90 | */
91 | virtual bool initialize();
92 |
93 | /**
94 | * Finalize the extension
95 | *
96 | * This method gets called after all requests were handled, and right before
97 | * the Apache module or CLI script will exit. You can override it to add
98 | * your own cleanup code.
99 | *
100 | * @return bool
101 | */
102 | virtual bool finalize()
103 | {
104 | return true;
105 | }
106 |
107 | /**
108 | * Create a new environment
109 | *
110 | * You can override this method if you've created your own environment class,
111 | * and you'd like to use an instance of that class instead. The returned
112 | * object must have been created on the heap.
113 | *
114 | * @return Environment*
115 | */
116 | virtual Environment *createEnvironment()
117 | {
118 | // allocate the environment
119 | return new Environment(this);
120 | }
121 |
122 | /**
123 | * Destruct an environment
124 | *
125 | * This is the counterpart of the createEnvironment method.
126 | *
127 | * @param Environment
128 | */
129 | virtual void deleteEnvironment(Environment *environment)
130 | {
131 | // destruct the environment
132 | delete environment;
133 | }
134 |
135 | /**
136 | * Start a request
137 | *
138 | * This method is called when the zend engine is about to start a new
139 | * request. Internally, it calls the request() method to instantiate
140 | * a new request object, and after that it initializes the request.
141 | *
142 | * @return boolean
143 | */
144 | virtual bool startRequest(Environment &environment)
145 | {
146 | // ok if no callback was set
147 | if (!_start) return true;
148 |
149 | // call the callback function
150 | return _start(environment);
151 | }
152 |
153 | /**
154 | * End a request
155 | *
156 | * This method is called when the Zend engine is ready with a request.
157 | * Internally, it destructs the request
158 | *
159 | * @return boolean
160 | */
161 | virtual bool endRequest(Environment &environment)
162 | {
163 | // ok if no callback is set
164 | if (!_stop) return true;
165 |
166 | // call callback
167 | return _stop(environment);
168 | }
169 |
170 | /**
171 | * Add a function to the extension
172 | *
173 | * It is only possible to create functions during the initialization of
174 | * the library, before the Extension::module() method is called.
175 | *
176 | * Note that the function must have been allocated on the HEAP (using
177 | * "new") and that the object will be destructed (using "delete")
178 | * by the extension object (you thus do not have to destruct it
179 | * yourself!)
180 | *
181 | * @param function The function to add
182 | * @return Function The added function
183 | */
184 | Function *add(Function *function);
185 |
186 | /**
187 | * Add a native function directly to the extension
188 | * @param name Name of the function
189 | * @param function The function to add
190 | * @param arguments Optional argument specification
191 | * @return Function The added function
192 | */
193 | Function *add(const char *name, native_callback_0 function, const std::initializer_list &arguments = {});
194 | Function *add(const char *name, native_callback_1 function, const std::initializer_list &arguments = {});
195 | Function *add(const char *name, native_callback_2 function, const std::initializer_list &arguments = {});
196 | Function *add(const char *name, native_callback_3 function, const std::initializer_list &arguments = {});
197 | Function *add(const char *name, native_callback_4 function, const std::initializer_list &arguments = {});
198 | Function *add(const char *name, native_callback_5 function, const std::initializer_list &arguments = {});
199 | Function *add(const char *name, native_callback_6 function, const std::initializer_list &arguments = {});
200 | Function *add(const char *name, native_callback_7 function, const std::initializer_list &arguments = {});
201 |
202 | /**
203 | * Add a native class to the extension
204 | * @param name Name of the class
205 | * @param type The class implementation
206 | */
207 | template
208 | void add(const char *name, const Class &type)
209 | {
210 | // construct info
211 | ClassInfo *info = new ClassInfo(name, type);
212 |
213 | // add class
214 | _classes.insert(std::unique_ptr<_ClassInfo>(info));
215 | }
216 |
217 | /**
218 | * Retrieve the module entry
219 | *
220 | * This is the memory address that should be exported by the get_module()
221 | * function.
222 | *
223 | * @return _zend_module_entry
224 | */
225 | _zend_module_entry *module();
226 |
227 |
228 | private:
229 | /**
230 | * Set of function objects defined in the library
231 | * @var set
232 | */
233 | std::set> _functions;
234 |
235 | /**
236 | * Set of classes defined in the library
237 | * @var set
238 | */
239 | std::set> _classes;
240 |
241 | /**
242 | * The information that is passed to the Zend engine
243 | *
244 | * Although it would be slightly faster to not make this a pointer, this
245 | * would require that client code also includes the PHP header files, which
246 | * we try to prevent with the PHP-CPP library, so we allocate it dynamically.
247 | *
248 | * @var zend_module_entry
249 | */
250 | _zend_module_entry *_entry;
251 |
252 | /**
253 | * Callback that is called before each request
254 | * @var request_callback
255 | */
256 | request_callback _start;
257 |
258 | /**
259 | * Callback that is called after each request
260 | * @var request_callback
261 | */
262 | request_callback _stop;
263 |
264 | };
265 |
266 | /**
267 | * End of namespace
268 | */
269 | }
270 |
271 |
272 |
--------------------------------------------------------------------------------
/src/include/phpcpp/function.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Function.h
3 | *
4 | * Object represents a callable function that is defined with the CPP API.
5 | * After you've instantiated the extension, you can add function objects to
6 | * it.
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2013 Copernica BV
10 | */
11 |
12 | /**
13 | * Forward definitions
14 | */
15 | struct _zend_function_entry;
16 | struct _zend_internal_function_info;
17 |
18 | /**
19 | * Set up namespace
20 | */
21 | namespace Php {
22 |
23 | /**
24 | * Class definition
25 | */
26 | class Function
27 | {
28 | public:
29 | /**
30 | * Constructor
31 | * @param name Name of the function
32 | * @param min Min number of arguments
33 | * @param max Max number of arguments
34 | */
35 | Function(const char *name, const std::initializer_list &arguments = {});
36 |
37 | /**
38 | * No copy and move constructors
39 | * @param function The other function
40 | */
41 | Function(const Function &function) = delete;
42 | Function(Function &&function) = delete;
43 |
44 | /**
45 | * Destructor
46 | */
47 | virtual ~Function();
48 |
49 | /**
50 | * No assignment operator
51 | * @param function The other function
52 | * @return Function
53 | */
54 | Function &operator=(const Function &function) = delete;
55 |
56 | /**
57 | * Comparison
58 | * @param function The other function
59 | * @return bool
60 | */
61 | bool operator<(const Function &function) const
62 | {
63 | return strcmp(name(), function.name()) < 0;
64 | }
65 |
66 | /**
67 | * Comparison
68 | * @param function The other function
69 | * @return bool
70 | */
71 | bool operator>(const Function &function) const
72 | {
73 | return strcmp(name(), function.name()) > 0;
74 | }
75 |
76 | /**
77 | * Comparison
78 | * @param function The other function
79 | * @return bool
80 | */
81 | bool operator==(const Function &function) const
82 | {
83 | return strcmp(name(), function.name()) == 0;
84 | }
85 |
86 | /**
87 | * Function name
88 | * @return const char *
89 | */
90 | const char *name() const
91 | {
92 | return _ptr.text();
93 | }
94 |
95 | /**
96 | * Method that gets called every time the function is executed
97 | * @param environment Environment object
98 | * @param params The parameters that were passed
99 | * @return Variable Return value
100 | */
101 | virtual Value invoke(Environment &environment, Parameters ¶ms)
102 | {
103 | return nullptr;
104 | }
105 |
106 |
107 | protected:
108 | /**
109 | * Suggestion for the return type
110 | * @var Type
111 | */
112 | Type _type = nullType;
113 |
114 | /**
115 | * Required number of arguments
116 | * @var integer
117 | */
118 | int _required;
119 |
120 | /**
121 | * Total number of arguments
122 | * @var integer
123 | */
124 | int _argc;
125 |
126 | /**
127 | * The arguments
128 | * @var zend_arg_info[]
129 | */
130 | struct _zend_arg_info *_argv;
131 |
132 | /**
133 | * The object address is stored in a hidden pointer, so that we have access to the function object
134 | * @var HiddenPointer
135 | */
136 | HiddenPointer _ptr;
137 |
138 | protected:
139 | /**
140 | * Fill a function entry
141 | * @param entry Entry to be filled
142 | * @param classname Optional class name
143 | * @param pub Is this a public property?
144 | */
145 | void fill(struct _zend_function_entry *entry, const char *classname=NULL, bool pub=true) const;
146 |
147 | /**
148 | * Fill function info
149 | * @param info Info object to be filled
150 | * @param classname Optional class name
151 | */
152 | void fill(struct _zend_internal_function_info *info, const char *classname=NULL) const;
153 |
154 | /**
155 | * Extension has access to the private members
156 | */
157 | friend class Extension;
158 |
159 | };
160 |
161 | /**
162 | * End of namespace
163 | */
164 | }
165 |
166 |
--------------------------------------------------------------------------------
/src/include/phpcpp/global.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Global variable
3 | *
4 | * A global variable is a value that - once updated - also updates
5 | * the global scope
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2013 Copernica BV
9 | */
10 |
11 | /**
12 | * Forward definitions
13 | */
14 | struct _zval_struct;
15 |
16 | /**
17 | * Namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Class definition
23 | */
24 | class Global : public Value
25 | {
26 | public:
27 | /**
28 | * No copy constructor
29 | * @param global
30 | */
31 | Global(const Global &global) = delete;
32 |
33 | /**
34 | * Move constructor
35 | * @param global
36 | */
37 | Global(Global &&global) : Value(std::move(global)), _name(std::move(global._name)), _exists(global._exists) {}
38 |
39 | /**
40 | * Destructor
41 | */
42 | virtual ~Global() {}
43 |
44 | /**
45 | * Assignment operator
46 | * @param global
47 | * @return Global
48 | */
49 | /*
50 | Global &operator=(const Global &global)
51 | {
52 | // skip self assignment
53 | if (&global == this) return *this;
54 |
55 | // call base
56 | Value::operator=(global);
57 |
58 | // copy name and exists setting
59 | _name = global._name;
60 | _exists = global._exists;
61 |
62 | // done
63 | return *this;
64 | }
65 | */
66 |
67 | /**
68 | * Move operator
69 | * @param global
70 | * @return Global
71 | */
72 | /*
73 | Global &operator=(Global &&global)
74 | {
75 | // skip self assignment
76 | if (&global == this) return *this;
77 |
78 | // call base
79 | Value::operator=(std::move(global));
80 |
81 | // copy name and exists setting
82 | _name = std::move(global._name);
83 | _exists = global._exists;
84 |
85 | // done
86 | return *this;
87 | }
88 | */
89 |
90 | /**
91 | * Assignment operator
92 | * @param value
93 | * @return Global
94 | */
95 | template
96 | Global &operator=(const T &value)
97 | {
98 | Value::operator=(value);
99 | return update();
100 | }
101 |
102 | /**
103 | * Set a certain property
104 | * Calling this method will turn the value into an array
105 | * @param index Index of the property to set
106 | * @param value Value to set
107 | * @return Value The value that was set
108 | */
109 | virtual const Value &set(int index, const Value &value) override
110 | {
111 | // update current object
112 | update();
113 |
114 | // call base
115 | return Value::set(index, value);
116 | }
117 |
118 | /**
119 | * Set a certain property
120 | * Calling this method will turn the value into an array
121 | * @param key Key of the property to set
122 | * @param size Size of the key
123 | * @param value Value to set
124 | * @return Value The value that was set
125 | */
126 | virtual const Value &set(const char *key, int size, const Value &value) override
127 | {
128 | // update current object
129 | update();
130 |
131 | // call base
132 | return Value::set(key, size, value);
133 | }
134 |
135 |
136 | protected:
137 | /**
138 | * Function that is called when the value is updated
139 | * @return Value
140 | */
141 | Global &update();
142 |
143 | private:
144 | /**
145 | * Constructor for non-existing var
146 | * @param name
147 | */
148 | Global(const char *name) : Value(), _name(name), _exists(false) {}
149 |
150 | /**
151 | * Alternative constructor for non-existing var
152 | * @param name
153 | */
154 | Global(const std::string &name) : Value(), _name(name), _exists(false) {}
155 |
156 | /**
157 | * Constructor to wrap zval for existing global bar
158 | * @param name
159 | * @param val
160 | */
161 | Global(const char *name, struct _zval_struct *val) : Value(val, true), _name(name), _exists(true) {}
162 |
163 | /**
164 | * Alternative constructor to wrap zval
165 | * @param name
166 | * @param val
167 | */
168 | Global(const std::string &name, struct _zval_struct *val) : Value(val, true), _name(name), _exists(true) {}
169 |
170 | /**
171 | * Name of the variable
172 | * @var string
173 | */
174 | std::string _name;
175 |
176 | /**
177 | * Does it already exist?
178 | * @var bool
179 | */
180 | bool _exists;
181 |
182 | /**
183 | * The environment can access the private method from this class
184 | */
185 | friend class Environment;
186 | };
187 |
188 | /**
189 | * End of namespace
190 | */
191 | }
192 |
193 |
194 |
--------------------------------------------------------------------------------
/src/include/phpcpp/globals.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Globals.h
3 | *
4 | * Variables and structured required by the Zend engine to work
5 | * with global variables
6 | *
7 | * @author Emiel Bruijntjes
8 | * @copyright 2013 Copernica BV
9 | */
10 |
11 | /**
12 | * Namespace
13 | */
14 | namespace Php {
15 |
16 | /**
17 | * The way how PHP C API deals with "global" variables is stupid.
18 | *
19 | * This is supposed to turn into a structure that is going to be
20 | * instantiated for each parallel running request, and for which the
21 | * PHP engine allocates a certain amount of memory, and a magic
22 | * pointer that is passed and should be forwarded to every thinkable
23 | * PHP function.
24 | *
25 | * We don't like this architecture. We have our own environment object
26 | * that makes much more sense, and that we use. However, we need
27 | * to assign this object somewhere, so that's what we do in this
28 | * one and only global variable
29 | */
30 | ZEND_BEGIN_MODULE_GLOBALS(phpcpp)
31 | Php::Environment *environment;
32 | ZEND_END_MODULE_GLOBALS(phpcpp)
33 |
34 | /**
35 | * And now we're going to define a macro. This also is a ridiculous
36 | * architecture from PHP to get access to a variable from the
37 | * structure above.
38 | */
39 | #ifdef ZTS
40 | #define PHPCPP_G(v) TSRMG(phpcpp_globals_id, phpcpp_globals *, v)
41 | #else
42 | #define PHPCPP_G(v) (phpcpp_globals.v)
43 | #endif
44 |
45 | /**
46 | * We're almost there, we now need to declare an instance of the
47 | * structure defined above (if building for a single thread) or some
48 | * sort of impossible to understand magic pointer-to-a-pointer (for
49 | * multi-threading builds). We make this a static variable because
50 | * this already is bad enough.
51 | */
52 | extern ZEND_DECLARE_MODULE_GLOBALS(phpcpp)
53 |
54 | /**
55 | * End of namespace
56 | */
57 | }
58 |
59 |
--------------------------------------------------------------------------------
/src/include/phpcpp/hashmember.h:
--------------------------------------------------------------------------------
1 | /**
2 | * HashMember.h
3 | *
4 | * When you're accessing members in an array or an object, you're
5 | * doing this via an internal member object. This is an object that
6 | * keeps track of the array to which it belongs, and that will update
7 | * the array when the member is modified
8 | *
9 | * You are not supposed to instantiate this class. An instance of it is
10 | * created when you call Value::operator[]
11 | *
12 | * @author Emiel Bruijntjes
13 | * @copyright 2013 Copernica BV
14 | */
15 |
16 | /**
17 | * Set up namespace
18 | */
19 | namespace Php {
20 |
21 | /**
22 | * Forward definitions
23 | */
24 | class Value;
25 |
26 | /**
27 | * Member class
28 | */
29 | template
30 | class HashMember
31 | {
32 | public:
33 | /**
34 | * Destructor
35 | */
36 | virtual ~HashMember() {}
37 |
38 | /**
39 | * Assign a value object to the array
40 | * @param value
41 | * @return Member
42 | */
43 | HashMember &operator=(const Value &value)
44 | {
45 | // set property in parent array
46 | _base.set(_index, value);
47 |
48 | // if there is a parent, it should sets its value too
49 | if (_parent) _parent->operator=(_base);
50 |
51 | // done
52 | return *this;
53 | }
54 |
55 | /**
56 | * Retrieve the original value
57 | * @return Value
58 | */
59 | Value value() const
60 | {
61 | return _base.get(_index);
62 | }
63 |
64 | /**
65 | * Cast to a value object
66 | * @return Value
67 | */
68 | operator Value () const
69 | {
70 | return _base.get(_index);
71 | }
72 |
73 | /**
74 | * Cast to a integer
75 | * @return int16_t
76 | */
77 | operator int16_t () const
78 | {
79 | return _base.get(_index).numericValue();
80 | }
81 |
82 | /**
83 | * Cast to a integer
84 | * @return int32_t
85 | */
86 | operator int32_t () const
87 | {
88 | return _base.get(_index).numericValue();
89 | }
90 |
91 | /**
92 | * Cast to a integer
93 | * @return int64_t
94 | */
95 | operator int64_t () const
96 | {
97 | return _base.get(_index).numericValue();
98 | }
99 |
100 | /**
101 | * Cast to a boolean
102 | * @return boolean
103 | */
104 | operator bool () const
105 | {
106 | return _base.get(_index).boolValue();
107 | }
108 |
109 | /**
110 | * Cast to a string
111 | * @return string
112 | */
113 | operator std::string () const
114 | {
115 | return _base.get(_index).stringValue();
116 | }
117 |
118 | /**
119 | * Cast to byte array
120 | * @return const char *
121 | */
122 | operator const char * () const
123 | {
124 | return _base.get(_index).rawValue();
125 | }
126 |
127 | /**
128 | * Cast to a floating point
129 | * @return double
130 | */
131 | operator double () const
132 | {
133 | return _base.get(_index).decimalValue();
134 | }
135 |
136 | /**
137 | * Array access operator
138 | * This can be used for accessing arrays
139 | * @param index
140 | * @return HashMember
141 | */
142 | HashMember operator[](int index)
143 | {
144 | return _base.get(_index)[index].add(this);
145 | }
146 |
147 | /**
148 | * Array access operator
149 | * This can be used for accessing associative arrays
150 | * @param key
151 | * @return HashMember
152 | */
153 | HashMember operator[](const std::string &key)
154 | {
155 | return _base.get(_index)[key].add(this);
156 | }
157 |
158 | /**
159 | * Array access operator
160 | * This can be used for accessing associative arrays
161 | * @param key
162 | * @return HashMember
163 | */
164 | HashMember operator[](const char *key)
165 | {
166 | return _base.get(_index)[key].add(this);
167 | }
168 |
169 | private:
170 | /**
171 | * Constructor
172 | * @param base Base value
173 | * @param index Index in the array
174 | */
175 | HashMember(const Value *base, Type index) : _base(*base), _index(index) {}
176 |
177 | /**
178 | * Protected copy constructor
179 | * @param value Other element
180 | */
181 | HashMember(const HashMember &member) : _base(member._base), _index(member._index), _parent(member._parent) {}
182 |
183 | /**
184 | * Add parent
185 | * @param parent
186 | * @return HashMember
187 | */
188 | HashMember &add(HashMember *parent)
189 | {
190 | _parent = parent;
191 | return *this;
192 | }
193 |
194 | /**
195 | * The original index
196 | * @var Type
197 | */
198 | Type _index;
199 |
200 | /**
201 | * Base value
202 | * @var Value
203 | */
204 | Value _base;
205 |
206 | /**
207 | * Parent member (in case of nested members)
208 | * @var HashMember
209 | */
210 | HashMember *_parent = nullptr;
211 |
212 | /**
213 | * Only value objects may construct members
214 | */
215 | friend class Value;
216 | friend class Properties;
217 |
218 | };
219 |
220 | /**
221 | * Custom output stream operator
222 | * @param stream
223 | * @param value
224 | * @return ostream
225 | */
226 | std::ostream &operator<<(std::ostream &stream, const HashMember &value);
227 | std::ostream &operator<<(std::ostream &stream, const HashMember &value);
228 |
229 |
230 | /**
231 | * End of namespace
232 | */
233 | }
234 |
235 |
--------------------------------------------------------------------------------
/src/include/phpcpp/hiddenpointer.h:
--------------------------------------------------------------------------------
1 | /**
2 | * HiddenPointer.h
3 | *
4 | * Helper class that we use to hide a pointer in a string. We do this
5 | * by creating a string buffer that is a littlebit bigger, and put
6 | * the hidden pointer in front of the name
7 | *
8 | * @author Emiel Bruijntjes
9 | * @copyright 2013 Copernica BV
10 | */
11 |
12 | /**
13 | * Set up namespace
14 | */
15 | namespace Php {
16 |
17 | /**
18 | * Class definition
19 | */
20 | template
21 | class HiddenPointer
22 | {
23 | public:
24 | /**
25 | * Constructor to hide the pointer in a buffer
26 | * @param pointer The pointer to hide
27 | * @param text The visible text
28 | * @param size Optional text size
29 | */
30 | HiddenPointer(Type *pointer, const char *text, int size=-1)
31 | {
32 | // calculate size
33 | if (size < 0) size = strlen(text);
34 |
35 | // reserve enough room for the text and the pointer
36 | _data.reserve(size + sizeof(Type *));
37 |
38 | // store the pointer
39 | _data.assign(std::string((const char *)&pointer, sizeof(Type *)));
40 |
41 | // append the text
42 | _data.append(text, size);
43 |
44 | // store pointers
45 | _pointer = pointer;
46 | _text = _data.c_str() + sizeof(Type *);
47 | }
48 |
49 | /**
50 | * Hide pointer in buffer
51 | * @param pointer
52 | * @param text
53 | */
54 | HiddenPointer(Type *pointer, const std::string &text) : HiddenPointer(pointer, text.c_str(), text.size()) {}
55 |
56 | /**
57 | * Constructor to retrieve the object given a buffer
58 | * @param text The visible text
59 | * @param size Size of the text
60 | */
61 | HiddenPointer(const char *text, int size=-1)
62 | {
63 | // calculate size
64 | if (size < 0) size = strlen(text);
65 |
66 | // the pointer is stored right in front of the name
67 | _pointer = *((Type **)(text - sizeof(Type *)));
68 | _text = text;
69 | }
70 |
71 | /**
72 | * Copy constructor
73 | * @param that
74 | */
75 | HiddenPointer(const HiddenPointer &that) : _pointer(that._pointer), _text(that._text), _data(that._data)
76 | {
77 | // if data is filled, the text is located inside the data
78 | if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
79 | }
80 |
81 | /**
82 | * Destructor
83 | */
84 | virtual ~HiddenPointer() {}
85 |
86 | /**
87 | * Assignment operator
88 | * @param that
89 | * @return HiddenPointer
90 | */
91 | HiddenPointer operator=(const HiddenPointer &that)
92 | {
93 | // skip self assignment
94 | if (&that == this) return *this;
95 |
96 | // copy members
97 | _pointer = that._pointer;
98 | _text = that._text;
99 | _data = that._data;
100 |
101 | // if data is filled, the text is located inside the data
102 | if (_data.size() > 0) _text = _data.c_str() + sizeof(Type *);
103 | }
104 |
105 | /**
106 | * Retrieve the pointer
107 | * @return Type*
108 | */
109 | Type *pointer() const
110 | {
111 | return _pointer;
112 | }
113 |
114 | /**
115 | * Change the pointer
116 | * @param Type*
117 | */
118 | void setPointer(Type *pointer)
119 | {
120 | // store pointer
121 | _pointer = pointer;
122 |
123 | // overwrite in data
124 | _data.replace(0, sizeof(Type *), (const char *)&_pointer, sizeof(Type *));
125 |
126 | // for safety reasons, we recalculate text pointer
127 | _text = _data.c_str() + sizeof(Type *);
128 | }
129 |
130 | /**
131 | * Retrieve the text
132 | * @return const char *
133 | */
134 | const char *text() const
135 | {
136 | return _text;
137 | }
138 |
139 | /**
140 | * Change the text
141 | * @param text
142 | * @param size
143 | */
144 | void setText(const char *text, int size=-1)
145 | {
146 | // check if size was set
147 | if (size < 0) size = strlen(text);
148 |
149 | // reserve enough room for the text and the pointer
150 | _data.reserve(size + sizeof(Type *));
151 |
152 | // store the pointer
153 | _data.assign(std::string((const char *)&_pointer, sizeof(Type *)));
154 |
155 | // append the text
156 | _data.append(text, size);
157 |
158 | // store new text
159 | _text = _data.c_str() + sizeof(Type *);
160 | }
161 |
162 | /**
163 | * Cast to the pointer
164 | * @return Type*
165 | */
166 | operator Type* () const
167 | {
168 | return _pointer;
169 | }
170 |
171 | /**
172 | * Cast to text
173 | * @return const char *
174 | */
175 | operator const char * () const
176 | {
177 | return _text;
178 | }
179 |
180 | /**
181 | * Length of the text
182 | * @return int
183 | */
184 | int length() const
185 | {
186 | return _data.size() - sizeof(Type *);
187 | }
188 |
189 | private:
190 | /**
191 | * The actual pointer
192 | * @var Type*
193 | */
194 | Type *_pointer;
195 |
196 | /**
197 | * The original text
198 | * @var text
199 | */
200 | const char *_text;
201 |
202 | /**
203 | * Optional data buffer
204 | * @var string
205 | */
206 | std::string _data;
207 |
208 | };
209 |
210 | /**
211 | * End of namespace
212 | */
213 | }
214 |
215 |
--------------------------------------------------------------------------------
/src/include/phpcpp/member.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Member.h
3 | *
4 | * Base class for elements of a class
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Forward declarations
12 | */
13 | struct _zend_class_entry;
14 |
15 | /**
16 | * Namespace
17 | */
18 | namespace Php {
19 |
20 | /**
21 | * Forward declarations
22 | */
23 | class MemberInfo;
24 |
25 | /**
26 | * Class definition
27 | */
28 | class Member
29 | {
30 | public:
31 | /**
32 | * Constructor
33 | * @param name Name of the member
34 | * @param pub Is this a public property (otherwise it is protected)
35 | */
36 | Member(const char *name, bool pub);
37 |
38 | /**
39 | * Constructor
40 | * @param name Name of the member
41 | * @param pub Is this a public property (otherwise it is protected)
42 | * @param value The value to add
43 | */
44 | Member(const char *name, bool pub, std::nullptr_t value);
45 |
46 | /**
47 | * Constructor
48 | * @param name Name of the member
49 | * @param pub Is this a public property (otherwise it is protected)
50 | * @param value The value to add
51 | */
52 | Member(const char *name, bool pub, int value);
53 |
54 | /**
55 | * Constructor
56 | * @param name Name of the member
57 | * @param pub Is this a public property (otherwise it is protected)
58 | * @param value The value to add
59 | */
60 | Member(const char *name, bool pub, long value);
61 |
62 | /**
63 | * Constructor
64 | * @param name Name of the member
65 | * @param pub Is this a public property (otherwise it is protected)
66 | * @param value The value to add
67 | */
68 | Member(const char *name, bool pub, bool value);
69 |
70 | /**
71 | * Constructor
72 | * @param name Name of the member
73 | * @param pub Is this a public property (otherwise it is protected)
74 | * @param value The value to add
75 | */
76 | Member(const char *name, bool pub, char value);
77 |
78 | /**
79 | * Constructor
80 | * @param name Name of the member
81 | * @param pub Is this a public property (otherwise it is protected)
82 | * @param value The value to add
83 | */
84 | Member(const char *name, bool pub, const std::string &value);
85 |
86 | /**
87 | * Constructor
88 | * @param name Name of the member
89 | * @param pub Is this a public property (otherwise it is protected)
90 | * @param value The value to add
91 | * @param size String length
92 | */
93 | Member(const char *name, bool pub, const char *value, int size = -1);
94 |
95 | /**
96 | * Constructor
97 | * @param name Name of the member
98 | * @param pub Is this a public property (otherwise it is protected)
99 | * @param value The value to add
100 | */
101 | Member(const char *name, bool pub, double value);
102 |
103 | /**
104 | * Constructor
105 | * @param name Name of the method
106 | * @param pub Is this a public method (otherwise it is protected)
107 | * @param method The method to add
108 | */
109 | Member(const char *name, bool pub, const _Method &method, const std::initializer_list &arguments = {});
110 |
111 | /**
112 | * Copy constructor
113 | * @param member The member to copy
114 | */
115 | Member(const Member &member);
116 |
117 | /**
118 | * Move constructor
119 | * @param member The member to move
120 | */
121 | Member(Member &&member);
122 |
123 | /**
124 | * Destructor
125 | */
126 | virtual ~Member();
127 |
128 | /**
129 | * Internal method to declare the property
130 | * @param zend_class_entry
131 | * @internal
132 | */
133 | void declare(struct _zend_class_entry *entry);
134 |
135 | /**
136 | * Internal method to fill a function entry
137 | * @param zend_function_entry
138 | * @param classname
139 | * @internal
140 | */
141 | void fill(struct _zend_function_entry *entry, const char *classname);
142 |
143 | /**
144 | * Is this a property member
145 | * @return bool
146 | */
147 | bool isProperty();
148 |
149 | /**
150 | * Is this a method member
151 | * @return bool
152 | */
153 | bool isMethod();
154 |
155 |
156 | private:
157 | /**
158 | * Name of the member
159 | * @var string
160 | */
161 | std::string _name;
162 |
163 | /**
164 | * Is this a public property
165 | * @var bool
166 | */
167 | bool _public;
168 |
169 | /**
170 | * The implementation for the member
171 | * @var MemberInfo
172 | */
173 | MemberInfo *_info;
174 |
175 |
176 | };
177 |
178 | /**
179 | * End of namespace
180 | */
181 | }
182 |
183 |
--------------------------------------------------------------------------------
/src/include/phpcpp/members.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Members.h
3 | *
4 | * Internal helper class that holds all members of a class
5 | *
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Class definition
17 | */
18 | class Members : public std::vector
19 | {
20 | public:
21 | /**
22 | * Constructor
23 | * @param arguments
24 | */
25 | Members(const std::initializer_list &members) : std::vector(members), _methods(NULL) {}
26 |
27 | /**
28 | * Destructor
29 | */
30 | virtual ~Members();
31 |
32 | /**
33 | * Get access to the methods
34 | * @param classname
35 | * @return Methods
36 | */
37 | struct _zend_function_entry *methods(const char *classname);
38 |
39 | private:
40 | /**
41 | * Number of methods
42 | * @return integer
43 | */
44 | int methods();
45 |
46 | /**
47 | * Array of method structures used internally in the Zend engine
48 | * @var zend_function_entry
49 | */
50 | struct _zend_function_entry *_methods;
51 | };
52 |
53 | /**
54 | * End of namespace
55 | */
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/include/phpcpp/method.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Method.h
3 | */
4 |
5 | /**
6 | * Namespace
7 | */
8 | namespace Php {
9 |
10 | /**
11 | * A very generic function pointer
12 | */
13 | typedef void (*function_ptr)();
14 |
15 | /**
16 | * Base class of the method
17 | */
18 | class _Method
19 | {
20 | public:
21 | /**
22 | * Copy constructor
23 | * @param method
24 | */
25 | _Method(const _Method &method) : _type(method._type), _callback(method._callback) {}
26 |
27 | /**
28 | * Destructor
29 | * @param type
30 | * @param callback
31 | */
32 | virtual ~_Method() {}
33 |
34 | /**
35 | * Invoke the method
36 | * @param environment
37 | * @param parameters
38 | * @return Value
39 | */
40 | Value invoke(Environment &environment, Parameters ¶meters)
41 | {
42 | // the object to call a method on
43 | Base *base = parameters.object();
44 |
45 | // find out which method to call, and call it
46 | switch (_type) {
47 | case 0: (base->*_callback.m0)(); return Value();
48 | case 1: (base->*_callback.m1)(parameters); return Value();
49 | case 2: (base->*_callback.m2)(environment); return Value();
50 | case 3: (base->*_callback.m3)(environment, parameters); return Value();
51 | case 4: return (base->*_callback.m4)();
52 | case 5: return (base->*_callback.m5)(parameters);
53 | case 6: return (base->*_callback.m6)(environment);
54 | case 7: return (base->*_callback.m7)(environment, parameters);
55 | default: return Value();
56 | }
57 | }
58 |
59 | protected:
60 | /**
61 | * Protected constructor to prevent that anyone instantiates this object
62 | */
63 | _Method(method_callback_0 callback) : _type(0) { _callback.m0 = callback; }
64 | _Method(method_callback_1 callback) : _type(1) { _callback.m1 = callback; }
65 | _Method(method_callback_2 callback) : _type(2) { _callback.m2 = callback; }
66 | _Method(method_callback_3 callback) : _type(3) { _callback.m3 = callback; }
67 | _Method(method_callback_4 callback) : _type(4) { _callback.m4 = callback; }
68 | _Method(method_callback_5 callback) : _type(5) { _callback.m5 = callback; }
69 | _Method(method_callback_6 callback) : _type(6) { _callback.m6 = callback; }
70 | _Method(method_callback_7 callback) : _type(7) { _callback.m7 = callback; }
71 |
72 | private:
73 | /**
74 | * Callback type
75 | * @var int
76 | */
77 | int _type;
78 |
79 | /**
80 | * The actual callback
81 | * @var void*
82 | */
83 | union {
84 | method_callback_0 m0;
85 | method_callback_1 m1;
86 | method_callback_2 m2;
87 | method_callback_3 m3;
88 | method_callback_4 m4;
89 | method_callback_5 m5;
90 | method_callback_6 m6;
91 | method_callback_7 m7;
92 | } _callback;
93 | };
94 |
95 | /**
96 | * Actual template class of the method
97 | */
98 | template
99 | class Method : public _Method
100 | {
101 | public:
102 | /**
103 | * Constructor
104 | * @param callback
105 | */
106 | Method(void(T::*callback)()) : _Method(static_cast(callback)) {}
107 | Method(void(T::*callback)(Parameters&)) : _Method(static_cast(callback)) {}
108 | Method(void(T::*callback)(Environment&)) : _Method(static_cast(callback)) {}
109 | Method(void(T::*callback)(Environment&,Parameters&)) : _Method(static_cast(callback)) {}
110 | Method(Value(T::*callback)()) : _Method(static_cast(callback)) {}
111 | Method(Value(T::*callback)(Parameters&)) : _Method(static_cast(callback)) {}
112 | Method(Value(T::*callback)(Environment&)) : _Method(static_cast(callback)) {}
113 | Method(Value(T::*callback)(Environment&,Parameters&)) : _Method(static_cast(callback)) {}
114 |
115 | /**
116 | * Destructor
117 | */
118 | virtual ~Method() {}
119 |
120 | };
121 |
122 | /**
123 | * End of namespace
124 | */
125 | }
126 |
127 |
128 |
--------------------------------------------------------------------------------
/src/include/phpcpp/parameters.h:
--------------------------------------------------------------------------------
1 | /**
2 | * Parameters.h
3 | *
4 | * Wrapper around parameters that are passed to a
5 |
6 | * @author Emiel Bruijntjes
7 | * @copyright 2013 Copernica BV
8 | */
9 |
10 | /**
11 | * Namespace
12 | */
13 | namespace Php {
14 |
15 | /**
16 | * Forward declarations
17 | */
18 | class Base;
19 |
20 | /**
21 | * Class definition
22 | */
23 | class Parameters : public std::vector
24 | {
25 | public:
26 | /**
27 | * Constructor
28 | * @param this_ptr Optional this_ptr
29 | * @param argc Number of arguments
30 | * @param tsrm_ls
31 | */
32 | Parameters(struct _zval_struct *this_ptr, int argc);// TSRMLS_DC);
33 |
34 | /**
35 | * Destructor
36 | */
37 | virtual ~Parameters() {}
38 |
39 | /**
40 | * The the object that is called
41 | * @return Base
42 | */
43 | Base *object();
44 |
45 | private:
46 | /**
47 | * The this pointer
48 | * @var zval
49 | */
50 | struct _zval_struct *_this;
51 | };
52 |
53 | /**
54 | * End of namespace
55 | */
56 | }
57 |
58 |
--------------------------------------------------------------------------------
/src/include/phpcpp/phpcpp.h:
--------------------------------------------------------------------------------
1 | /**
2 | * phpcpp.h
3 | *
4 | * Library to build PHP extensions with CPP
5 | *
6 | * @copyright 2013 CopernicA BV
7 | * @author Emiel Bruijntjes
8 | */
9 |
10 | /**
11 | * Other C and C++ libraries that PhpCpp depends on
12 | */
13 | #include
14 | #include
15 | #include
16 | #include
17 | #include