From 08ed8866a5bba0b23a8d5587116a968512df2568 Mon Sep 17 00:00:00 2001 From: valmat Date: Tue, 8 Apr 2014 08:53:35 +0600 Subject: Ini entries emplemented (issues #64) --- include/extension.h | 2 +- include/ini.h | 153 ++++++++++++++++++++++++++++++++++++ include/namespace.h | 41 ++++++++++ phpcpp.h | 1 + src/extension.cpp | 27 ++++++- src/includes.h | 2 + src/ini.cpp | 57 ++++++++++++++ tests/cpp/h/ini_entries.h | 16 ++++ tests/cpp/include/ini_entries/001.h | 29 +++++++ tests/cpp/main.cpp | 28 +++++++ tests/php/dbg.php | 54 ++++++++++--- tests/php/php_alias.sh | 2 +- 12 files changed, 396 insertions(+), 16 deletions(-) create mode 100644 include/ini.h create mode 100644 src/ini.cpp create mode 100644 tests/cpp/h/ini_entries.h create mode 100644 tests/cpp/include/ini_entries/001.h diff --git a/include/extension.h b/include/extension.h index f9361b3..6d1fa0c 100644 --- a/include/extension.h +++ b/include/extension.h @@ -139,7 +139,7 @@ public: { return module(); } - + private: /** * The information that is passed to the Zend engine diff --git a/include/ini.h b/include/ini.h new file mode 100644 index 0000000..b881cfe --- /dev/null +++ b/include/ini.h @@ -0,0 +1,153 @@ +/** + * Ini.h + * + * + * + * + * @copyright 2013 Copernica BV + */ + +/** + * Forward declaration + */ + +struct _zend_ini_entry; + +/** + * Set up namespace + */ +namespace Php { + + + /** + * Class definition + */ + class Ini + { + public: + + /** + * Supported place-types for ini setting + * The possible settings for where the configuration can be changed are: + * PHP_INI_USER, PHP_INI_PERDIR, PHP_INI_SYSTEM and PHP_INI_ALL + * Usually you would choose where the setting can be changed based on how it is used. For example if you want to access + * the setting during RINIT stage then you would want PHP_INI_PERDIR because the setting would have no use after RINIT. + */ + enum class Place : int { + User = (1<<0), // ZEND_INI_USER (1<<0) + Perdir = (1<<1), // ZEND_INI_PERDIR (1<<1) + System = (1<<2), // ZEND_INI_SYSTEM (1<<2) + All = (1<<0) | (1<<1) | (1<<2) // ZEND_INI_ALL (ZEND_INI_USER|ZEND_INI_PERDIR|ZEND_INI_SYSTEM) + }; + + + /** + * default constructors + */ + Ini(const char *name, const char *value, const char *orig_value, const Place place = Place::All) : + _name(name), _value(value), _orig_value(orig_value), _place(place) {} + + Ini(const char *name, const char *value, const Place place = Place::All) : + _name(name), _value(value), _orig_empty(true), _place(place) {} + + /** + * Constructors for bool value + */ + Ini(const char *name,const bool value, const bool orig_value, const Place place = Place::All) : + _name(name), _value(bool2str(value)), _orig_value(bool2str(orig_value)), _place(place) {} + + Ini(const char *name, const bool value, const Place place = Place::All) : + _name(name), _value(bool2str(value)), _orig_empty(true), _place(place) {} + + /** + * Constructors for integer value + * @param value + */ + Ini(const char *name, const int16_t value, const int16_t orig_value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_value(std::to_string(orig_value)), _place(place) {} + + Ini(const char *name, const int16_t value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {} + + + Ini(const char *name, const int32_t value, const int32_t orig_value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_value(std::to_string(orig_value)), _place(place) {} + + Ini(const char *name, const int32_t value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {} + + + Ini(const char *name, const int64_t value, const int64_t orig_value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_value(std::to_string(orig_value)), _place(place) {} + + Ini(const char *name, const int64_t value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {} + + /** + * Constructors for float value + * @param value + */ + Ini(const char *name, const double value, const double orig_value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_value(std::to_string(orig_value)), _place(place) {} + + Ini(const char *name, const double value, const Place place = Place::All) : + _name(name), _value(std::to_string(value)), _orig_empty(true), _place(place) {} + + + /** + * Copy constructor + * @param Ini + */ + Ini(const Ini &that) : + _name(that._name), _value(that._value), _orig_value(that._orig_value), _orig_empty(that._orig_empty), _place(that._place) + {} + + /** + * Move constructor + * @param Ini + */ + Ini(Ini &&that) : + _name(that._name), _value(std::move(that._value)), _orig_value(std::move(that._orig_value)), _orig_empty(that._orig_empty), _place(that._place) + {} + + + /** + * Filling ini_entries + * @param zend_ini_entry *ini_entry, int module_number + * @param int module_number + */ + void fill(_zend_ini_entry *ini_entry, int module_number); + + + + + private: + + static constexpr const char* bool2str(const bool value) + { + return ( static_cast(value) ? "On" : "Off"); + } + + // ini entry name + const char* _name; + + // ini entry value + std::string _value; + + // ini entry original value + std::string _orig_value; + + // _orig_value is empty + bool _orig_empty = false; + + // plase where the configuration can be changed + Place _place; + }; + + + +/** + * End of namespace + */ +} + diff --git a/include/namespace.h b/include/namespace.h index de73924..5830339 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -167,6 +167,41 @@ public: return *this; } + + /** + * Add a ini entry to the extension by moving it + * @param ini The class implementation + * @return Namespace Same object to allow chaining + */ + Namespace &add(Ini &&ini) + { + // make a copy of the object + auto *copy = new Ini(std::move(ini)); + + // and add it to the list of classes + _ini_entries.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; + } + + /** + * Add a ini entry to the extension by copying it + * @param ini The class implementation + * @param Namespace Same object to allow chaining + */ + Namespace &add(const Ini &ini) + { + // make a copy of the object + auto *copy = new Ini(std::move(ini)); + + // and add it to the list of classes + _ini_entries.push_back(std::unique_ptr(copy)); + + // allow chaining + return *this; + } + protected: /** @@ -192,6 +227,12 @@ protected: * @var list */ std::list> _namespaces; + + /** + * Ini entry defined by the extension + * @var list + */ + std::list> _ini_entries; /** * The total number of functions diff --git a/phpcpp.h b/phpcpp.h index a07d1ac..4e2ea76 100644 --- a/phpcpp.h +++ b/phpcpp.h @@ -51,6 +51,7 @@ /** * Include all headers files that are related to this library */ +#include #include #include #include diff --git a/src/extension.cpp b/src/extension.cpp index 949efa9..2782f3f 100644 --- a/src/extension.cpp +++ b/src/extension.cpp @@ -110,10 +110,25 @@ int Extension::onStartup(int type, int module_number TSRMLS_DC) { // initialize and allocate the "global" variables ZEND_INIT_MODULE_GLOBALS(phpcpp, init_globals, NULL); - + + // get the extension Extension *extension = find(module_number TSRMLS_CC); + + // array contains ini settings + static zend_ini_entry *ini_entries = new zend_ini_entry[ extension->_ini_entries.size()+1 ]; + + // Filling ini_entries + unsigned int Ind = 0; + for (auto &ini : extension->_ini_entries) ini->fill(&ini_entries[Ind++], module_number); + + // add last empty ini entry (Zend, for some reason, it requires) + zend_ini_entry empty_entry { 0, 0, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0, nullptr, 0, 0, 0, nullptr }; + ini_entries[Ind] = empty_entry; + // register + REGISTER_INI_ENTRIES(); + // initialize namespace extension->initialize("" TSRMLS_CC); @@ -135,7 +150,13 @@ int Extension::onShutdown(int type, int module_number TSRMLS_DC) { // get the extension Extension *extension = find(module_number TSRMLS_CC); - + + + UNREGISTER_INI_ENTRIES(); + // free memory from array ini entries + static zend_ini_entry *ini_entries; + delete [] ini_entries; + // is the callback registered? if (extension->_onShutdown) extension->_onShutdown(); @@ -204,7 +225,7 @@ Extension::Extension(const char *name, const char *version) : Namespace("") _entry->zend_api = ZEND_MODULE_API_NO; // api number _entry->zend_debug = ZEND_DEBUG; // debug mode enabled? _entry->zts = USING_ZTS; // is thread safety enabled? - _entry->ini_entry = NULL; // the php.ini record + _entry->ini_entry = NULL; // the php.ini record, will be filled by Zend engine _entry->deps = NULL; // dependencies on other modules _entry->name = name; // extension name _entry->functions = NULL; // functions supported by this module (none for now) diff --git a/src/includes.h b/src/includes.h index 10dd4b8..45cbd2d 100644 --- a/src/includes.h +++ b/src/includes.h @@ -34,6 +34,7 @@ #include #include #include +#include /** * Macro to convert results to success status @@ -43,6 +44,7 @@ /** * Include other files from this library */ +#include "../include/ini.h" #include "../include/exception.h" #include "../include/streams.h" #include "../include/type.h" diff --git a/src/ini.cpp b/src/ini.cpp new file mode 100644 index 0000000..e6a9888 --- /dev/null +++ b/src/ini.cpp @@ -0,0 +1,57 @@ +/** + * Ini.cpp + * + * Implementation for .... + * + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + + /** + * Filling ini_entries + * @param zend_ini_entry *ini_entry, int module_number + * @param int module_number + */ + void Ini::fill(zend_ini_entry *ini_entry, int module_number) + { + ini_entry->module_number = module_number; + ini_entry->modifiable = static_cast(this->_place); + ini_entry->name = const_cast(this->_name); + ini_entry->name_length = strlen(this->_name)+1; + ini_entry->on_modify = OnUpdateString; + ini_entry->mh_arg1 = nullptr; + #ifdef ZTS + ini_entry->mh_arg2 = (void *) &phpcpp_globals_id; + #else + ini_entry->mh_arg2 = (void *) &phpcpp_globals; + #endif + ini_entry->mh_arg3 = nullptr; + ini_entry->value = const_cast(this->_value.c_str()); + ini_entry->value_length = this->_value.size(); + if( this->_orig_empty) + { + ini_entry->orig_value = nullptr; + ini_entry->orig_value_length = 0; + } + else + { + ini_entry->orig_value = const_cast(this->_orig_value.c_str()); + ini_entry->orig_value_length = this->_orig_value.size(); + } + ini_entry->orig_modifiable = 0; + ini_entry->modified = 0; + ini_entry->displayer = nullptr; + } + + +/** + * End of namespace + */ +} + + diff --git a/tests/cpp/h/ini_entries.h b/tests/cpp/h/ini_entries.h new file mode 100644 index 0000000..a6fb10e --- /dev/null +++ b/tests/cpp/h/ini_entries.h @@ -0,0 +1,16 @@ +/** + * + * Test ini entries + * + */ + + +#include "../include/ini_entries/001.h" +//#include "../include/ini_entries/.h" +//#include "../include/ini_entries/.h" + + + + + + diff --git a/tests/cpp/include/ini_entries/001.h b/tests/cpp/include/ini_entries/001.h new file mode 100644 index 0000000..649b0bd --- /dev/null +++ b/tests/cpp/include/ini_entries/001.h @@ -0,0 +1,29 @@ +/** + * + * Test ini entries + * test ini_entries/001.phpt + * + */ + +/** + * Set up namespace + */ +namespace TestIniEntries { + + + void iniTest1(Php::Parameters ¶ms) + { + Php::out << "extension_for_tests.some_string ={{" << Php::ini_get("extension_for_tests.some_string") << "}}" << std::endl; + Php::out << "extension_for_tests.some_string2 ={{" << Php::ini_get("extension_for_tests.some_string2") << "}}" << std::endl; + Php::out << "extension_for_tests.some_string3 ={{" << Php::ini_get("extension_for_tests.some_string3") << "}}" << std::endl; + Php::out << "extension_for_tests.some_string3 ={{" << Php::ini_get("extension_for_tests.some_bool") << "}}" << std::endl; + + + + return; + } +/** + * End of namespace + */ +} + diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 84d95d8..c563c8d 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -11,6 +11,7 @@ #include "h/ValueIterator.h" #include "h/Classes_and_objects.h" #include "h/variables.h" +#include "h/ini_entries.h" @@ -128,6 +129,33 @@ extern "C" extension.add(std::move(cObj2Scalar)); + extension + .add(Php::Ini("ini1", "valIni1")) + .add(Php::Ini("ini2", "valIni2", "OrigValIni2")) + .add(Php::Ini("ini3", "valIni3", "OrigValIni3", Php::Ini::Place::System)) + .add(Php::Ini("ini4", true, false, Php::Ini::Place::User)) + .add(Php::Ini("ini5", false)); + + Php::Ini ini6("ini6", 55, 11); + extension + .add(ini6) + .add(Php::Ini("ini7", 74)); + + Php::Ini ini8("ini8", 3.1415926, 6.2831852); + Php::Ini ini9("ini9", 2.7182818, 5.4365636, Php::Ini::Place::User); + extension.add(ini8); + extension.add(std::move(ini9)); + + + + + /** + * tests ini entries + * + */ + extension.add("TestIniEntries\\iniTest1", TestIniEntries::iniTest1); + + diff --git a/tests/php/dbg.php b/tests/php/dbg.php index 003c494..8de0bb8 100644 --- a/tests/php/dbg.php +++ b/tests/php/dbg.php @@ -13,14 +13,46 @@ -var_dump( TestBaseClass\MyClass::CONSTANT1 ); -var_dump( TestBaseClass\MyClass::EXP ); -var_dump( TestBaseClass\MyClass::CONSTANT2 ); -var_dump( TestBaseClass\MyClass::CONSTANT3 ); - -var_dump( TestBaseClass\MyClass::$StatProp1 ); -var_dump( TestBaseClass\MyClass::$Exp ); -var_dump( TestBaseClass\MyClass::$StatProp2 ); -var_dump( TestBaseClass\MyClass::$StatProp3 ); -TestBaseClass\MyClass::$StatProp2 = "otherval"; -var_dump( TestBaseClass\MyClass::$StatProp2 ); +echo "\x1b[1;31m"; +(new ReflectionExtension('extension_for_tests') )->info(); +echo "\x1b[0m"; +echo "\x1b[0;34m"; +var_export( ini_get_all ( 'extension_for_tests' ) ); +echo "\x1b[0m", PHP_EOL; +exit; + +TestIniEntries\iniTest1(); + +//ini_set("extension_for_tests.some_string3", 'RFVBGT') ; +echo PHP_EOL; +echo "extension_for_tests.some_string ={{" , ini_get("extension_for_tests.some_string") , "}}", PHP_EOL; +echo "extension_for_tests.some_string2 ={{" , ini_get("extension_for_tests.some_string2") , "}}", PHP_EOL; +echo "extension_for_tests.some_string3 ={{" , ini_get("extension_for_tests.some_string3") , "}}", PHP_EOL; +echo "extension_for_tests.some_string3 ={{" , ini_get("extension_for_tests.some_bool") , "}}", PHP_EOL; + + +echo "\x1b[1;31m"; +(new ReflectionExtension('extension_for_tests') )->info(); +echo "\x1b[0m"; + +echo "\x1b[0;34m"; +var_export( ini_get_all ( 'extension_for_tests' ) ); +echo "\x1b[0m", PHP_EOL; + +ini_set("extension_for_tests.some_string3", 'RFVBGT') ; +echo "extension_for_tests.some_string3 ={{" , ini_get("extension_for_tests.some_string3") , "}}", PHP_EOL; +ini_restore ( 'extension_for_tests.some_string3' ); +echo "extension_for_tests.some_string3 ={{" , ini_get("extension_for_tests.some_string3") , "}}", PHP_EOL; + + +echo "extension_for_tests.some_bool :"; +var_dump(ini_get("extension_for_tests.some_bool")); +echo PHP_EOL; + +ini_set("extension_for_tests.some_bool", 'off'); + +echo "extension_for_tests.some_bool :"; +var_dump(ini_get("extension_for_tests.some_bool")); +echo PHP_EOL; + +//var_export(php_sapi_name()); diff --git a/tests/php/php_alias.sh b/tests/php/php_alias.sh index f4c7f8f..6947325 100755 --- a/tests/php/php_alias.sh +++ b/tests/php/php_alias.sh @@ -16,4 +16,4 @@ fi LD_LIBRARY_PATH="$(cd $PWD/../.. && echo $PWD):${LD_LIBRARY_PATH}" export LD_LIBRARY_PATH -/usr/bin/php -d extension_dir=../ext_dir -d extension=extfortest.so $1 +/usr/bin/php -d extension_dir=../ext_dir -d extension=extfortest.so -d ini7=47 -d extension_for_tests.frcli=frcli $1 $2 $3 $4 $5 -- cgit v1.2.3 From 21ba3f0aae94206457327552666d75dd2cf0a8f2 Mon Sep 17 00:00:00 2001 From: valmat Date: Wed, 9 Apr 2014 12:00:33 +0600 Subject: Made compatible. Now works in the new structure of the library. --- include/namespace.h | 17 +++++++++++++++ src/ini.cpp | 57 -------------------------------------------------- zend/extensionimpl.cpp | 13 ++++-------- zend/ini.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++ zend/namespace.cpp | 15 +++++++++++++ 5 files changed, 93 insertions(+), 66 deletions(-) delete mode 100644 src/ini.cpp create mode 100644 zend/ini.cpp diff --git a/include/namespace.h b/include/namespace.h index 25d4e5e..645eace 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -207,6 +207,15 @@ public: // done return result; } + + /** + * The total number of ini entries + * @return size_t + */ + size_t ini_size() + { + return _ini_entries.size(); + } /** * Apply a callback to each registered function @@ -227,6 +236,14 @@ public: * @param callback */ void apply(const std::function &callback); + + /** + * Filling ini entries into external zend_ini_entry array + * @param zend_ini_entry* + */ + void fill_ini(_zend_ini_entry *ini_entries, int module_number); + + }; diff --git a/src/ini.cpp b/src/ini.cpp deleted file mode 100644 index e6a9888..0000000 --- a/src/ini.cpp +++ /dev/null @@ -1,57 +0,0 @@ -/** - * Ini.cpp - * - * Implementation for .... - * - * @copyright 2013 Copernica BV - */ -#include "includes.h" - -/** - * Set up namespace - */ -namespace Php { - - /** - * Filling ini_entries - * @param zend_ini_entry *ini_entry, int module_number - * @param int module_number - */ - void Ini::fill(zend_ini_entry *ini_entry, int module_number) - { - ini_entry->module_number = module_number; - ini_entry->modifiable = static_cast(this->_place); - ini_entry->name = const_cast(this->_name); - ini_entry->name_length = strlen(this->_name)+1; - ini_entry->on_modify = OnUpdateString; - ini_entry->mh_arg1 = nullptr; - #ifdef ZTS - ini_entry->mh_arg2 = (void *) &phpcpp_globals_id; - #else - ini_entry->mh_arg2 = (void *) &phpcpp_globals; - #endif - ini_entry->mh_arg3 = nullptr; - ini_entry->value = const_cast(this->_value.c_str()); - ini_entry->value_length = this->_value.size(); - if( this->_orig_empty) - { - ini_entry->orig_value = nullptr; - ini_entry->orig_value_length = 0; - } - else - { - ini_entry->orig_value = const_cast(this->_orig_value.c_str()); - ini_entry->orig_value_length = this->_orig_value.size(); - } - ini_entry->orig_modifiable = 0; - ini_entry->modified = 0; - ini_entry->displayer = nullptr; - } - - -/** - * End of namespace - */ -} - - diff --git a/zend/extensionimpl.cpp b/zend/extensionimpl.cpp index 3534cdb..545b590 100644 --- a/zend/extensionimpl.cpp +++ b/zend/extensionimpl.cpp @@ -116,17 +116,12 @@ int ExtensionImpl::processStartup(int type, int module_number TSRMLS_DC) auto *extension = find(module_number TSRMLS_CC); // array contains ini settings - static zend_ini_entry *ini_entries = new zend_ini_entry[ extension->_ini_entries.size()+1 ]; + static zend_ini_entry *ini_entries = new zend_ini_entry[ extension->_data->ini_size()+1 ]; - // Filling ini_entries - unsigned int Ind = 0; - for (auto &ini : extension->_ini_entries) ini->fill(&ini_entries[Ind++], module_number); - - // add last empty ini entry (Zend, for some reason, it requires) - zend_ini_entry empty_entry { 0, 0, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0, nullptr, 0, 0, 0, nullptr }; - ini_entries[Ind] = empty_entry; + // Filling ini entries + extension->_data->fill_ini(ini_entries, module_number); - // register + // register ini entries in Zend core REGISTER_INI_ENTRIES(); // initialize the extension diff --git a/zend/ini.cpp b/zend/ini.cpp new file mode 100644 index 0000000..e6a9888 --- /dev/null +++ b/zend/ini.cpp @@ -0,0 +1,57 @@ +/** + * Ini.cpp + * + * Implementation for .... + * + * @copyright 2013 Copernica BV + */ +#include "includes.h" + +/** + * Set up namespace + */ +namespace Php { + + /** + * Filling ini_entries + * @param zend_ini_entry *ini_entry, int module_number + * @param int module_number + */ + void Ini::fill(zend_ini_entry *ini_entry, int module_number) + { + ini_entry->module_number = module_number; + ini_entry->modifiable = static_cast(this->_place); + ini_entry->name = const_cast(this->_name); + ini_entry->name_length = strlen(this->_name)+1; + ini_entry->on_modify = OnUpdateString; + ini_entry->mh_arg1 = nullptr; + #ifdef ZTS + ini_entry->mh_arg2 = (void *) &phpcpp_globals_id; + #else + ini_entry->mh_arg2 = (void *) &phpcpp_globals; + #endif + ini_entry->mh_arg3 = nullptr; + ini_entry->value = const_cast(this->_value.c_str()); + ini_entry->value_length = this->_value.size(); + if( this->_orig_empty) + { + ini_entry->orig_value = nullptr; + ini_entry->orig_value_length = 0; + } + else + { + ini_entry->orig_value = const_cast(this->_orig_value.c_str()); + ini_entry->orig_value_length = this->_orig_value.size(); + } + ini_entry->orig_modifiable = 0; + ini_entry->modified = 0; + ini_entry->displayer = nullptr; + } + + +/** + * End of namespace + */ +} + + diff --git a/zend/namespace.cpp b/zend/namespace.cpp index bea31a1..e9ec631 100644 --- a/zend/namespace.cpp +++ b/zend/namespace.cpp @@ -127,6 +127,21 @@ void Namespace::apply(const std::functionfill(&ini_entries[Ind++], module_number); + + // add last empty ini entry (Zend, for some reason, it requires) + zend_ini_entry empty_entry { 0, 0, nullptr, 0, nullptr, nullptr, nullptr, nullptr, nullptr, 0, nullptr, 0, 0, 0, nullptr }; + ini_entries[Ind] = empty_entry; +} + /** * End namespace */ -- cgit v1.2.3 From 1a6b709b8f732ea8a91b07a74f64928e0484b1f6 Mon Sep 17 00:00:00 2001 From: valmat Date: Thu, 10 Apr 2014 11:09:22 +0600 Subject: Php::Ini::Place::System --> Php::Ini::System Proposed here: https://github.com/CopernicaMarketingSoftware/PHP-CPP/issues/64#issuecomment-39838981 --- include/ini.h | 3 +-- tests/cpp/main.cpp | 17 +++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/ini.h b/include/ini.h index b881cfe..2223bee 100644 --- a/include/ini.h +++ b/include/ini.h @@ -33,14 +33,13 @@ namespace Php { * Usually you would choose where the setting can be changed based on how it is used. For example if you want to access * the setting during RINIT stage then you would want PHP_INI_PERDIR because the setting would have no use after RINIT. */ - enum class Place : int { + enum Place : int { User = (1<<0), // ZEND_INI_USER (1<<0) Perdir = (1<<1), // ZEND_INI_PERDIR (1<<1) System = (1<<2), // ZEND_INI_SYSTEM (1<<2) All = (1<<0) | (1<<1) | (1<<2) // ZEND_INI_ALL (ZEND_INI_USER|ZEND_INI_PERDIR|ZEND_INI_SYSTEM) }; - /** * default constructors */ diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index c563c8d..334398a 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -129,30 +129,27 @@ extern "C" extension.add(std::move(cObj2Scalar)); + /** + * tests ini entries + * + */ extension .add(Php::Ini("ini1", "valIni1")) .add(Php::Ini("ini2", "valIni2", "OrigValIni2")) - .add(Php::Ini("ini3", "valIni3", "OrigValIni3", Php::Ini::Place::System)) + .add(Php::Ini("ini3", "valIni3", "OrigValIni3", Php::Ini::System)) .add(Php::Ini("ini4", true, false, Php::Ini::Place::User)) .add(Php::Ini("ini5", false)); Php::Ini ini6("ini6", 55, 11); extension .add(ini6) - .add(Php::Ini("ini7", 74)); + .add(Php::Ini("ini7", 74,5)); Php::Ini ini8("ini8", 3.1415926, 6.2831852); - Php::Ini ini9("ini9", 2.7182818, 5.4365636, Php::Ini::Place::User); + Php::Ini ini9("ini9", 2.7182818, 5.4365636, Php::Ini::User); extension.add(ini8); extension.add(std::move(ini9)); - - - - /** - * tests ini entries - * - */ extension.add("TestIniEntries\\iniTest1", TestIniEntries::iniTest1); -- cgit v1.2.3 From 14b63ee87dde3688fea7e58fa25b73362117ae11 Mon Sep 17 00:00:00 2001 From: valmat Date: Thu, 10 Apr 2014 13:04:34 +0600 Subject: Fixed problem with duplicate names ini entries. Mentioned https://github.com/CopernicaMarketingSoftware/PHP-CPP/issues/64#issuecomment-39838004 Now in the case of duplication name of ini entriy the new value overrides the old one. Before was incorrect handling of this situation. --- include/ini.h | 15 +++++++++++++-- include/namespace.h | 6 +++--- phpcpp.h | 1 + tests/cpp/main.cpp | 2 ++ zend/includes.h | 1 + zend/ini.cpp | 4 ++-- 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/ini.h b/include/ini.h index 2223bee..9d4cbd9 100644 --- a/include/ini.h +++ b/include/ini.h @@ -116,9 +116,20 @@ namespace Php { * @param int module_number */ void fill(_zend_ini_entry *ini_entry, int module_number); - + /** + * Compare by name + * A predicate that takes two arguments of type Ini. + * Used when adding elements of type Ini in the container std::set + */ + struct Compare + { + int operator()(const std::shared_ptr &s1, const std::shared_ptr &s2) const + { + return s1->_name.compare(s2->_name); + } + }; private: @@ -128,7 +139,7 @@ namespace Php { } // ini entry name - const char* _name; + std::string _name; // ini entry value std::string _value; diff --git a/include/namespace.h b/include/namespace.h index 645eace..468a423 100644 --- a/include/namespace.h +++ b/include/namespace.h @@ -52,7 +52,7 @@ protected: * Ini entry defined by the extension * @var list */ - std::list> _ini_entries; + std::set, Ini::Compare> _ini_entries; public: /** @@ -172,7 +172,7 @@ public: Namespace &add(Ini &&ini) { // and add it to the list of classes - _ini_entries.push_back(std::unique_ptr(new Ini(std::move(ini)))); + _ini_entries.emplace(new Ini(std::move(ini))); // allow chaining return *this; @@ -186,7 +186,7 @@ public: Namespace &add(const Ini &ini) { // and add it to the list of classes - _ini_entries.push_back(std::unique_ptr(new Ini(ini))); + _ini_entries.emplace(new Ini(ini)); // allow chaining return *this; diff --git a/phpcpp.h b/phpcpp.h index ce1beb5..4e9735d 100644 --- a/phpcpp.h +++ b/phpcpp.h @@ -21,6 +21,7 @@ #include #include #include +#include /** * Include all headers files that are related to this library diff --git a/tests/cpp/main.cpp b/tests/cpp/main.cpp index 334398a..2e0a1dc 100644 --- a/tests/cpp/main.cpp +++ b/tests/cpp/main.cpp @@ -147,6 +147,8 @@ extern "C" Php::Ini ini8("ini8", 3.1415926, 6.2831852); Php::Ini ini9("ini9", 2.7182818, 5.4365636, Php::Ini::User); + extension.add(Php::Ini("ini9", 0.333333, 0.777777, Php::Ini::Perdir)); + extension.add(ini8); extension.add(std::move(ini9)); diff --git a/zend/includes.h b/zend/includes.h index e7dece9..4aa5eb0 100644 --- a/zend/includes.h +++ b/zend/includes.h @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include diff --git a/zend/ini.cpp b/zend/ini.cpp index e6a9888..0c04d6a 100644 --- a/zend/ini.cpp +++ b/zend/ini.cpp @@ -21,8 +21,8 @@ namespace Php { { ini_entry->module_number = module_number; ini_entry->modifiable = static_cast(this->_place); - ini_entry->name = const_cast(this->_name); - ini_entry->name_length = strlen(this->_name)+1; + ini_entry->name = const_cast(this->_name.c_str()); + ini_entry->name_length = this->_name.size()+1; ini_entry->on_modify = OnUpdateString; ini_entry->mh_arg1 = nullptr; #ifdef ZTS -- cgit v1.2.3 From 3119060c9c8905b9d07e04a342a2c32cf0bf1358 Mon Sep 17 00:00:00 2001 From: valmat Date: Thu, 10 Apr 2014 13:16:32 +0600 Subject: fix warnings g++ -Wall -c -O2 -std=c++11 -fpic -I"/home/valmat/work/PHP-CPP/tests/include/lib" -I"/home/valmat/work/PHP-CPP/tests/include/zts" -o main.o main.cpp In file included from /home/valmat/work/PHP-CPP/tests/include/lib/phpcpp.h:60:0, from main.cpp:8: /home/valmat/work/PHP-CPP/tests/include/lib/phpcpp/call.h:70:28: warning: extra tokens at end of #pragma directive [enabled by default] #pragma push_macro("isset"); ^ /home/valmat/work/PHP-CPP/tests/include/lib/phpcpp/call.h:84:27: warning: extra tokens at end of #pragma directive [enabled by default] #pragma pop_macro("isset"); ^ --- include/call.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/call.h b/include/call.h index 9be5c91..2fcc9b0 100644 --- a/include/call.h +++ b/include/call.h @@ -67,7 +67,7 @@ inline void unset(const HashMember &member) { member.unset(); } /** * The isset function conflicts with the 'isset' macro defined by the Zend engine */ -#pragma push_macro("isset"); +#pragma push_macro("isset") #undef isset /** @@ -81,7 +81,7 @@ inline Value isset(const HashMember &member) { return member.exists() && /** * Re-install the ISSET macro */ -#pragma pop_macro("isset"); +#pragma pop_macro("isset") /** * End of namespace -- cgit v1.2.3