diff options
author | Joe George <joe@zeroc.com> | 2023-02-23 09:19:13 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-02-23 09:19:13 -0500 |
commit | 374a5485f89fa3db7782ce45b22a33f02e59b79a (patch) | |
tree | 1852c7b60340133d84e498328f9ac73e234704a0 | |
parent | JS: prefer use endsWith to lastIndexOf (diff) | |
download | ice-374a5485f89fa3db7782ce45b22a33f02e59b79a.tar.bz2 ice-374a5485f89fa3db7782ce45b22a33f02e59b79a.tar.xz ice-374a5485f89fa3db7782ce45b22a33f02e59b79a.zip |
Add php5 flag to slice2php (#1443)
-rw-r--r-- | CHANGELOG-3.7.md | 19 | ||||
-rw-r--r-- | cpp/src/slice2php/Main.cpp | 119 | ||||
-rw-r--r-- | man/man1/slice2php.1 | 5 | ||||
-rw-r--r-- | php/Makefile | 2 | ||||
-rw-r--r-- | php/config/Make.rules | 6 |
5 files changed, 65 insertions, 86 deletions
diff --git a/CHANGELOG-3.7.md b/CHANGELOG-3.7.md index 65ffdb75c3a..7e7ef669cd8 100644 --- a/CHANGELOG-3.7.md +++ b/CHANGELOG-3.7.md @@ -10,12 +10,13 @@ particular aspect of Ice. - [Changes in Ice 3.7.9](#changes-in-ice-379) - [C++ Changes](#c-changes) + - [PHP Changes](#php-changes) - [Changes in Ice 3.7.8](#changes-in-ice-378) - [C++ Changes](#c-changes-1) - [JavaScript Changes](#javascript-changes) - [MATLAB Changes](#matlab-changes) + - [PHP Changes](#php-changes-1) - [Python Changes](#python-changes) - - [PHP Changes](#php-changes) - [Changes in Ice 3.7.7](#changes-in-ice-377) - [C++ Changes](#c-changes-2) - [Java Changes](#java-changes) @@ -30,7 +31,7 @@ particular aspect of Ice. - [C++ Changes](#c-changes-4) - [C# Changes](#c-changes-5) - [JavaScript Changes](#javascript-changes-2) - - [PHP Changes](#php-changes-1) + - [PHP Changes](#php-changes-2) - [Python Changes](#python-changes-1) - [Ruby Changes](#ruby-changes) - [Swift Changes](#swift-changes-1) @@ -59,7 +60,7 @@ particular aspect of Ice. - [JavaScript Changes](#javascript-changes-5) - [MATLAB Changes](#matlab-changes-3) - [Objective-C Changes](#objective-c-changes) - - [PHP Changes](#php-changes-2) + - [PHP Changes](#php-changes-3) - [Python Changes](#python-changes-4) - [Changes in Ice 3.7.1](#changes-in-ice-371) - [General Changes](#general-changes-5) @@ -69,7 +70,7 @@ particular aspect of Ice. - [JavaScript Changes](#javascript-changes-6) - [MATLAB Changes](#matlab-changes-4) - [Objective-C Changes](#objective-c-changes-1) - - [PHP Changes](#php-changes-3) + - [PHP Changes](#php-changes-4) - [Python Changes](#python-changes-5) - [Ruby Changes](#ruby-changes-2) - [Changes in Ice 3.7.0](#changes-in-ice-370) @@ -79,11 +80,10 @@ particular aspect of Ice. - [Java Changes](#java-changes-5) - [JavaScript Changes](#javascript-changes-7) - [Objective-C Changes](#objective-c-changes-2) - - [PHP Changes](#php-changes-4) + - [PHP Changes](#php-changes-5) - [Python Changes](#python-changes-6) - [Ruby Changes](#ruby-changes-3) - # Changes in Ice 3.7.9 These are the changes since Ice 3.7.8. @@ -93,6 +93,13 @@ These are the changes since Ice 3.7.8. - Fixed an IceSSL bug where the OpenSSL error description was not correctly included with the `Ice::ProtocolException` thrown when a read failure occurred. OpenSSL versions previous to `1.1.1e` are not affected. +## PHP Changes + +- Added support for PHP 8.2. + +- `slice2php` will now generate code compatible with PHP >= 7 by default. To generate PHP 5 compatible code, use the + new `--php5` flag. + # Changes in Ice 3.7.8 These are the changes since Ice 3.7.7. diff --git a/cpp/src/slice2php/Main.cpp b/cpp/src/slice2php/Main.cpp index 4b70f734ca4..1b2cfa033f8 100644 --- a/cpp/src/slice2php/Main.cpp +++ b/cpp/src/slice2php/Main.cpp @@ -68,7 +68,7 @@ class CodeVisitor : public ParserVisitor { public: - CodeVisitor(IceUtilInternal::Output&, bool); + CodeVisitor(IceUtilInternal::Output&, bool, bool); virtual void visitClassDecl(const ClassDeclPtr&); virtual bool visitClassDefStart(const ClassDefPtr&); @@ -84,10 +84,6 @@ private: void startNamespace(const ContainedPtr&); void endNamespace(); - void writeClassDef(const ClassDefPtr&, bool); - void writeException(const ExceptionPtr&, bool); - void writeStruct(const StructPtr&, bool); - // // Return the PHP name for the given Slice type. When using namespaces, // this name is a relative (unqualified) name, otherwise this name is the @@ -146,14 +142,16 @@ private: bool _ns; // Using namespaces? list<string> _moduleStack; // TODO: Necessary? set<string> _classHistory; // TODO: Necessary? + bool _php5; // Generate PHP5 compatible code }; // // CodeVisitor implementation. // -CodeVisitor::CodeVisitor(Output& out, bool ns) : +CodeVisitor::CodeVisitor(Output& out, bool ns, bool php5) : _out(out), - _ns(ns) + _ns(ns), + _php5(php5) { } @@ -196,32 +194,6 @@ bool CodeVisitor::visitClassDefStart(const ClassDefPtr& p) { string scoped = p->scoped(); - - startNamespace(p); - - _out << sp << nl << "if (PHP_VERSION_ID < 80200)"; - _out << sb; - writeClassDef(p, false); - _out << eb; - _out << nl << "else"; - _out << sb; - writeClassDef(p, true); - _out << eb; - - endNamespace(); - - if(_classHistory.count(scoped) == 0) - { - _classHistory.insert(scoped); // Avoid redundant declarations. - } - - return false; -} - -void -CodeVisitor::writeClassDef(const ClassDefPtr& p, bool returnTypeDeclaration) -{ - string scoped = p->scoped(); string name = getName(p); string type = getTypeVar(p); string abs = getAbsolute(p, _ns); @@ -235,6 +207,8 @@ CodeVisitor::writeClassDef(const ClassDefPtr& p, bool returnTypeDeclaration) bool isInterface = p->isInterface(); bool isAbstract = isInterface || p->allOperations().size() > 0; // Don't use isAbstract() - see bug 3739 + startNamespace(p); + _out << sp << nl << "global " << type << ';'; if(!p->isLocal() && isAbstract) { @@ -386,13 +360,13 @@ CodeVisitor::writeClassDef(const ClassDefPtr& p, bool returnTypeDeclaration) // // __toString // - if (returnTypeDeclaration) + if (_php5) { - _out << sp << nl << "public function __toString(): string"; + _out << sp << nl << "public function __toString()"; } else { - _out << sp << nl << "public function __toString()"; + _out << sp << nl << "public function __toString(): string"; } _out << sb; @@ -739,35 +713,27 @@ CodeVisitor::writeClassDef(const ClassDefPtr& p, bool returnTypeDeclaration) } } } -} - -bool -CodeVisitor::visitExceptionStart(const ExceptionPtr& p) -{ - startNamespace(p); - - _out << sp << nl << "if (PHP_VERSION_ID < 80200)"; - _out << sb; - writeException(p, false); - _out << eb; - _out << nl << "else"; - _out << sb; - writeException(p, true); - _out << eb; endNamespace(); + if(_classHistory.count(scoped) == 0) + { + _classHistory.insert(scoped); // Avoid redundant declarations. + } + return false; } -void -CodeVisitor::writeException(const ExceptionPtr &p, bool returnTypeDeclaration) +bool +CodeVisitor::visitExceptionStart(const ExceptionPtr& p) { string scoped = p->scoped(); string name = getName(p); string type = getTypeVar(p); string abs = getAbsolute(p, _ns); + startNamespace(p); + _out << sp << nl << "global " << type << ';'; _out << nl << "class " << name << " extends "; ExceptionPtr base = p->base(); @@ -836,13 +802,13 @@ CodeVisitor::writeException(const ExceptionPtr &p, bool returnTypeDeclaration) // // __toString // - if (returnTypeDeclaration) + if (_php5) { - _out << sp << nl << "public function __toString(): string"; + _out << sp << nl << "public function __toString()"; } else { - _out << sp << nl << "public function __toString()"; + _out << sp << nl << "public function __toString(): string"; } _out << sb; @@ -917,29 +883,14 @@ CodeVisitor::writeException(const ExceptionPtr &p, bool returnTypeDeclaration) _out << "null"; } _out << ");"; -} - -bool -CodeVisitor::visitStructStart(const StructPtr& p) -{ - startNamespace(p); - - _out << sp << nl << "if (PHP_VERSION_ID < 80200)"; - _out << sb; - writeStruct(p, false); - _out << eb; - _out << nl << "else"; - _out << sb; - writeStruct(p, true); - _out << eb; endNamespace(); return false; } -void -CodeVisitor::writeStruct(const StructPtr& p, bool returnTypeDeclaration) +bool +CodeVisitor::visitStructStart(const StructPtr& p) { string scoped = p->scoped(); string name = getName(p); @@ -958,6 +909,8 @@ CodeVisitor::writeStruct(const StructPtr& p, bool returnTypeDeclaration) } } + startNamespace(p); + _out << sp << nl << "global " << type << ';'; _out << nl << "class " << name; @@ -975,13 +928,13 @@ CodeVisitor::writeStruct(const StructPtr& p, bool returnTypeDeclaration) // // __toString // - if (returnTypeDeclaration) + if (_php5) { - _out << sp << nl << "public function __toString(): string"; + _out << sp << nl << "public function __toString()"; } else { - _out << sp << nl << "public function __toString()"; + _out << sp << nl << "public function __toString(): string"; } _out << sb; _out << nl << "global " << type << ';'; @@ -1034,6 +987,10 @@ CodeVisitor::writeStruct(const StructPtr& p, bool returnTypeDeclaration) _out.dec(); } _out << "));"; + + endNamespace(); + + return false; } void @@ -1553,7 +1510,7 @@ CodeVisitor::collectExceptionMembers(const ExceptionPtr& p, MemberInfoList& allM } static void -generate(const UnitPtr& un, bool all, bool checksum, bool ns, const vector<string>& includePaths, Output& out) +generate(const UnitPtr& un, bool all, bool checksum, bool ns, bool php5, const vector<string>& includePaths, Output& out) { if(!all) { @@ -1584,7 +1541,7 @@ generate(const UnitPtr& un, bool all, bool checksum, bool ns, const vector<strin } } - CodeVisitor codeVisitor(out, ns); + CodeVisitor codeVisitor(out, ns, php5); un->visit(&codeVisitor, false); if(checksum) @@ -1714,6 +1671,7 @@ usage(const string& n) " deprecated: use instead [[\"ice-prefix\"]] metadata.\n" "--underscore Allow underscores in Slice identifiers\n" " deprecated: use instead [[\"underscore\"]] metadata.\n" + "--php5 Generate PHP5 compatible code.\n" ; } @@ -1738,6 +1696,7 @@ compile(const vector<string>& argv) opts.addOpt("", "all"); opts.addOpt("", "checksum"); opts.addOpt("n", "no-namespace"); + opts.addOpt("", "php5"); bool validate = find(argv.begin(), argv.end(), "--validate") != argv.end(); @@ -1809,6 +1768,8 @@ compile(const vector<string>& argv) bool ns = !opts.isSet("no-namespace"); + bool php5 = opts.isSet("php5"); + if(args.empty()) { consoleErr << argv[0] << ": error: no input file" << endl; @@ -1960,7 +1921,7 @@ compile(const vector<string>& argv) // // Generate the PHP mapping. // - generate(u, all, checksum, ns, includePaths, out); + generate(u, all, checksum, ns, php5, includePaths, out); out << "?>\n"; out.close(); diff --git a/man/man1/slice2php.1 b/man/man1/slice2php.1 index 82b5ddea1c7..fc597eeeffc 100644 --- a/man/man1/slice2php.1 +++ b/man/man1/slice2php.1 @@ -101,6 +101,11 @@ Generate code without support for PHP namespaces (deprecated). .br Generate checksums for Slice definitions. +.TP +.BR \-\-php5 " " CLASS\fR +.br +Generate PHP5 compatible code. + .SH SEE ALSO .BR slice2cpp (1), diff --git a/php/Makefile b/php/Makefile index 7dc73064a5a..0f86ed87c4d 100644 --- a/php/Makefile +++ b/php/Makefile @@ -24,7 +24,7 @@ ifeq ($(filter all php,$(ICE_BIN_DIST)),) # Load Makefile.mk fragments # ifeq ($(filter tests,$(MAKECMDGOALS)),) - ifeq ($(shell [ $$($(PHP_CONFIG) --vernum) -lt 70000 ] && echo 0),0) + ifeq ($(php_major_version),5) include $(lang_srcdir)/src/php5/Makefile.mk else include $(lang_srcdir)/src/php/Makefile.mk diff --git a/php/config/Make.rules b/php/config/Make.rules index 2975864fc63..fe7a7d91e6d 100644 --- a/php/config/Make.rules +++ b/php/config/Make.rules @@ -18,10 +18,16 @@ USE_NAMESPACES ?= yes # Don't change anything below this line! # ---------------------------------------------------------------------- +php_major_version = $(firstword $(subst ., ,$(shell $(PHP_CONFIG) --version))) + ifneq ($(USE_NAMESPACES),yes) slice2php_flags = --no-namespace endif +ifeq ($(php_major_version),5) +slice2php_flags += --php5 +endif + # # $(call make-php-test-project,$1=testdir) # |