From 009580dcccaebed3ddec07e0001b302221c5f97f Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sun, 20 Oct 2024 12:06:04 +0200 Subject: [PATCH 1/3] fixed the Php::include_once() call for PHP 8.1 --- Examples/IncludeOnce/30-includeonce.ini | 4 ++ Examples/IncludeOnce/Makefile | 34 +++++++++++++++++ Examples/IncludeOnce/class.php | 21 +++++++++++ Examples/IncludeOnce/includeonce.cpp | 50 +++++++++++++++++++++++++ Examples/IncludeOnce/includeonce.php | 18 +++++++++ zend/file.cpp | 34 +++++++++-------- 6 files changed, 146 insertions(+), 15 deletions(-) create mode 100644 Examples/IncludeOnce/30-includeonce.ini create mode 100644 Examples/IncludeOnce/Makefile create mode 100644 Examples/IncludeOnce/class.php create mode 100644 Examples/IncludeOnce/includeonce.cpp create mode 100644 Examples/IncludeOnce/includeonce.php diff --git a/Examples/IncludeOnce/30-includeonce.ini b/Examples/IncludeOnce/30-includeonce.ini new file mode 100644 index 00000000..6cfdbd38 --- /dev/null +++ b/Examples/IncludeOnce/30-includeonce.ini @@ -0,0 +1,4 @@ +; configuration for phpcpp module +; priority=30 +extension=includeonce.so + diff --git a/Examples/IncludeOnce/Makefile b/Examples/IncludeOnce/Makefile new file mode 100644 index 00000000..e42b8496 --- /dev/null +++ b/Examples/IncludeOnce/Makefile @@ -0,0 +1,34 @@ +CPP = g++ +RM = rm -f +CPP_FLAGS = -Wall -c -I. -O2 -std=c++11 +PHP_CONFIG = $(shell which php-config) +LIBRARY_DIR = $(shell ${PHP_CONFIG} --extension-dir) +PHP_CONFIG_DIR = $(shell ${PHP_CONFIG} --ini-dir) + +LD = g++ +LD_FLAGS = -Wall -shared -O2 +RESULT = includeonce.so + +PHPINIFILE = 30-includeonce.ini + +SOURCES = $(wildcard *.cpp) +OBJECTS = $(SOURCES:%.cpp=%.o) + +all: ${OBJECTS} ${RESULT} + +${RESULT}: ${OBJECTS} + ${LD} ${LD_FLAGS} -o $@ ${OBJECTS} -lphpcpp + +clean: + ${RM} *.obj *~* ${OBJECTS} ${RESULT} + +${OBJECTS}: + ${CPP} ${CPP_FLAGS} -fpic -o $@ ${@:%.o=%.cpp} + +install: + cp -f ${RESULT} ${LIBRARY_DIR}/ + cp -f ${PHPINIFILE} ${PHP_CONFIG_DIR}/ + +uninstall: + rm ${LIBRARY_DIR}/${RESULT} + rm ${PHP_CONFIG_DIR}/${PHPINIFILE} diff --git a/Examples/IncludeOnce/class.php b/Examples/IncludeOnce/class.php new file mode 100644 index 00000000..81a68325 --- /dev/null +++ b/Examples/IncludeOnce/class.php @@ -0,0 +1,21 @@ + + * @copyright 2024 Copernica BV + */ + +/** + * Include ourselves (should do nothing) + */ +include_once(__FILE__); +my_include_once(__FILE__); + +/** + * Class definition + */ +class SillyClass {} diff --git a/Examples/IncludeOnce/includeonce.cpp b/Examples/IncludeOnce/includeonce.cpp new file mode 100644 index 00000000..243abedf --- /dev/null +++ b/Examples/IncludeOnce/includeonce.cpp @@ -0,0 +1,50 @@ +/** + * callphpfunction.cpp + * @author Jasper van Eck + * + * An example file to show the working of a php function call in C++. + */ + +/** + * Libraries used. + */ +#include +#include + +/** + * Namespace to use + */ +using namespace std; + +/** + * Function to test the Php::include_once() call + * @param ¶ms + */ +void my_include_once(Php::Parameters ¶ms) +{ + // the string + Php::Value path = params[0]; + + // do the call + Php::include_once(path.stringValue()); +} + + +// Symbols are exported according to the "C" language +extern "C" +{ + // export the "get_module" function that will be called by the Zend engine + PHPCPP_EXPORT void *get_module() + { + // create extension + static Php::Extension extension("include_once","1.0"); + + // add function to extension + extension.add("my_include_once", { + Php::ByVal("path", Php::Type::String), + }); + + // return the extension module + return extension.module(); + } +} diff --git a/Examples/IncludeOnce/includeonce.php b/Examples/IncludeOnce/includeonce.php new file mode 100644 index 00000000..4478f745 --- /dev/null +++ b/Examples/IncludeOnce/includeonce.php @@ -0,0 +1,18 @@ +valid(); // we are going to open the file - zend_file_handle fileHandle; + zend_file_handle filehandle; #if PHP_VERSION_ID < 80100 + // open the file if (zend_stream_open(ZSTR_VAL(_path), &fileHandle) == FAILURE) return false; + #else - /** - * zend_stream_open now only accepts the fileHandle object - * Filename must now be set through the object path. - */ - fileHandle.filename = _path; - // open the file - if (zend_stream_open(&fileHandle) == FAILURE) return false; + // since php 8 (or 8.1? - this has not been checked), zend_stream_open just takes the file-handle, and we must associate it first with a filename + zend_stream_init_filename_ex(&filehandle, _path); + + // the stream if supposed to be open by now + if (zend_stream_open(&filehandle) == FAILURE) return false; + #endif - // make sure the path name is stored in the handle (@todo: is this necessary? do we need the copy?) - if (!fileHandle.opened_path) fileHandle.opened_path = zend_string_copy(_path); + // make sure the path name is stored in the handle (@todo: is this necessary? do we need the copy, + // this was copied from zend_execute.c, maybe deals with case when opened_path is not set for + // special types of files that are correctly opened, but that do not expose path-info, while this info + // is still needed by the subsequent zend_compile_file() call for error messages?) + if (!filehandle.opened_path) filehandle.opened_path = zend_string_copy(_path); // we need temporary compiler options CompilerOptions options(ZEND_COMPILE_DEFAULT); // create the opcodes - _opcodes.reset(new Opcodes(zend_compile_file(&fileHandle, ZEND_INCLUDE))); + _opcodes.reset(new Opcodes(zend_compile_file(&filehandle, ZEND_INCLUDE))); // close the file handle - zend_destroy_file_handle(&fileHandle); + zend_destroy_file_handle(&filehandle); // done return _opcodes->valid(); @@ -133,9 +137,6 @@ Value File::execute() // try compiling the file if (!compile()) return nullptr; - // add the entry to the list of included files - zend_hash_add_empty_element(&EG(included_files), _path); - // execute the opcodes return _opcodes->execute(); } @@ -152,6 +153,9 @@ Value File::once() // check if this file was already included if (zend_hash_exists(&EG(included_files), _path)) return nullptr; + // add the entry to the list of included files + zend_hash_add_empty_element(&EG(included_files), _path); + // execute the file return execute(); } From 52895f528bb5a4320cf3a78325028ba924817886 Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Sun, 20 Oct 2024 12:08:19 +0200 Subject: [PATCH 2/3] new version number --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index d50e5de9..4845793e 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ INSTALL_LIB = ${INSTALL_PREFIX}/lib # SONAME = 2.4 -VERSION = 2.4.5 +VERSION = 2.4.6 # From 910f7b5f91bbf626044511fa58c0f8b0789823db Mon Sep 17 00:00:00 2001 From: Emiel Bruijntjes Date: Mon, 21 Oct 2024 11:26:54 +0200 Subject: [PATCH 3/3] fixed compile error on older php versions (lower than 8) --- Makefile | 2 +- zend/file.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index 4845793e..2b9b4809 100644 --- a/Makefile +++ b/Makefile @@ -50,7 +50,7 @@ INSTALL_LIB = ${INSTALL_PREFIX}/lib # SONAME = 2.4 -VERSION = 2.4.6 +VERSION = 2.4.7 # diff --git a/zend/file.cpp b/zend/file.cpp index 35e086e0..3d7d181a 100644 --- a/zend/file.cpp +++ b/zend/file.cpp @@ -67,7 +67,7 @@ bool File::compile() #if PHP_VERSION_ID < 80100 // open the file - if (zend_stream_open(ZSTR_VAL(_path), &fileHandle) == FAILURE) return false; + if (zend_stream_open(ZSTR_VAL(_path), &filehandle) == FAILURE) return false; #else