├── react.ini ├── .gitignore ├── Makefile ├── core ├── Makefile ├── resultrow.cpp ├── resolver.h ├── result.cpp ├── parameter.h ├── localparameter.h ├── statement.h ├── signalwatcher.h ├── readwatcher.h ├── synchronizewatcher.h ├── writewatcher.h ├── statuswatcher.h ├── resolver.cpp ├── timeoutwatcher.h ├── connection.cpp ├── connection.h ├── resultfield.h ├── intervalwatcher.h ├── statement.cpp ├── resolverresult.h ├── result.h ├── resultrow.h ├── loop.h ├── loop.cpp └── extension.cpp ├── tests ├── resolver.php ├── fd.php ├── statement.php ├── timers.php ├── querystatement.php ├── mysql.php └── loop.php ├── README.md~ ├── README.md └── LICENSE /react.ini: -------------------------------------------------------------------------------- 1 | extension = react.so 2 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled Object files 2 | *.slo 3 | *.lo 4 | *.o 5 | 6 | # Compiled Dynamic libraries 7 | *.so 8 | *.dylib 9 | 10 | # Compiled Static libraries 11 | *.lai 12 | *.la 13 | *.a 14 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | EXTENSION_DIR = /usr/lib/php5/20090626/ 2 | CONFIG_DIR = /etc/php5/conf.d/ 3 | 4 | all: 5 | $(MAKE) -C core all 6 | 7 | clean: 8 | $(MAKE) -C core clean 9 | 10 | install: 11 | cp -f core/react.so ${EXTENSION_DIR} 12 | cp -f react.ini ${CONFIG_DIR} 13 | -------------------------------------------------------------------------------- /core/Makefile: -------------------------------------------------------------------------------- 1 | CPP = g++ 2 | RM = rm -f 3 | CPPFLAGS = -Wall -c -I. -O2 -flto -std=c++11 -g 4 | LD = g++ 5 | LD_FLAGS = -Wall -shared -O2 6 | LIB = react.so 7 | SOURCES = $(wildcard *.cpp */*.cpp) 8 | OBJECTS = $(SOURCES:%.cpp=%.o) 9 | 10 | all: ${OBJECTS} ${LIB} 11 | 12 | ${LIB}: ${OBJECTS} 13 | ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lev -lcares -lvariant -lphpcpp -lreactcpp-mysql `mysql_config --libs` -lreactcpp 14 | 15 | clean: 16 | ${RM} *.obj *~* ${OBJECTS} ${LIB} 17 | 18 | ${OBJECTS}: 19 | ${CPP} ${CPPFLAGS} -fpic -o $@ ${@:%.o=%.cpp} 20 | -------------------------------------------------------------------------------- /tests/resolver.php: -------------------------------------------------------------------------------- 1 | ip("www.copernica.com", function() { 17 | 18 | echo("IPs: "); 19 | 20 | return false; 21 | }); 22 | 23 | // fetch all MX records 24 | $resolver->mx("copernica.com", function() { 25 | 26 | echo("MX records:\n"); 27 | 28 | return false; 29 | }); 30 | 31 | // run the event loop 32 | $loop->run(); 33 | -------------------------------------------------------------------------------- /tests/fd.php: -------------------------------------------------------------------------------- 1 | onReadable('STDIN_FILENO', function() { 14 | 15 | // report that input is available 16 | echo("Input is available at first reader\n"); 17 | 18 | return false; 19 | }); 20 | 21 | // Notify us when input is available on stdin -- alternative way: creating a reader object 22 | $reader2 = new Async\ReadWatcher($loop, 'STDIN_FILENO', function() { 23 | 24 | // report that input is available 25 | echo("Input is available at second reader\n"); 26 | 27 | return false; 28 | }); 29 | 30 | // run the event loop 31 | $loop->run(); 32 | -------------------------------------------------------------------------------- /tests/statement.php: -------------------------------------------------------------------------------- 1 | execute(function() { 32 | 33 | echo("Statement was executed\n"); 34 | 35 | return false; 36 | }); 37 | 38 | // run the event loop 39 | $loop->run(); 40 | -------------------------------------------------------------------------------- /tests/timers.php: -------------------------------------------------------------------------------- 1 | onTimeout(1.0, function() { 16 | 17 | // report that the timer has expired 18 | echo("Timer 1 expired\n"); 19 | 20 | return false; 21 | }); 22 | 23 | // Set a timer -- alternative way: creating a timer object 24 | // The timer will stop executing after 2.0 seconds 25 | // and the callback function will notify us of that fact 26 | $timer2 = new Async\TimeoutWatcher($loop, 2.0, function() { 27 | 28 | // report that the timer has expired 29 | echo("Timer 2 expired\n"); 30 | 31 | return false; 32 | }); 33 | 34 | // The current time is displayed 35 | echo("Current time: ".$loop->now()."\n"); 36 | 37 | // run the event loop 38 | $loop->run(); 39 | -------------------------------------------------------------------------------- /core/resultrow.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * ResultRow.cpp 3 | * 4 | * Class with result data for a single MySQL row. 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include "resultrow.h" 13 | 14 | /** 15 | * Set up namespace 16 | */ 17 | namespace ReactPhp { 18 | 19 | /** 20 | * Get the number of fields in the row 21 | * @return size_t 22 | */ 23 | size_t ResultRow::size() const 24 | { 25 | return _resultRow.size(); 26 | } 27 | 28 | /** 29 | * Retrieve a field by index 30 | * 31 | * This function throws an exception if no field 32 | * exists under the given index (i.e. index 33 | * is not smaller than size()). 34 | * 35 | * @param index field index 36 | */ 37 | Php::Value ResultRow::operator [] (size_t index) const 38 | { 39 | return (std::string)_resultRow.operator[](index); 40 | } 41 | 42 | /** 43 | * Retrieve a field by name 44 | * 45 | * This function throws an exception if no field 46 | * exists under the given key. 47 | * 48 | * @param key field name 49 | */ 50 | Php::Value ResultRow::operator [] (const std::string &key) const 51 | { 52 | return (std::string)_resultRow.operator[](key); 53 | } 54 | 55 | /** 56 | * End of namespace 57 | */ 58 | } 59 | -------------------------------------------------------------------------------- /tests/querystatement.php: -------------------------------------------------------------------------------- 1 | executeQuery(function($result) { 30 | 31 | echo("Executing the query statement:\n"); 32 | 33 | // iterate over the result set 34 | foreach ($result as $resultRow) 35 | { 36 | // wrap each row in curly braces 37 | echo("{ "); 38 | 39 | // iterate over each individual row of the result set 40 | foreach ($resultRow as $resultField) 41 | { 42 | // and dump all the result fields to the screen 43 | echo("$resultField "); 44 | } 45 | 46 | // close the row 47 | echo("}\n"); 48 | } 49 | 50 | return false; 51 | }); 52 | 53 | // run the event loop 54 | $loop->run(); 55 | -------------------------------------------------------------------------------- /core/resolver.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Resolver.h 3 | * 4 | * Class for resolving domain names 5 | */ 6 | 7 | /** 8 | * Dependencies 9 | */ 10 | #include 11 | #include 12 | 13 | /** 14 | * Set up namespace 15 | */ 16 | namespace ReactPhp { 17 | 18 | /** 19 | * Class definition 20 | */ 21 | class Resolver : public Php::Base 22 | { 23 | private: 24 | /** 25 | * The actual resolver object 26 | * @var std::shared_ptr 27 | */ 28 | std::shared_ptr _resolver; 29 | 30 | public: 31 | /** 32 | * Constructor 33 | */ 34 | Resolver() {} 35 | 36 | /** 37 | * Alternative constructor 38 | * @param loop 39 | */ 40 | void __construct(Php::Parameters ¶ms); 41 | 42 | /** 43 | * Destructor 44 | */ 45 | virtual ~Resolver() {} 46 | 47 | /** 48 | * Find all IP addresses for a certain domain 49 | * @param domain The domain to fetch the IPs for 50 | * @param version IP version, can be 4 or 6 51 | * @param callback 52 | * @return bool 53 | */ 54 | Php::Value ip(Php::Parameters ¶ms); 55 | 56 | /** 57 | * Find all MX records for a certain domain 58 | * @param domain The domain name to search MX records for 59 | * @param callback Callback that is called when found 60 | * @return bool 61 | */ 62 | Php::Value mx(Php::Parameters ¶ms); 63 | 64 | }; 65 | 66 | /** 67 | * End of namespace 68 | */ 69 | } 70 | -------------------------------------------------------------------------------- /tests/mysql.php: -------------------------------------------------------------------------------- 1 | query("SELECT * FROM Persons", function($result) { 23 | 24 | // iterate over the result set 25 | foreach ($result as $row) 26 | { 27 | // wrap each row in curly braces 28 | echo("{ "); 29 | 30 | // iterate over each individual row of the result set 31 | foreach ($row as $field) 32 | { 33 | // and dump all the result fields to the screen 34 | echo("$field "); 35 | } 36 | 37 | // close the row 38 | echo("}\n"); 39 | } 40 | 41 | echo("\nAlternative way produces the following results:\n"); 42 | 43 | // alternative way -> iterate over the result set and fetch each individual row 44 | for ($i = 0; $i < $result->size(); $i++) 45 | { 46 | // wrap each row in curly braces 47 | echo("{\n"); 48 | 49 | // fetch the row and dump it to the screen 50 | $result->fetchRow($i); 51 | 52 | // close the row 53 | echo("}\n"); 54 | } 55 | 56 | return false; 57 | }); 58 | 59 | // run the event loop 60 | $loop->run(); 61 | 62 | -------------------------------------------------------------------------------- /core/result.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Result.cpp 3 | * 4 | * Class representing a MySQL result set 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include "result.h" 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Retrieve row at given offset 22 | */ 23 | void Result::fetchRow(Php::Parameters ¶ms) 24 | { 25 | // retrieve the parameter 26 | int index = params[0]; 27 | 28 | // check for valid input 29 | if(index > (int)_result.size() || index < 0) throw Php::Exception("Invalid result object"); 30 | 31 | for (auto field : _result[index]) 32 | { 33 | // print the rows to the screen 34 | std::cout << field.first << " => " << field.second << std::endl; 35 | } 36 | } 37 | 38 | /** 39 | * Is this a valid result? 40 | */ 41 | Php::Value Result::valid() 42 | { 43 | _result.valid(); 44 | 45 | return true; 46 | } 47 | 48 | /** 49 | * The number of rows affected 50 | */ 51 | size_t Result::affectedRows() 52 | { 53 | return (int)_result.affectedRows(); 54 | } 55 | 56 | /** 57 | * Get the number of rows in this result 58 | */ 59 | Php::Value Result::size() 60 | { 61 | return (int)_result.size(); 62 | } 63 | 64 | /** 65 | * Retrieve iterator for first row 66 | */ 67 | Php::Value Result::begin() 68 | { 69 | _result.begin(); 70 | 71 | return true; 72 | } 73 | 74 | /** 75 | * Retrieve iterator past the end 76 | */ 77 | Php::Value Result::end() 78 | { 79 | _result.end(); 80 | 81 | return true; 82 | } 83 | 84 | /** 85 | * End of namespace 86 | */ 87 | } 88 | -------------------------------------------------------------------------------- /core/parameter.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Parameter.h 3 | * 4 | * Input parameter for MySQL prepared statement 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Set up namespace 18 | */ 19 | namespace ReactPhp { 20 | 21 | /** 22 | * Class definition 23 | */ 24 | class Parameter : public Php::Base 25 | { 26 | private: 27 | /** 28 | * The actual parameter object 29 | * @var std::shared_ptr 30 | */ 31 | std::shared_ptr _parameter; 32 | 33 | public: 34 | /** 35 | * Constructor 36 | */ 37 | Parameter() {} 38 | 39 | /** 40 | * Pass in an existing parameter 41 | * @var React::MySQL::Parameter 42 | */ 43 | Parameter(const std::shared_ptr ¶meter) : _parameter(parameter) {} 44 | 45 | /** 46 | * Destructor 47 | */ 48 | ~Parameter() {} 49 | 50 | /** 51 | * Get access to the internal parameter object that is wrapped 52 | * by this PHP parameter class 53 | * @return React::MySQL::Parameter 54 | */ 55 | React::MySQL::Parameter *param() 56 | { 57 | React::MySQL::Parameter *_param = _parameter.get(); 58 | return _param; 59 | } 60 | 61 | /** 62 | * Constructor 63 | * 64 | * @param value parameter value 65 | */ 66 | void __construct(Php::Parameters ¶meters) 67 | { 68 | // retrieve the parameter 69 | const std::string value = parameters[0]; 70 | 71 | _parameter = std::make_shared(value); 72 | } 73 | }; 74 | 75 | /** 76 | * End of namespace 77 | */ 78 | } 79 | -------------------------------------------------------------------------------- /tests/loop.php: -------------------------------------------------------------------------------- 1 | set(5.0); 39 | 40 | // we also want to be notified for future readability events 41 | return true; 42 | }); 43 | 44 | // Set a handler to notify us when control+c is pressed 45 | $handler = $loop->onSignal(SIGINT, function() use($loop, $timer, $reader) { 46 | 47 | // report that control+c was pressed 48 | echo("control+c detected"); 49 | 50 | // Now the timer and the reader can be cancelled 51 | $timer->cancel(); 52 | $reader->cancel(); 53 | 54 | // In one second the application will be terminated 55 | $loop->onTimeout(1.0, function() { 56 | 57 | // report that the internal timer has expired 58 | echo("Internal timer expired\n"); 59 | 60 | return false; 61 | }); 62 | 63 | return false; 64 | }); 65 | 66 | // run the event loop 67 | $loop->run(); 68 | -------------------------------------------------------------------------------- /core/localparameter.h: -------------------------------------------------------------------------------- 1 | /** 2 | * LocalParameter.h 3 | * 4 | * A parameter in a local prepared-statement-like query. 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Set up namespace 18 | */ 19 | namespace ReactPhp { 20 | 21 | /** 22 | * Class definition 23 | */ 24 | class LocalParameter : public Php::Base 25 | { 26 | private: 27 | /** 28 | * The actual Localparameter object 29 | * @var std::shared_ptr 30 | */ 31 | std::shared_ptr _localParameter; 32 | 33 | public: 34 | /** 35 | * Constructor 36 | */ 37 | LocalParameter() {} 38 | 39 | /** 40 | * Pass in an existing local parameter 41 | * @var React::MySQL::LoacalParameter 42 | */ 43 | LocalParameter(const std::shared_ptr &LocalParameter) : _localParameter(LocalParameter) {} 44 | 45 | /** 46 | * Destructor 47 | */ 48 | virtual ~LocalParameter() {} 49 | 50 | /** 51 | * Get access to the internal parameter object that is wrapped 52 | * by this PHP local parameter class 53 | * @return React::MySQL::Parameter 54 | */ 55 | React::MySQL::LocalParameter *localParam() 56 | { 57 | React::MySQL::LocalParameter *_localParam = _localParameter.get(); 58 | return _localParam; 59 | } 60 | 61 | /** 62 | * Constructor 63 | * 64 | * @param value parameter value 65 | */ 66 | void __construct(Php::Parameters ¶meters) 67 | { 68 | // retrieve the parameter 69 | const std::string value = parameters[0]; 70 | 71 | _localParameter = std::make_shared(value); 72 | } 73 | }; 74 | 75 | /** 76 | * End of namespace 77 | */ 78 | } 79 | -------------------------------------------------------------------------------- /core/statement.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Statement.h 3 | * 4 | * Class for executing prepared statements 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Set up namespace 18 | */ 19 | namespace ReactPhp { 20 | 21 | /** 22 | * Class definition 23 | */ 24 | class Statement : public Php::Base 25 | { 26 | private: 27 | 28 | /** 29 | * The actual statement object 30 | * @var std::shared_ptr 31 | */ 32 | std::shared_ptr _statement; 33 | 34 | public: 35 | 36 | /** 37 | * Constructor 38 | */ 39 | Statement() {} 40 | 41 | /** 42 | * Pass in an existing statement 43 | * @var React::MySQL::Statement 44 | */ 45 | Statement(const std::shared_ptr &statement) : _statement(statement) {} 46 | 47 | /** 48 | * Destructor 49 | */ 50 | virtual ~Statement() {} 51 | 52 | /** 53 | * Constructor 54 | * 55 | * @param connection the connection to run the statement on 56 | * @param statement the statement to execute 57 | * @param callback the callback to inform of success or failure 58 | */ 59 | void __construct(Php::Parameters ¶meters); 60 | 61 | /** 62 | * Execute statement with given parameters 63 | * 64 | * @param parameters input parameters 65 | * @param count number of parameters 66 | * @param callback callback to inform of success or failure 67 | */ 68 | void execute(Php::Parameters ¶meters); 69 | 70 | /** 71 | * Execute statement query with given parameters 72 | * 73 | * @param callback callback to inform of success or failure 74 | */ 75 | void executeQuery(Php::Parameters ¶meters); 76 | }; 77 | 78 | /** 79 | * End of namespace 80 | */ 81 | } 82 | -------------------------------------------------------------------------------- /core/signalwatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * SignalWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Signal watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class SignalWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Signal watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | SignalWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Signal watcher 40 | * @var std::shared_ptr 41 | */ 42 | SignalWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~SignalWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param signum 53 | * @param callback 54 | */ 55 | void __construct(Php::Parameters ¶meters) 56 | { 57 | // retrieve the parameters 58 | Php::Value loopParam = parameters[0]; 59 | int signum = parameters[1]; 60 | Php::Value callback = parameters[2]; 61 | 62 | // get the actual base object 63 | Loop *loop = (Loop *)loopParam.implementation(); 64 | 65 | // create the actual timer-watcher 66 | _watcher = std::make_shared(loop->mainLoop(), signum, [callback]() -> bool { 67 | 68 | // pass the call on to PHP 69 | callback(); 70 | 71 | return false; 72 | }); 73 | } 74 | 75 | /** 76 | * Start the signal watcher 77 | * @return bool 78 | */ 79 | Php::Value start() 80 | { 81 | return _watcher->start(); 82 | } 83 | 84 | /** 85 | * Cancel the signal watcher 86 | * @return bool 87 | */ 88 | Php::Value cancel() 89 | { 90 | return _watcher->cancel(); 91 | } 92 | 93 | }; 94 | 95 | /** 96 | * End of namespace 97 | */ 98 | } 99 | -------------------------------------------------------------------------------- /core/readwatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * ReadWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Read watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class ReadWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Read watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | ReadWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Read watcher 40 | * @var std::shared_ptr 41 | */ 42 | ReadWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~ReadWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param fd 53 | * @param callback 54 | */ 55 | void __construct(Php::Parameters ¶meters) 56 | { 57 | // retrieve the parameters 58 | Php::Value loopParam = parameters[0]; 59 | int fd = parameters[1]; 60 | Php::Value callback = parameters[2]; 61 | 62 | // get the actual base object 63 | Loop *loop = (Loop *)loopParam.implementation(); 64 | 65 | // create the actual read-watcher 66 | _watcher = std::make_shared(loop->loop(), fd, [callback]() -> bool { 67 | 68 | // pass the call on to PHP 69 | callback(); 70 | 71 | return true; 72 | }); 73 | } 74 | 75 | /** 76 | * Cancel the watcher 77 | * @return bool 78 | */ 79 | Php::Value cancel() 80 | { 81 | return _watcher->cancel(); 82 | } 83 | 84 | /** 85 | * Start/resume the watcher 86 | * @return bool 87 | */ 88 | Php::Value resume() 89 | { 90 | return _watcher->resume(); 91 | } 92 | 93 | }; 94 | 95 | /** 96 | * End of namespace 97 | */ 98 | } 99 | -------------------------------------------------------------------------------- /core/synchronizewatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * SynchronizeWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Synchronize watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class SynchronizeWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Synchronize watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | SynchronizeWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Synchronize watcher 40 | * @var std::shared_ptr 41 | */ 42 | SynchronizeWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~SynchronizeWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param fd 53 | * @param callback 54 | */ 55 | void __construct(Php::Parameters ¶meters) 56 | { 57 | // retrieve the parameters 58 | Php::Value loopParam = parameters[0]; 59 | Php::Value callback = parameters[1]; 60 | 61 | // get the actual base object 62 | Loop *loop = (Loop *)loopParam.implementation(); 63 | 64 | // create the actual synchronize-watcher 65 | _watcher = std::make_shared(loop->loop(), [callback]() -> bool { 66 | 67 | // pass the call on to PHP 68 | callback(); 69 | 70 | return true; 71 | }); 72 | } 73 | 74 | /** 75 | * Synchronize with the event loop 76 | * @return bool 77 | */ 78 | Php::Value synchronize() 79 | { 80 | return _watcher->synchronize(); 81 | } 82 | 83 | /** 84 | * Cancel the synchronizer 85 | * @return bool 86 | */ 87 | Php::Value cancel() 88 | { 89 | return _watcher->cancel(); 90 | } 91 | 92 | }; 93 | 94 | /** 95 | * End of namespace 96 | */ 97 | } 98 | 99 | -------------------------------------------------------------------------------- /core/writewatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * WriteWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Write watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class WriteWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Write watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | WriteWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Write watcher 40 | * @var std::shared_ptr 41 | */ 42 | WriteWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~WriteWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param fd 53 | * @param callback 54 | */ 55 | void __construct(Php::Parameters ¶meters) 56 | { 57 | // retrieve the parameters 58 | Php::Value loopParam = parameters[0]; 59 | int fd = parameters[1]; 60 | Php::Value callback = parameters[2]; 61 | 62 | // get the actual base object 63 | Loop *loop = (Loop *)loopParam.implementation(); 64 | 65 | // create the actual write-watcher 66 | _watcher = std::make_shared(loop->loop(), fd, [callback]() -> bool { 67 | 68 | // pass the call on to PHP 69 | callback(); 70 | 71 | return true; 72 | }); 73 | } 74 | 75 | /** 76 | * Cancel the watcher 77 | * @return bool 78 | */ 79 | Php::Value cancel() 80 | { 81 | return _watcher->cancel(); 82 | } 83 | 84 | /** 85 | * Start/resume the watcher 86 | * @return bool 87 | */ 88 | Php::Value resume() 89 | { 90 | return _watcher->resume(); 91 | } 92 | 93 | }; 94 | 95 | /** 96 | * End of namespace 97 | */ 98 | } 99 | 100 | -------------------------------------------------------------------------------- /core/statuswatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * StatusWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Status Watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class StatusWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Status Watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | StatusWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Status Watcher 40 | * @var std::shared_ptr 41 | */ 42 | StatusWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~StatusWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param pid 53 | * @param trace 54 | * @param callback 55 | */ 56 | void __construct(Php::Parameters ¶meters) 57 | { 58 | // retrieve the parameters 59 | Php::Value loopParam = parameters[0]; 60 | Php::Value pid = parameters[1]; 61 | Php::Value trace = parameters[2]; 62 | Php::Value callback = parameters[3]; 63 | 64 | // get the actual base object 65 | Loop *loop = (Loop *)loopParam.implementation(); 66 | 67 | // create the actual timer-watcher 68 | _watcher = std::make_shared(loop->mainLoop(), pid, trace, [callback](int, int) -> bool { 69 | 70 | // pass the call on to PHP 71 | callback(); 72 | 73 | return true; 74 | }); 75 | } 76 | 77 | /** 78 | * Start the signal watcher 79 | * @return bool 80 | */ 81 | Php::Value start() 82 | { 83 | return _watcher->start(); 84 | } 85 | 86 | /** 87 | * Cancel the signal watcher 88 | * @return bool 89 | */ 90 | Php::Value cancel() 91 | { 92 | return _watcher->cancel(); 93 | } 94 | 95 | }; 96 | 97 | /** 98 | * End of namespace 99 | */ 100 | } 101 | -------------------------------------------------------------------------------- /core/resolver.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Resolver.cpp 3 | * 4 | * @copyright 2014 Copernica BV 5 | */ 6 | 7 | /** 8 | * Dependencies 9 | */ 10 | #include "resolver.h" 11 | #include "resolverresult.h" 12 | #include "loop.h" 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Alternative constructor 22 | * @param loop 23 | */ 24 | void Resolver::__construct(Php::Parameters ¶ms) 25 | { 26 | // retrieve the parameters 27 | Php::Value loopParam = params[0]; 28 | 29 | // get the actual base object 30 | Loop *loop = (Loop *)loopParam.implementation(); 31 | 32 | // create the actual resolver 33 | _resolver = std::make_shared(loop->loop()); 34 | } 35 | 36 | /** 37 | * Find all IP addresses for a certain domain 38 | * @param domain The domain to fetch the IPs for 39 | * @param version IP version, can be 4 or 6 40 | * @param callback 41 | * @return bool 42 | */ 43 | Php::Value Resolver::ip(Php::Parameters ¶ms) 44 | { 45 | // retrieve the parameters 46 | Php::Value domain = params[0]; 47 | Php::Value callback = params[1]; 48 | 49 | // call the React::Dns::Resolver::ip function 50 | _resolver->ip(domain, [callback](React::Dns::IpResult &&ips, const char *error) { 51 | 52 | // check for error 53 | if (error) throw Php::Exception(error); 54 | 55 | // call the PHP callback 56 | callback(); 57 | 58 | // fetch all IPs 59 | for (auto &ip : ips) std::cout << ip << std::endl; 60 | }); 61 | 62 | // done 63 | return true; 64 | } 65 | 66 | /** 67 | * Find all MX records for a certain domain 68 | * @param domain The domain name to search MX records for 69 | * @param callback Callback that is called when found 70 | * @return bool 71 | */ 72 | Php::Value Resolver::mx(Php::Parameters ¶ms) 73 | { 74 | // retrieve the parameters 75 | Php::Value domain = params[0]; 76 | Php::Value callback = params[1]; 77 | 78 | // call the React::Dns::Resolver::mx function 79 | _resolver->mx(domain, [callback](React::Dns::MxResult &&mxs, const char *error) { 80 | 81 | // check for error 82 | if (error) throw Php::Exception(error); 83 | 84 | // call the PHP callback 85 | callback(); 86 | 87 | // fetch all MX records 88 | for (auto &record : mxs) std::cout << record << std::endl; 89 | }); 90 | 91 | // done 92 | return true; 93 | } 94 | 95 | /** 96 | * End of namespace 97 | */ 98 | } 99 | -------------------------------------------------------------------------------- /core/timeoutwatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * TimeoutWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Timeout watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class TimeoutWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Timeout watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | TimeoutWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Timeout watcher 40 | * @var std::shared_ptr 41 | */ 42 | TimeoutWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~TimeoutWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param timeout 53 | * @param callback 54 | */ 55 | void __construct(Php::Parameters ¶meters) 56 | { 57 | // retrieve the parameters 58 | Php::Value loopParam = parameters[0]; 59 | React::Timestamp timeout = parameters[1]; 60 | Php::Value callback = parameters[2]; 61 | 62 | // get the actual base object 63 | Loop *loop = (Loop *)loopParam.implementation(); 64 | 65 | // create the actual timer-watcher 66 | _watcher = std::make_shared(loop->loop(), timeout, [callback]() -> bool { 67 | 68 | // pass the call on to PHP 69 | callback(); 70 | 71 | return false; 72 | }); 73 | } 74 | 75 | /** 76 | * Start the timer 77 | * This is only meaningful if the timer is not yet running 78 | * @return bool 79 | */ 80 | Php::Value start() 81 | { 82 | return _watcher->start(); 83 | } 84 | 85 | /** 86 | * Cancel the timer 87 | * @return bool 88 | */ 89 | Php::Value cancel() 90 | { 91 | return _watcher->cancel(); 92 | } 93 | 94 | /** 95 | * Set the timeout 96 | * @param timeout 97 | * @return bool 98 | */ 99 | Php::Value set(Php::Parameters ¶meters) 100 | { 101 | return _watcher->set(parameters[0]); 102 | } 103 | }; 104 | 105 | /** 106 | * End of namespace 107 | */ 108 | } 109 | -------------------------------------------------------------------------------- /core/connection.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Connection.cpp 3 | * 4 | * Class representing a connection to a MySQL or MariaDB daemon 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include "connection.h" 14 | #include "loop.h" 15 | #include "result.h" 16 | 17 | /** 18 | * Set up namespace 19 | */ 20 | namespace ReactPhp { 21 | 22 | /** 23 | * Establish a connection to mysql 24 | * 25 | * @param loop the loop to bind to 26 | * @param hostname the hostname to connect to 27 | * @param username the username to login with 28 | * @param password the password to authenticate with 29 | * @param database the database to use 30 | * @param callback the callback to inform once the connection is established or failed 31 | */ 32 | void Connection::__construct(Php::Parameters ¶ms) 33 | { 34 | // retrieve the parameters 35 | Php::Value loopParam = params[0]; 36 | Php::Value hostname = params[1]; 37 | Php::Value username = params[2]; 38 | Php::Value password = params[3]; 39 | Php::Value database = params[4]; 40 | Php::Value callback = params[5]; 41 | 42 | // get the actual base objects 43 | Loop *loop = (Loop *)loopParam.implementation(); 44 | 45 | // create the actual connection 46 | _connection = std::make_shared(loop->loop(), hostname, username, password, database, [callback](React::MySQL::Connection *connection, const char *error) { 47 | 48 | if (error) throw Php::Exception(error); 49 | 50 | // call the PHP callback 51 | callback(); 52 | }); 53 | } 54 | 55 | /** 56 | * Execute a query 57 | * 58 | * @param query the query to execute 59 | * @param callback the callback to inform for all the result sets generated by the query 60 | */ 61 | void Connection::query(Php::Parameters ¶meters) 62 | { 63 | // retrieve the parameters 64 | Php::Value query = parameters[0]; 65 | Php::Value callback = parameters[1]; 66 | 67 | // perform the actual query 68 | _connection->query(query, [callback](React::MySQL::Result&& result, const char *error) { 69 | 70 | // check if there is a query error 71 | if (error) throw Php::Exception(error); 72 | 73 | // wrap the result in a PHP object 74 | Result *object = new Result(std::move(result)); 75 | 76 | // call the PHP callback 77 | callback(Php::Object("Async\\Result", object)); 78 | 79 | // stop the application 80 | exit(0); 81 | }); 82 | } 83 | 84 | /** 85 | * End of namespace 86 | */ 87 | } 88 | -------------------------------------------------------------------------------- /core/connection.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Connection.h 3 | * 4 | * Class representing a connection to a MySQL daemon 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | 16 | /** 17 | * Set up namespace 18 | */ 19 | namespace ReactPhp { 20 | 21 | /** 22 | * Class definition 23 | */ 24 | class Connection : public Php::Base 25 | { 26 | private: 27 | /** 28 | * The actual connection object 29 | * @var std::shared_ptr 30 | */ 31 | std::shared_ptr _connection; 32 | 33 | public: 34 | /** 35 | * Constructor 36 | */ 37 | Connection() {} 38 | 39 | /** 40 | * Pass in an existing connection 41 | * @var React::MySQL::Connection 42 | */ 43 | Connection(const std::shared_ptr &connection) : _connection(connection) {} 44 | 45 | /** 46 | * Destructor 47 | */ 48 | virtual ~Connection() {} 49 | 50 | /** 51 | * Get access to the internal connection object that is wrapped 52 | * by this PHP connection class 53 | * @return React::MySQL::Connection 54 | */ 55 | React::MySQL::Connection *conn() 56 | { 57 | React::MySQL::Connection *_conn = _connection.get(); 58 | 59 | return _conn; 60 | } 61 | 62 | /** 63 | * Establish a connection to mysql 64 | * 65 | * @param loop the loop to bind to 66 | * @param hostname the hostname to connect to 67 | * @param username the username to login with 68 | * @param password the password to authenticate with 69 | * @param database the database to use 70 | * @param callback the callback to inform once the connection is established or failed 71 | */ 72 | void __construct(Php::Parameters ¶ms); 73 | 74 | /** 75 | * Parse the string and replace all placeholders with 76 | * the provided values. 77 | * 78 | * The callback is executed with the result. 79 | * 80 | * @param query the query to parse 81 | * @param callback the callback to give the result 82 | * @param parameters placeholder values 83 | * @param count number of placeholder values 84 | */ 85 | void prepare(Php::Parameters ¶ms); 86 | 87 | /** 88 | * Execute a query 89 | * 90 | * @param query the query to execute 91 | * @param callback the callback to inform for all the result sets generated by the query 92 | */ 93 | void query(Php::Parameters ¶meters); 94 | 95 | }; 96 | 97 | /** 98 | * End of namespace 99 | */ 100 | } 101 | -------------------------------------------------------------------------------- /core/resultfield.h: -------------------------------------------------------------------------------- 1 | /** 2 | * ResultField.h 3 | * 4 | * A single field from a single row from 5 | * a MySQL result set 6 | * 7 | * @copyright 2014 Copernica BV 8 | */ 9 | 10 | /** 11 | * Dependencies 12 | */ 13 | #include 14 | #include 15 | #include 16 | 17 | /** 18 | * Set up namespace 19 | */ 20 | namespace ReactPhp { 21 | 22 | /** 23 | * Class definition 24 | */ 25 | class ResultField : public Php::Base 26 | { 27 | private: 28 | /** 29 | * The actual result field object 30 | * @var React::MySQL::ResultField 31 | */ 32 | React::MySQL::ResultField _resultField; 33 | 34 | public: 35 | /** 36 | * Constructor 37 | */ 38 | ResultField(); 39 | 40 | /** 41 | * Pass in an existing result field 42 | * @param resultField Movable result field object 43 | */ 44 | ResultField(React::MySQL::ResultField &&resultField) : _resultField(std::move(resultField)) {} 45 | 46 | /** 47 | * Destructor 48 | */ 49 | virtual ~ResultField() {} 50 | 51 | /** 52 | * Get whether this field is NULL 53 | */ 54 | bool isNULL() const 55 | { 56 | return _resultField.isNULL(); 57 | } 58 | 59 | /** 60 | * Cast to a number 61 | * 62 | * Note that if the value is NULL, this will yield 0. 63 | * To check for NULL values, use the isNULL function. 64 | * 65 | * @throws std::invalid_argument if this field does not contain a number 66 | * @throws std::out_of_range if the value is too small or too big to fit in the requested type 67 | */ 68 | operator int8_t() const {return _resultField.operator int8_t();} 69 | operator uint16_t() const {return _resultField.operator uint16_t();} 70 | operator int16_t() const {return _resultField.operator int16_t();} 71 | operator uint32_t() const {return _resultField.operator uint32_t();} 72 | operator int32_t() const {return _resultField.operator int32_t();} 73 | operator uint64_t() const {return _resultField.operator uint64_t();} 74 | operator int64_t() const {return _resultField.operator int64_t();} 75 | operator float() const {return _resultField.operator float();} 76 | operator double() const {return _resultField.operator double();} 77 | 78 | /** 79 | * Cast to a string 80 | * 81 | * Note that if the value is NULL, this will yield 82 | * an empty string. To check for NULL values, use 83 | * the isNULL function. 84 | */ 85 | operator std::string() const 86 | { 87 | return _resultField.operator std::string(); 88 | } 89 | }; 90 | 91 | /** 92 | * End of namespace 93 | */ 94 | } 95 | -------------------------------------------------------------------------------- /core/intervalwatcher.h: -------------------------------------------------------------------------------- 1 | /** 2 | * IntervalWatcher.h 3 | * 4 | * Wrapper around the REACT-CPP Interval watcher class 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class IntervalWatcher : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The actual Interval watcher object 28 | * @var std::shared_ptr 29 | */ 30 | std::shared_ptr _watcher; 31 | 32 | public: 33 | /** 34 | * Constructor 35 | */ 36 | IntervalWatcher() {} 37 | 38 | /** 39 | * Pass in an existing Interval watcher 40 | * @var std::shared_ptr 41 | */ 42 | IntervalWatcher(const std::shared_ptr &watcher) : _watcher(watcher) {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~IntervalWatcher() {} 48 | 49 | /** 50 | * Alternative constructor 51 | * @param loop 52 | * @param Initial timeout 53 | * @param Timeout interval period 54 | * @param Function that is called when timer is expired 55 | */ 56 | void __construct(Php::Parameters ¶meters) 57 | { 58 | // retrieve the parameters 59 | Php::Value loopParam = parameters[0]; 60 | React::Timestamp initial = parameters[1]; 61 | React::Timestamp interval = parameters[2]; 62 | Php::Value callback = parameters[3]; 63 | 64 | // get the actual base object 65 | Loop *loop = (Loop *)loopParam.implementation(); 66 | 67 | // create the actual interval-watcher 68 | _watcher = std::make_shared(loop->loop(), initial, interval, [callback]() -> bool{ 69 | 70 | // pass the call on to PHP 71 | callback(); 72 | 73 | return true; 74 | }); 75 | } 76 | 77 | /** 78 | * Start the timer 79 | * This is only meaningful if the timer is not yet running 80 | * @return bool 81 | */ 82 | Php::Value start() 83 | { 84 | return _watcher->start(); 85 | } 86 | 87 | /** 88 | * Cancel the timer 89 | * @return bool 90 | */ 91 | Php::Value cancel() 92 | { 93 | return _watcher->cancel(); 94 | } 95 | 96 | /** 97 | * Set the timer to a new time 98 | * @param initial Initial timeout for the first timeout 99 | * @param interval Interval for subsequent timeouts 100 | * @return bool 101 | */ 102 | Php::Value set(Php::Parameters ¶meters) 103 | { 104 | return _watcher->set(parameters[0], parameters[1]); 105 | } 106 | 107 | }; 108 | 109 | /** 110 | * End of namespace 111 | */ 112 | } 113 | -------------------------------------------------------------------------------- /core/statement.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Statement.cpp 3 | * 4 | * Class for executing prepared statements 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include "statement.h" 13 | #include "connection.h" 14 | #include "parameter.h" 15 | #include "result.h" 16 | 17 | /** 18 | * Set up namespace 19 | */ 20 | namespace ReactPhp { 21 | 22 | /** 23 | * Constructor 24 | * 25 | * @param connection the connection to run the statement on 26 | * @param statement the statement to execute 27 | * @param callback the callback to inform of success or failure 28 | */ 29 | void Statement::__construct(Php::Parameters ¶meters) 30 | { 31 | // retrieve the parameters 32 | Php::Value connectionParam = parameters[0]; 33 | Php::Value state = parameters[1]; 34 | Php::Value callback = parameters[2]; 35 | 36 | // get the actual base objects 37 | Connection *connection = (Connection *)connectionParam.implementation(); 38 | 39 | // create the actual statement 40 | _statement = std::make_shared(connection->conn(), state, [callback] (React::MySQL::Statement *statement, const char *error) { 41 | 42 | // check for possible errors 43 | if (error) throw Php::Exception(error); 44 | 45 | // call the PHP callback 46 | callback(); 47 | }); 48 | } 49 | 50 | /** 51 | * Execute statement with given parameters 52 | * 53 | * @param parameters input parameters 54 | * @param count number of parameters 55 | * @param callback callback to inform of success or failure 56 | */ 57 | void Statement::execute(Php::Parameters ¶meters) 58 | { 59 | // retrieve the parameters 60 | Php::Value callback = parameters[0]; 61 | 62 | // get the actual base objects 63 | //Parameter *parameter = (Parameter *)parameterParam.implementation(); 64 | 65 | // call the React::MySQL::Statement execute function 66 | _statement->execute([callback](React::MySQL::Result&& result, const char *error) { 67 | 68 | // check for possible errors 69 | if (error) throw Php::Exception(error); 70 | 71 | // call the PHP callback 72 | callback(); 73 | }); 74 | } 75 | 76 | /** 77 | * Execute statement query with given parameters 78 | * 79 | * @param callback callback to inform of success or failure 80 | */ 81 | void Statement::executeQuery(Php::Parameters ¶meters) 82 | { 83 | // retrieve the parameters 84 | Php::Value callback = parameters[0]; 85 | 86 | // call the React::MySQL::Statement execute function 87 | _statement->execute([callback](React::MySQL::Result&& result, const char *error) { 88 | 89 | // check if there is a query error 90 | if (error) throw Php::Exception(error); 91 | 92 | // wrap the result in a PHP object 93 | Result *object = new Result(std::move(result)); 94 | 95 | // call the PHP callback 96 | callback(Php::Object("Async\\Result", object)); 97 | }); 98 | } 99 | 100 | /** 101 | * End of namespace 102 | */ 103 | } 104 | -------------------------------------------------------------------------------- /core/resolverresult.h: -------------------------------------------------------------------------------- 1 | /** 2 | * ResolverResultResult.h 3 | * 4 | * Implementation-only class that parses the result of IP and MX records 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Set up namespace 11 | */ 12 | namespace ReactPhp{ 13 | 14 | /** 15 | * Class definition 16 | */ 17 | class ResolverResult : public Php::Base, public Php::Traversable 18 | { 19 | private: 20 | /** 21 | * The actual MxResult object 22 | * @var React::Dns::MxResult 23 | */ 24 | React::Dns::MxResult _mxResult; 25 | 26 | public: 27 | 28 | class ResolverResultIterator : public Php::Iterator 29 | { 30 | private: 31 | /** 32 | * The result that is being iterated over 33 | * This is a pointer to the actual result 34 | * @var React::MySQL::MxResult 35 | */ 36 | React::Dns::MxResult *_mxResult; 37 | 38 | /** 39 | * The current position in the result set 40 | * @var int 41 | */ 42 | int _current = 0; 43 | 44 | public: 45 | /** 46 | * Constructor 47 | */ 48 | ResolverResultIterator(); 49 | 50 | /** 51 | * Constructor 52 | * @param object The Php::Base object that is being iterated over 53 | * @param result The result from the database query 54 | */ 55 | ResolverResultIterator(ResolverResult *object, React::Dns::MxResult *mxResult) : 56 | Php::Iterator(object), _mxResult(mxResult) {} 57 | 58 | /** 59 | * Destructor 60 | */ 61 | virtual ~ResolverResultIterator() {} 62 | 63 | /** 64 | * Is the iterator on a valid position 65 | * @return bool 66 | */ 67 | virtual bool valid() override 68 | { 69 | // the iterator is valid as long as the current position points 70 | // to a valid row 71 | return true; 72 | } 73 | 74 | /** 75 | * The value at the current position 76 | * @return Php::Object 77 | */ 78 | virtual Php::Value current() override 79 | { 80 | return _mxResult; 81 | } 82 | 83 | /** 84 | * The key at the current position 85 | * @return Value 86 | */ 87 | virtual Php::Value key() override 88 | { 89 | // this is simply the current index 90 | return _current; 91 | } 92 | 93 | /** 94 | * Move to the next position 95 | */ 96 | virtual void next() override 97 | { 98 | // move to the next position 99 | _current++; 100 | } 101 | 102 | /** 103 | * Rewind the iterator to the front position 104 | */ 105 | virtual void rewind() override 106 | { 107 | // go back to the beginning 108 | _current = 0; 109 | } 110 | }; 111 | 112 | /** 113 | * Constructor 114 | */ 115 | ResolverResult() {} 116 | 117 | /** 118 | * Pass in an existing result 119 | * @param result Movable result object 120 | */ 121 | ResolverResult(React::Dns::MxResult &&mxResult) : _mxResult(std::move(mxResult)) {} 122 | 123 | /** 124 | * Destructor 125 | */ 126 | virtual ~ResolverResult() {} 127 | 128 | /** 129 | * Get the iterator 130 | * @return Php::Iterator 131 | */ 132 | virtual Php::Iterator *getIterator() override 133 | { 134 | // construct a new result iterator on the heap 135 | // the (PHP-CPP library will delete it when ready) 136 | return new ResolverResultIterator(this, &_mxResult); 137 | } 138 | }; 139 | 140 | /** 141 | * End of namespace 142 | */ 143 | } 144 | -------------------------------------------------------------------------------- /core/result.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Result.h 3 | * 4 | * Class representing a MySQL result set 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include "resultrow.h" 17 | 18 | /** 19 | * Set up namespace 20 | */ 21 | namespace ReactPhp { 22 | 23 | /** 24 | * Class definition 25 | */ 26 | class Result : public Php::Base, public Php::Traversable 27 | { 28 | private: 29 | /** 30 | * The actual result object 31 | * @var React::MySQL::Result 32 | */ 33 | React::MySQL::Result _result; 34 | 35 | public: 36 | 37 | class ResultIterator : public Php::Iterator 38 | { 39 | private: 40 | /** 41 | * The result that is being iterated over 42 | * This is a pointer to the actual result 43 | * @var React::MySQL::Result 44 | */ 45 | React::MySQL::Result *_result; 46 | 47 | /** 48 | * The current position in the result set 49 | * @var int 50 | */ 51 | int _current = 0; 52 | 53 | public: 54 | /** 55 | * Constructor 56 | */ 57 | ResultIterator(); 58 | 59 | /** 60 | * Constructor 61 | * @param object The Php::Base object that is being iterated over 62 | * @param result The result from the database query 63 | */ 64 | ResultIterator(Result *object, React::MySQL::Result *result) : 65 | Php::Iterator(object), _result(result) {} 66 | 67 | /** 68 | * Destructor 69 | */ 70 | virtual ~ResultIterator() {} 71 | 72 | /** 73 | * Is the iterator on a valid position 74 | * @return bool 75 | */ 76 | virtual bool valid() override 77 | { 78 | // the iterator is valid as long as the current position points 79 | // to a valid row 80 | return (unsigned int)_current < _result->size(); 81 | } 82 | 83 | /** 84 | * The value at the current position 85 | * @return Php::Object 86 | */ 87 | virtual Php::Value current() override 88 | { 89 | // create an instance of a single row 90 | ResultRow *row = new ResultRow(_result->operator[](_current)); 91 | 92 | // return the PHP object 93 | return Php::Object("Async\\ResultRow", row); 94 | } 95 | 96 | /** 97 | * The key at the current position 98 | * @return Value 99 | */ 100 | virtual Php::Value key() override 101 | { 102 | // this is simply the current index 103 | return _current; 104 | } 105 | 106 | /** 107 | * Move to the next position 108 | */ 109 | virtual void next() override 110 | { 111 | // move to the next position 112 | _current++; 113 | } 114 | 115 | /** 116 | * Rewind the iterator to the front position 117 | */ 118 | virtual void rewind() override 119 | { 120 | // go back to the beginning 121 | _current = 0; 122 | } 123 | }; 124 | 125 | /** 126 | * Constructor 127 | */ 128 | Result(); 129 | 130 | /** 131 | * Pass in an existing result 132 | * @param result Movable result object 133 | */ 134 | Result(React::MySQL::Result &&result) : _result(std::move(result)) {} 135 | 136 | /** 137 | * Destructor 138 | */ 139 | virtual ~Result() {} 140 | 141 | /** 142 | * Get the iterator 143 | * @return Php::Iterator 144 | */ 145 | virtual Php::Iterator *getIterator() override 146 | { 147 | // construct a new result iterator on the heap 148 | // the (PHP-CPP library will delete it when ready) 149 | return new ResultIterator(this, &_result); 150 | } 151 | 152 | /** 153 | * Retrieve row at given offset 154 | */ 155 | void fetchRow(Php::Parameters ¶ms); 156 | 157 | /** 158 | * Is this a valid result? 159 | */ 160 | Php::Value valid(); 161 | 162 | /** 163 | * The number of rows affected 164 | */ 165 | size_t affectedRows(); 166 | 167 | /** 168 | * Get the number of rows in this result 169 | */ 170 | Php::Value size(); 171 | 172 | /** 173 | * Retrieve iterator for first row 174 | */ 175 | Php::Value begin(); 176 | 177 | /** 178 | * Retrieve iterator past the end 179 | */ 180 | Php::Value end(); 181 | }; 182 | 183 | /** 184 | * End of namespace 185 | */ 186 | } 187 | -------------------------------------------------------------------------------- /core/resultrow.h: -------------------------------------------------------------------------------- 1 | /** 2 | * ResultRow.h 3 | * 4 | * Class with result data for a single MySQL row 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | #include 15 | #include "resultfield.h" 16 | #include 17 | 18 | /** 19 | * Set up namespace 20 | */ 21 | namespace ReactPhp { 22 | 23 | /** 24 | * Class definition 25 | */ 26 | class ResultRow : public Php::Base, public Php::Traversable 27 | { 28 | private: 29 | /** 30 | * The actual resultRow object 31 | * @var React::MySQL::ResultRow 32 | */ 33 | React::MySQL::ResultRow _resultRow; 34 | 35 | public: 36 | 37 | class ResultRowIterator : public Php::Iterator 38 | { 39 | private: 40 | /** 41 | * The result that is being iterated over 42 | * This is a pointer to the actual result row 43 | * @var React::MySQL::ResultRow 44 | */ 45 | React::MySQL::ResultRow *_resultRow; 46 | 47 | /** 48 | * The current position in the result set 49 | * @var int 50 | */ 51 | int _current = 0; 52 | 53 | public: 54 | /** 55 | * Constructor 56 | */ 57 | ResultRowIterator(); 58 | 59 | /** 60 | * Constructor 61 | * @param object The Php::Base object that is being iterated over 62 | * @param result The result from the database query 63 | */ 64 | ResultRowIterator(ResultRow *object, React::MySQL::ResultRow *resultRow) : 65 | Php::Iterator(object), _resultRow(resultRow) {} 66 | 67 | /** 68 | * Destructor 69 | */ 70 | virtual ~ResultRowIterator() {} 71 | 72 | /** 73 | * Is the iterator on a valid position 74 | * @return bool 75 | */ 76 | virtual bool valid() override 77 | { 78 | // the iterator is valid as long as the current position points 79 | // to a valid field 80 | return (unsigned int)_current < _resultRow->size(); 81 | } 82 | 83 | /** 84 | * The value at the current position 85 | * @return Value 86 | */ 87 | virtual Php::Value current() override 88 | { 89 | // create an instance of a single field 90 | auto field = _resultRow->operator[](_current); 91 | 92 | // return the field 93 | return (std::string)field; 94 | } 95 | 96 | /** 97 | * The key at the current position 98 | * @return Value 99 | */ 100 | virtual Php::Value key() override 101 | { 102 | // this is simply the current index 103 | return _current; 104 | } 105 | 106 | /** 107 | * Move to the next position 108 | */ 109 | virtual void next() override 110 | { 111 | // move to the next position 112 | _current++; 113 | } 114 | 115 | /** 116 | * Rewind the iterator to the front position 117 | */ 118 | virtual void rewind() override 119 | { 120 | // go back to the beginning 121 | _current = 0; 122 | } 123 | }; 124 | 125 | /** 126 | * Constructor 127 | */ 128 | ResultRow(); 129 | 130 | /** 131 | * Pass in an existing resultRow 132 | * @var React::MySQL::ResultRow 133 | */ 134 | ResultRow(React::MySQL::ResultRow &&resultRow) : _resultRow(std::move(resultRow)) {} 135 | 136 | /** 137 | * Destructor 138 | */ 139 | virtual ~ResultRow() {} 140 | 141 | /** 142 | * Get the iterator 143 | * @return Php::Iterator 144 | */ 145 | virtual Php::Iterator *getIterator() override 146 | { 147 | // construct a new result iterator on the heap 148 | // the (PHP-CPP library will delete it when ready) 149 | return new ResultRowIterator(this, &_resultRow); 150 | } 151 | 152 | /** 153 | * Get the number of fields in the row 154 | * @return size_t 155 | */ 156 | size_t size() const; 157 | 158 | /** 159 | * Retrieve a field by index 160 | * 161 | * This function throws an exception if no field 162 | * exists under the given index (i.e. index 163 | * is not smaller than size()). 164 | * 165 | * @param index field index 166 | */ 167 | Php::Value operator [] (size_t index) const; 168 | 169 | /** 170 | * Retrieve a field by name 171 | * 172 | * This function throws an exception if no field 173 | * exists under the given key. 174 | * 175 | * @param key field name 176 | */ 177 | Php::Value operator [] (const std::string &key) const; 178 | }; 179 | 180 | /** 181 | * End of namespace 182 | */ 183 | } 184 | -------------------------------------------------------------------------------- /core/loop.h: -------------------------------------------------------------------------------- 1 | /** 2 | * Loop.h 3 | * 4 | * Main event loop class that runs the main PHP event loop 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include 14 | 15 | /** 16 | * Set up namespace 17 | */ 18 | namespace ReactPhp { 19 | 20 | /** 21 | * Class definition 22 | */ 23 | class Loop : public Php::Base 24 | { 25 | private: 26 | /** 27 | * The loop object from the React-CPP library 28 | * @var React::Loop 29 | */ 30 | React::Loop _loop; 31 | 32 | /** 33 | * The main loop object from the React-CPP library 34 | * @var React::MainLoop 35 | */ 36 | React::MainLoop _mainLoop; 37 | 38 | public: 39 | /** 40 | * Constructor 41 | */ 42 | Loop() {} 43 | 44 | /** 45 | * Destructor 46 | */ 47 | virtual ~Loop() {} 48 | 49 | /** 50 | * Get access to the internal loop object that is wrapped 51 | * by this PHP loop class 52 | * @return React::Loop 53 | */ 54 | React::Loop *loop() 55 | { 56 | return &_loop; 57 | } 58 | 59 | /** 60 | * Get access to the internal main loop object that is wrapped 61 | * by this PHP loop class 62 | * @return React::MainLoop 63 | */ 64 | React::MainLoop *mainLoop() 65 | { 66 | return &_mainLoop; 67 | } 68 | 69 | /** 70 | * The current time 71 | * @return double 72 | */ 73 | Php::Value now() 74 | { 75 | return _loop.now(); 76 | } 77 | 78 | /** 79 | * Run the loop 80 | * @return bool 81 | */ 82 | Php::Value run() 83 | { 84 | return _loop.run(); 85 | } 86 | 87 | /** 88 | * Stop the loop 89 | * @return bool 90 | */ 91 | Php::Value stop() 92 | { 93 | _loop.stop(); 94 | return true; 95 | } 96 | 97 | /** 98 | * Run one iteration of the event loop 99 | * @return bool 100 | */ 101 | Php::Value step() 102 | { 103 | return _loop.step(); 104 | } 105 | 106 | /** 107 | * Resume the loop after it was suspended 108 | */ 109 | void resume() 110 | { 111 | _loop.resume(); 112 | } 113 | 114 | /** 115 | * Suspend the loop. While the loop is suspended, timers will not be processed, 116 | * and the time for the timers does not proceed. Once the loop is resumed, the 117 | * timers continue to run. 118 | */ 119 | void suspend() 120 | { 121 | _loop.suspend(); 122 | } 123 | 124 | /** 125 | * Run a timeout after a while 126 | * @param timeout Number of seconds (as a float) to wait for the timer to be called 127 | * @param callback Function that is called when timer expires 128 | * @return object TimeoutWatcher object that can be used for cancelling the timer 129 | */ 130 | Php::Value onTimeout(Php::Parameters ¶meters); 131 | 132 | /** 133 | * Call a callback with a certain interval 134 | * @param interval Number of seconds for each iteration 135 | * @param callback Function that is called every timeout seconds 136 | * @return object IntervalWatcher object that can be used for cancelling the timer 137 | */ 138 | Php::Value onInterval(Php::Parameters ¶meters); 139 | 140 | /** 141 | * Function which is called the moment a file descriptor becomes readable 142 | * @param fd The file descriptor 143 | * @param callback Function that is called when the file descriptor is readable 144 | * @return object ReadWatcher object that can be used for cancelling the timer 145 | */ 146 | Php::Value onReadable(Php::Parameters ¶meters); 147 | 148 | /** 149 | * Function which is called the moment a file descriptor becomes writable 150 | * @param fd The file descriptor 151 | * @param callback Function that is called when the file descriptor is readable 152 | * @return object WriteWatcher object that can be used for cancelling the timer 153 | */ 154 | Php::Value onWritable(Php::Parameters ¶meters); 155 | 156 | /** 157 | * Register a synchronize function 158 | * @param callback Function that is called when the file descriptor is readable 159 | * @return object SynchronizeWatcher object that can be used for cancelling the timer 160 | */ 161 | Php::Value onSynchronize(Php::Parameters ¶meters); 162 | 163 | /** 164 | * Register a function that is called the moment a signal is fired. 165 | * @param signum The signal 166 | * @param callback Function that is called the moment the signal is caught 167 | * @return Object that can be used to stop checking for signals 168 | */ 169 | Php::Value onSignal(Php::Parameters ¶meters); 170 | 171 | /** 172 | * Register a function that is called the moment the status of a child changes 173 | * @param pid The child PID 174 | * @param trace Monitor for all status changes (true) or only for child exits (false) 175 | * @param callback Function that is called the moment the child changes status 176 | * @return Object that can be used to stop checking for status changes 177 | */ 178 | Php::Value onStatusChange(Php::Parameters ¶meters); 179 | }; 180 | 181 | /** 182 | * End of namespace 183 | */ 184 | }; 185 | -------------------------------------------------------------------------------- /core/loop.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Loop.cpp 3 | * 4 | * Implementation of the event loop 5 | * 6 | * @copyright 2014 Copernica BV 7 | */ 8 | 9 | /** 10 | * Dependencies 11 | */ 12 | #include 13 | #include "loop.h" 14 | #include "timeoutwatcher.h" 15 | #include "intervalwatcher.h" 16 | #include "readwatcher.h" 17 | #include "writewatcher.h" 18 | #include "synchronizewatcher.h" 19 | #include "signalwatcher.h" 20 | #include "statuswatcher.h" 21 | 22 | /** 23 | * Set up namespace 24 | */ 25 | namespace ReactPhp { 26 | 27 | /** 28 | * Function which is called the moment a file descriptor becomes readable 29 | * @param fd The file descriptor 30 | * @param callback Function that is called when the file descriptor is readable 31 | * @return object ReadWatcher object that can be used for cancelling the timer 32 | */ 33 | Php::Value Loop::onReadable(Php::Parameters ¶meters) 34 | { 35 | // the filedescriptor and the callback function 36 | int fd = parameters[0]; 37 | Php::Value callback = parameters[1]; 38 | 39 | // call the Loop::onReadable function 40 | auto reader = _loop.onReadable(fd, [callback]() -> bool{ 41 | 42 | // call the PHP callback 43 | callback(); 44 | 45 | return true; 46 | }); 47 | 48 | // create a new PHP object, and return that 49 | return Php::Object("Async\\ReadWatcher", new ReadWatcher(reader)); 50 | } 51 | 52 | /** 53 | * Function which is called the moment a file descriptor becomes writable 54 | * @param fd The file descriptor 55 | * @param callback Function that is called when the file descriptor is readable 56 | * @return object WriteWatcher object that can be used for cancelling the timer 57 | */ 58 | Php::Value Loop::onWritable(Php::Parameters ¶meters) 59 | { 60 | // the filedescriptor and the callback function 61 | int fd = parameters[0]; 62 | Php::Value callback = parameters[1]; 63 | 64 | //call the Loop::onReadable function 65 | auto writer = _loop.onWritable(fd, [callback]() -> bool { 66 | 67 | //call the PHP callback 68 | callback(); 69 | 70 | return true; 71 | }); 72 | 73 | // create a new PHP object, and return that 74 | return Php::Object("Async\\WriteWatcher", new WriteWatcher(writer)); 75 | } 76 | 77 | /** 78 | * Register a synchronize function 79 | * @param callback Function that is called when the file descriptor is readable 80 | * @return object SynchronizeWatcher object that can be used for cancelling the timer 81 | */ 82 | Php::Value Loop::onSynchronize(Php::Parameters ¶meters) 83 | { 84 | // the callback function 85 | Php::Value callback = parameters[0]; 86 | 87 | //call the Loop::onReadable function 88 | auto synchronizer = _loop.onSynchronize([callback]() -> bool { 89 | 90 | //call the PHP callback 91 | callback(); 92 | 93 | return true; 94 | }); 95 | 96 | // create a new PHP object, and return that 97 | return Php::Object("Async\\SynchronizeWatcher", new SynchronizeWatcher(synchronizer)); 98 | } 99 | 100 | /** 101 | * Run a timeout after a while 102 | * @param timeout Number of seconds (as a float) to wait for the timer to be called 103 | * @param callback Function that is called when timer expires 104 | * @return object TimeoutWatcher object that can be used for cancelling the timer 105 | */ 106 | Php::Value Loop::onTimeout(Php::Parameters ¶meters) 107 | { 108 | // the timeout, and the callback function 109 | React::Timestamp timeout = parameters[0]; 110 | Php::Value callback = parameters[1]; 111 | 112 | // call the Loop::onTimeout function 113 | auto timer = _loop.onTimeout(timeout, [callback]() -> bool { 114 | 115 | // call the PHP callback 116 | callback(); 117 | 118 | return true; 119 | }); 120 | 121 | // create a new PHP object, and return that 122 | return Php::Object("Async\\TimeoutWatcher", new TimeoutWatcher(timer)); 123 | } 124 | 125 | /** 126 | * Call a callback with a certain interval 127 | * @param interval Number of seconds for each iteration 128 | * @param callback Function that is called every timeout seconds 129 | * @return object IntervalWatcher object that can be used for cancelling the timer 130 | */ 131 | Php::Value Loop::onInterval(Php::Parameters ¶meters) 132 | { 133 | // the interval, and the callback function 134 | React::Timestamp interval = parameters[0]; 135 | Php::Value callback = parameters[1]; 136 | 137 | // call the Loop::onInterval function 138 | auto watcher = _loop.onInterval(interval, interval, [callback]() -> bool { 139 | 140 | // call the PHP callback 141 | callback(); 142 | 143 | return true; 144 | }); 145 | 146 | // create a new PHP object, and return that 147 | return Php::Object("Async\\IntervalWatcher", new IntervalWatcher(watcher)); 148 | } 149 | 150 | /** 151 | * Register a function that is called the moment a signal is fired. 152 | * @param signum The signal 153 | * @param callback Function that is called the moment the signal is caught 154 | * @return Object that can be used to stop checking for signals 155 | */ 156 | Php::Value Loop::onSignal(Php::Parameters ¶meters) 157 | { 158 | // the signal and the callback function 159 | int signum = parameters[0]; 160 | Php::Value callback = parameters[1]; 161 | 162 | // call the MainLoop::onSignal function 163 | auto signal = _mainLoop.onSignal(signum, [callback] () -> bool { 164 | 165 | // call the PHP callback 166 | callback(); 167 | 168 | return false; 169 | }); 170 | 171 | return Php::Object("Async\\SignalWatcher", new SignalWatcher(signal)); 172 | } 173 | 174 | /** 175 | * Register a function that is called the moment the status of a child changes 176 | * @param pid The child PID 177 | * @param trace Monitor for all status changes (true) or only for child exits (false) 178 | * @param callback Function that is called the moment the child changes status 179 | * @return Object that can be used to stop checking for status changes 180 | */ 181 | Php::Value Loop::onStatusChange(Php::Parameters ¶meters) 182 | { 183 | // the pid, trace and callback function 184 | Php::Value pid = parameters[0]; 185 | Php::Value trace = parameters[1]; 186 | Php::Value callback = parameters[2]; 187 | 188 | // call the MainLoop::onStatusChange function 189 | auto status = _mainLoop.onStatusChange(pid, trace, [callback] (int, int) -> bool { 190 | 191 | // call the PHP callback 192 | callback(); 193 | 194 | return true; 195 | }); 196 | 197 | return Php::Object("Async\\StatusWatcher", new StatusWatcher(status)); 198 | } 199 | /** 200 | * End of namespace 201 | */ 202 | } 203 | 204 | -------------------------------------------------------------------------------- /core/extension.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Extension.cpp 3 | * 4 | * Starting point for the event loop extension. In this file all 5 | * classes and methods that are defined by the extension are 6 | * registered to PHP 7 | * 8 | * @copyright 2014 Copernica BV 9 | */ 10 | 11 | /** 12 | * Dependencies 13 | */ 14 | #include "loop.h" 15 | #include "timeoutwatcher.h" 16 | #include "intervalwatcher.h" 17 | #include "readwatcher.h" 18 | #include "writewatcher.h" 19 | #include "synchronizewatcher.h" 20 | #include "signalwatcher.h" 21 | #include "statuswatcher.h" 22 | #include "resolver.h" 23 | #include "connection.h" 24 | #include "statement.h" 25 | #include "parameter.h" 26 | #include "localparameter.h" 27 | #include "result.h" 28 | #include "resolverresult.h" 29 | 30 | // Symbols are exported according to the "C" language 31 | extern "C" 32 | { 33 | // export the "get_module" function that will be called by the Zend engine 34 | PHPCPP_EXPORT void *get_module() 35 | { 36 | // create extension 37 | static Php::Extension extension("REACT-PHP-CPP","0.1"); 38 | 39 | // create a namespace 40 | Php::Namespace async("Async"); 41 | 42 | // the loop class 43 | Php::Class loop("Loop"); 44 | loop.method("now", &ReactPhp::Loop::now); 45 | loop.method("run", &ReactPhp::Loop::run); 46 | loop.method("step", &ReactPhp::Loop::step); 47 | loop.method("stop", &ReactPhp::Loop::stop); 48 | loop.method("resume", &ReactPhp::Loop::resume); 49 | loop.method("suspend", &ReactPhp::Loop::suspend); 50 | loop.method("onTimeout", &ReactPhp::Loop::onTimeout); 51 | loop.method("onInterval", &ReactPhp::Loop::onInterval); 52 | loop.method("onReadable", &ReactPhp::Loop::onReadable); 53 | loop.method("onWritable", &ReactPhp::Loop::onWritable); 54 | loop.method("onSynchronize", &ReactPhp::Loop::onSynchronize); 55 | loop.method("onSignal", &ReactPhp::Loop::onSignal); 56 | loop.method("onStatusChange", &ReactPhp::Loop::onStatusChange); 57 | 58 | // the timer watcher class 59 | Php::Class timeoutWatcher("TimeoutWatcher"); 60 | timeoutWatcher.method("__construct", &ReactPhp::TimeoutWatcher::__construct); 61 | timeoutWatcher.method("start", &ReactPhp::TimeoutWatcher::start); 62 | timeoutWatcher.method("cancel", &ReactPhp::TimeoutWatcher::cancel); 63 | timeoutWatcher.method("set", &ReactPhp::TimeoutWatcher::set); 64 | 65 | // the read watcher class 66 | Php::Class readWatcher("ReadWatcher"); 67 | readWatcher.method("__construct", &ReactPhp::ReadWatcher::__construct); 68 | readWatcher.method("cancel", &ReactPhp::ReadWatcher::cancel); 69 | readWatcher.method("resume", &ReactPhp::ReadWatcher::resume); 70 | 71 | // the write watcher class 72 | Php::Class writeWatcher("WriteWatcher"); 73 | writeWatcher.method("__construct", &ReactPhp::WriteWatcher::__construct); 74 | writeWatcher.method("cancel", &ReactPhp::WriteWatcher::cancel); 75 | writeWatcher.method("resume", &ReactPhp::WriteWatcher::resume); 76 | 77 | // the interval watcher class 78 | Php::Class intervalWatcher("IntervalWatcher"); 79 | intervalWatcher.method("__construct", &ReactPhp::IntervalWatcher::__construct); 80 | intervalWatcher.method("start", &ReactPhp::IntervalWatcher::start); 81 | intervalWatcher.method("cancel", &ReactPhp::IntervalWatcher::cancel); 82 | intervalWatcher.method("set", &ReactPhp::IntervalWatcher::set); 83 | 84 | // the synchronize watcher class 85 | Php::Class synchronizeWatcher("SynchronizeWatcher"); 86 | synchronizeWatcher.method("__construct", &ReactPhp::SynchronizeWatcher::__construct); 87 | synchronizeWatcher.method("synchronize", &ReactPhp::SynchronizeWatcher::synchronize); 88 | synchronizeWatcher.method("cancel", &ReactPhp::SynchronizeWatcher::cancel); 89 | 90 | // the signal watcher class 91 | Php::Class signalWatcher("SignalWatcher"); 92 | signalWatcher.method("__construct", &ReactPhp::SignalWatcher::__construct); 93 | signalWatcher.method("start", &ReactPhp::SignalWatcher::start); 94 | signalWatcher.method("cancel", &ReactPhp::SignalWatcher::cancel); 95 | 96 | // the status watcher class 97 | Php::Class statusWatcher("StatusWatcher"); 98 | statusWatcher.method("__construct", &ReactPhp::StatusWatcher::__construct); 99 | statusWatcher.method("start", &ReactPhp::StatusWatcher::start); 100 | statusWatcher.method("cancel", &ReactPhp::StatusWatcher::cancel); 101 | 102 | // the resolver class 103 | Php::Class resolver("Resolver"); 104 | resolver.method("__construct", &ReactPhp::Resolver::__construct); 105 | resolver.method("ip", &ReactPhp::Resolver::ip); 106 | resolver.method("mx", &ReactPhp::Resolver::mx); 107 | 108 | // the connection class 109 | Php::Class connection("Connection"); 110 | connection.method("__construct", &ReactPhp::Connection::__construct); 111 | connection.method("query", &ReactPhp::Connection::query); 112 | 113 | // the statement class 114 | Php::Class statement("Statement"); 115 | statement.method("__construct", &ReactPhp::Statement::__construct); 116 | statement.method("execute", &ReactPhp::Statement::execute); 117 | statement.method("executeQuery", &ReactPhp::Statement::executeQuery); 118 | 119 | // the parameter class 120 | Php::Class parameter("Parameter"); 121 | parameter.method("__construct", &ReactPhp::Parameter::__construct); 122 | 123 | // the local parameter class 124 | Php::Class localParameter("Parameter"); 125 | localParameter.method("__construct", &ReactPhp::LocalParameter::__construct); 126 | 127 | // the result class 128 | Php::Class result("Result"); 129 | result.method("valid", &ReactPhp::Result::valid); 130 | result.method("size", &ReactPhp::Result::size); 131 | result.method("begin", &ReactPhp::Result::begin); 132 | result.method("end", &ReactPhp::Result::end); 133 | result.method("fetchRow", &ReactPhp::Result::fetchRow); 134 | 135 | // the result row class 136 | Php::Class resultRow("ResultRow"); 137 | 138 | // the result field class 139 | Php::Class resultField("ResultField"); 140 | 141 | // the resolver result class 142 | Php::Class resolverResult("ResolverResult"); 143 | 144 | // add all classes to the namespace 145 | async.add(loop); 146 | async.add(timeoutWatcher); 147 | async.add(readWatcher); 148 | async.add(writeWatcher); 149 | async.add(intervalWatcher); 150 | async.add(synchronizeWatcher); 151 | async.add(signalWatcher); 152 | async.add(statusWatcher); 153 | async.add(resolver); 154 | async.add(connection); 155 | async.add(statement); 156 | async.add(parameter); 157 | async.add(result); 158 | async.add(localParameter); 159 | async.add(resultRow); 160 | async.add(resultField); 161 | async.add(resolverResult); 162 | 163 | // add the namespace to the extension 164 | extension.add(async); 165 | 166 | // return the extension module 167 | return extension.module(); 168 | } 169 | } 170 | 171 | 172 | -------------------------------------------------------------------------------- /README.md~: -------------------------------------------------------------------------------- 1 | REACT-PHP-CPP 2 | ============= 3 | 4 | Event loop library for PHP implemented in C++. Support for asynchronous non-blocking sockets, DNS lookups and database connections. 5 | 6 | REACT-PHP-CPP utilizes the features of the PHP-CPP library and is a wrapper around the REACT-CPP and REACT-CPP-MYSQL libraries. Thus, it depends on the aforementioned libraries. 7 | 8 | 9 | Event Loop 10 | ========== 11 | 12 | The ReactPhp::Loop class is wrapped around the React::Loop class of the REACT-CPP library and runs the main PHP event loop. It contains methods to set timers, while it also utilizes callback functions which will be called whenever a filedescriptor becomes readable or writable. 13 | 14 | Below is a typical application, where an instance of the event loop is created, which can then be used to register timers and filedescriptors that will be checked for readability: 15 | 16 | ```php 17 | 18 | onTimeout(1, function() { 25 | 26 | echo("timeout 1 expires\n"); 27 | 28 | return false; 29 | 30 | }); 31 | 32 | // set a timeout -- alternative way 33 | $timer2 = new Async\TimeoutWatcher($loop, 2, function() { 34 | 35 | echo("timeout 2 expires\n"); 36 | 37 | return false; 38 | }); 39 | 40 | // notify us when input is available 41 | $reader1 = $loop->onReadable('STDIN_FILENO', function() { 42 | 43 | echo("Input is available at first reader\n"); 44 | 45 | return false; 46 | }); 47 | 48 | // notify us when input is available -- alternative way 49 | $reader2 = new Async\ReadWatcher($loop, 'STDIN_FILENO', function() { 50 | 51 | echo("Input is available at second reader\n"); 52 | 53 | return false; 54 | }); 55 | 56 | // the current time 57 | echo($loop->now()."\n"); 58 | 59 | // run the event loop 60 | $loop->run(); 61 | 62 | ?> 63 | 64 | ``` 65 | 66 | After the instance of the event loop is created, it should be run in order to register the timers and filedescriptors. 67 | 68 | 69 | Connection class 70 | ================ 71 | 72 | The ReactPhp::Connection class is wrapped around the React::MySQL::Connection class of the REACT-CPP-MYSQL library and it can be used to establish a connection to a MySQL daemon, as the following script illustrates: 73 | 74 | ```php 75 | 76 | run(); 91 | 92 | ?> 93 | 94 | ``` 95 | 96 | Again, an instance of the event loop needs to be created, so that the connection class can be used, whilst the callback function notifies us when the connection is established. 97 | 98 | 99 | Statements 100 | ========== 101 | 102 | Naturally, the next step after establishing a connection to a MySQL daemon is to use statements in order to define or manipulate a certain table in a database. 103 | 104 | To achieve this we can use the ReactPhp::Statement class, which is also wrapped around the React::MySQL::Statement class of the REACT-CPP-MYSQL library. First, a ReactPhp::Statement object needs to be created in the following fashion: 105 | 106 | ```php 107 | 108 | // initialize a statement 109 | $statement = new Async\Statement($connection, "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('A', 'B', 10), ('C', 'D', 20), ('E', 'F', 30)", function() { 110 | 111 | echo("Statement initialized\n"); 112 | 113 | return false; 114 | }); 115 | 116 | ``` 117 | 118 | From the above script it is obvious that the connection object is essential in order to create the statement object, since it constitutes the first parameter of the constructor. The second parameter is the actual MySQL statement and the third is the callback function, which notifies us that the statement can be executed. 119 | For the execution to take place, we need to call the ReactPhp::Statement::execute() function. However, when the statement is a SELECT statement, the ReactPhp::Statement::executeQuery() function needs to be called. This happens, because in the case of a query a result object needs to be passed as a parameter in the callback function, so that the result set can be dumped to the screen: 120 | 121 | ```php 122 | 123 | execute(function() { 146 | 147 | echo("Executing statement\n"); 148 | 149 | return false; 150 | 151 | }); 152 | 153 | // execute a query statement -- should only be used when we have a SELECT statement 154 | $statement->executeQuery(function($result) { 155 | 156 | echo("Executing a SELECT statement\n"); 157 | 158 | // iterate over the result set 159 | foreach ($result as $row) 160 | { 161 | // wrap the row in curly braces 162 | echo("{ "); 163 | 164 | // iterate over each individual row of the result set 165 | foreach ($row as $field) 166 | { 167 | // and dump all the result fields to the screen 168 | echo("$field "); 169 | } 170 | 171 | // close the row 172 | echo("}\n"); 173 | } 174 | 175 | return false; 176 | }); 177 | 178 | // run the event loop 179 | $loop->run(); 180 | 181 | ?> 182 | 183 | ``` 184 | 185 | That, practically, means that all MySQL statements are executed using the ReactPhp::Statement::execute() function, except the SELECT statement, which demands the ReactPhp::Statement::executeQuery() function to execute. 186 | This is the reason why there is a separate function, which can be used for executing queries only, namely the ReactPhp::Connection::query() function. As a result, whenever a query has to be executed, the best approach is to use the aforementioned function and not the ReactPhp::Statement::executeQuery() function, since the former is more straightforward: 187 | 188 | ```php 189 | 190 | query("SELECT * FROM Persons", function($result) { 205 | 206 | // iterate over the result set 207 | foreach ($result as $row) 208 | { 209 | // wrap each row in curly braces 210 | echo("{ "); 211 | 212 | // iterate over each individual row of the result set 213 | foreach ($row as $field) 214 | { 215 | // and dump all the result fields to the screen 216 | echo("$field "); 217 | } 218 | 219 | // close the row 220 | echo("}\n"); 221 | } 222 | 223 | // alternative way -> iterate over the result set and dump each individual row to the screen 224 | /* for ($i = 0; $i < $result->size(); $i++) 225 | { 226 | // wrap each row in curly braces 227 | echo("{\n"); 228 | 229 | // fetch the row and dump it to screen 230 | $result->fetchRow($i); 231 | 232 | // close the row 233 | echo("\n}\n"); 234 | } 235 | */ 236 | return false; 237 | }); 238 | 239 | // run the event loop 240 | $loop->run(); 241 | 242 | ?> 243 | 244 | ``` 245 | 246 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | REACT-PHP-CPP 2 | ============= 3 | 4 | Event loop library for PHP implemented in C++. Support for asynchronous non-blocking sockets, DNS lookups and database connections. 5 | 6 | REACT-PHP-CPP utilizes the features of the PHP-CPP library and is a wrapper around the REACT-CPP and REACT-CPP-MYSQL libraries. Thus, it depends on the aforementioned libraries. 7 | 8 | 9 | Event Loop 10 | ========== 11 | 12 | The ReactPhp::Loop class is wrapped around the React::Loop class of the REACT-CPP library and runs the main PHP event loop. It contains methods to set timers, while it also utilizes callback functions which will be called whenever a filedescriptor becomes readable or writable. 13 | 14 | Below is a typical application, where an instance of the event loop is created, which can then be used to register timers and filedescriptors that will be checked for readability: 15 | 16 | ```php 17 | 18 | onTimeout(1.0, function() { 27 | 28 | // report that the timer has expired 29 | echo("timer 1 expires\n"); 30 | 31 | return false; 32 | 33 | }); 34 | 35 | // set a timeout -- alternative way: creating a timer object 36 | $timer2 = new Async\TimeoutWatcher($loop, 2.0, function() { 37 | 38 | // report that the second timer has expired 39 | echo("timer 2 expires\n"); 40 | 41 | return false; 42 | }); 43 | 44 | // notify us when input is available on stdin 45 | $reader1 = $loop->onReadable('STDIN_FILENO', function() { 46 | 47 | // report that input is available 48 | echo("Input is available at first reader\n"); 49 | 50 | return false; 51 | }); 52 | 53 | // notify us when input is available -- alternative way: creating a reader object 54 | $reader2 = new Async\ReadWatcher($loop, 'STDIN_FILENO', function() { 55 | 56 | // report that input is available on the second reader 57 | echo("Input is available at second reader\n"); 58 | 59 | return false; 60 | }); 61 | 62 | // display the current time 63 | echo("Current time: ".$loop->now()."\n"); 64 | 65 | // run the event loop 66 | $loop->run(); 67 | 68 | ?> 69 | 70 | ``` 71 | 72 | After the instance of the event loop is created, it should be run in order to register the timers and filedescriptors. 73 | 74 | 75 | Connection class 76 | ================ 77 | 78 | The ReactPhp::Connection class is wrapped around the React::MySQL::Connection class of the REACT-CPP-MYSQL library and it can be used to establish a connection to a MySQL daemon, as the following script illustrates: 79 | 80 | ```php 81 | 82 | run(); 99 | 100 | ?> 101 | 102 | ``` 103 | 104 | Again, an instance of the event loop needs to be created, so that the connection class can be used, whilst the callback function notifies us when the connection is established. 105 | 106 | 107 | Statements 108 | ========== 109 | 110 | Naturally, the next step after establishing a connection to a MySQL daemon is to use statements in order to define or manipulate a certain table in a database. 111 | 112 | To achieve this we can use the ReactPhp::Statement class, which is also wrapped around the React::MySQL::Statement class of the REACT-CPP-MYSQL library. First, a ReactPhp::Statement object needs to be created in the following fashion: 113 | 114 | ```php 115 | 116 | // initialize a statement 117 | $statement = new Async\Statement($connection, "INSERT INTO Persons (FirstName, LastName, Age) VALUES ('A', 'B', 10), ('C', 'D', 20), ('E', 'F', 30)", function() { 118 | 119 | echo("Statement initialized\n"); 120 | 121 | return false; 122 | }); 123 | 124 | ``` 125 | 126 | From the above script it is obvious that the connection object is essential in order to create the statement object, since it constitutes the first parameter of the constructor. The second parameter is the actual MySQL statement and the third is the callback function, which notifies us that the statement can be executed. 127 | For the execution to take place, we need to call the ReactPhp::Statement::execute() function. However, when the statement is a SELECT statement, the ReactPhp::Statement::executeQuery() function needs to be called. This happens, because in the case of a query a result object needs to be passed as a parameter in the callback function, so that the result set can be dumped to the screen: 128 | 129 | ```php 130 | 131 | execute(function() { 155 | 156 | echo("Statement was executed\n"); 157 | 158 | return false; 159 | 160 | }); 161 | 162 | // execute a query statement -- should only be used when we have a SELECT statement 163 | $statement->executeQuery(function($result) { 164 | 165 | echo("Executing a SELECT statement\n"); 166 | 167 | // iterate over the result set 168 | foreach ($result as $row) 169 | { 170 | // wrap the row in curly braces 171 | echo("{ "); 172 | 173 | // iterate over each individual row of the result set 174 | foreach ($row as $field) 175 | { 176 | // and dump all the result fields to the screen 177 | echo("$field "); 178 | } 179 | 180 | // close the row 181 | echo("}\n"); 182 | } 183 | 184 | return false; 185 | }); 186 | 187 | // run the event loop 188 | $loop->run(); 189 | 190 | ?> 191 | 192 | ``` 193 | 194 | That, practically, means that all MySQL statements are executed using the ReactPhp::Statement::execute() function, except the SELECT statement, which demands the ReactPhp::Statement::executeQuery() function to execute. 195 | This is the reason why there is a separate function, which can be used for executing queries only, namely the ReactPhp::Connection::query() function. As a result, whenever a query has to be executed, the best approach is to use the aforementioned function and not the ReactPhp::Statement::executeQuery() function, since the former is more straightforward: 196 | 197 | ```php 198 | 199 | query("SELECT * FROM Persons", function($result) { 215 | 216 | // iterate over the result set 217 | foreach ($result as $row) 218 | { 219 | // wrap each row in curly braces 220 | echo("{ "); 221 | 222 | // iterate over each individual row of the result set 223 | foreach ($row as $field) 224 | { 225 | // and dump all the result fields to the screen 226 | echo("$field "); 227 | } 228 | 229 | // close the row 230 | echo("}\n"); 231 | } 232 | 233 | // alternative way -> iterate over the result set and dump each individual row to the screen 234 | /* for ($i = 0; $i < $result->size(); $i++) 235 | { 236 | // wrap each row in curly braces 237 | echo("{\n"); 238 | 239 | // fetch the row and dump it to screen 240 | $result->fetchRow($i); 241 | 242 | // close the row 243 | echo("\n}\n"); 244 | } 245 | */ 246 | return false; 247 | }); 248 | 249 | // run the event loop 250 | $loop->run(); 251 | 252 | ?> 253 | 254 | ``` 255 | 256 | At this point, we should state that there are two ways to iterate over the result set produced by the query. 257 | The first (used in the above script) uses the Php::Traversable class of the PHP-CPP library, which enables classes to be used in foreach loops, just like regular arrays. 258 | The second way (in comments at the above script) iterates over each valid result and dumps each individual row to the screen, as long as the size of the result set is not exceeded. 259 | The difference between these two ways lies in the fact that when using the foreach loop the results of the query are being printed in the PHP user space, whereas when using the for loop they are put to the screen through C++. As a consequence, we can say that the former way is a more 'valid' way to output the query results. 260 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | 203 | --------------------------------------------------------------------------------