fix: fix PHP readonly legacy files for nested messages

pull/10320/head
Brent Shaffer 3 years ago
parent 7cd0b6fbf1
commit c752a43e1e
  1. 1
      php/tests/GeneratedClassTest.php
  2. 2
      php/tests/proto/test_php_namespace.proto
  3. 33
      src/google/protobuf/compiler/php/php_generator.cc

@ -338,6 +338,7 @@ class GeneratedClassTest extends TestBase
{ {
$this->assertTrue(class_exists('\Upper\READONLY')); $this->assertTrue(class_exists('\Upper\READONLY'));
$this->assertTrue(class_exists('\Lower\readonly')); $this->assertTrue(class_exists('\Lower\readonly'));
$this->assertTrue(class_exists('\Php\Test\TestNamespace\PBEmpty\ReadOnly'));
} }
public function testLegacyReadOnlyEnum() public function testLegacyReadOnlyEnum()

@ -27,5 +27,7 @@ message TestNamespace {
enum NestedEnum { enum NestedEnum {
ZERO = 0; ZERO = 0;
}; };
// Test previously unreserved name
message ReadOnly {}
} }
} }

@ -421,12 +421,17 @@ std::string LegacyGeneratedClassFileName(const DescriptorType* desc,
} }
template <typename DescriptorType> template <typename DescriptorType>
std::string LegacyReadOnlyGeneratedClassFileName(const DescriptorType* desc, std::string LegacyReadOnlyGeneratedClassFileName(std::string php_namespace,
const Options& options) { const DescriptorType* desc) {
std::string php_namespace = RootPhpNamespace(desc, options);
if (!php_namespace.empty()) { if (!php_namespace.empty()) {
for (int i = 0; i < php_namespace.size(); i++) {
if (php_namespace[i] == '\\') {
php_namespace[i] = '/';
}
}
return php_namespace + "/" + desc->name() + ".php"; return php_namespace + "/" + desc->name() + ".php";
} }
return desc->name() + ".php"; return desc->name() + ".php";
} }
@ -1316,27 +1321,39 @@ template <typename DescriptorType>
void LegacyReadOnlyGenerateClassFile(const FileDescriptor* file, void LegacyReadOnlyGenerateClassFile(const FileDescriptor* file,
const DescriptorType* desc, const Options& options, const DescriptorType* desc, const Options& options,
GeneratorContext* generator_context) { GeneratorContext* generator_context) {
std::string filename = LegacyReadOnlyGeneratedClassFileName(desc, options); std::string fullname = FullClassName(desc, options);
std::string php_namespace;
std::string classname;
int lastindex = fullname.find_last_of("\\");
if (lastindex != std::string::npos) {
php_namespace = fullname.substr(0, lastindex);
classname = fullname.substr(lastindex + 1);
} else {
php_namespace = "";
classname = fullname;
}
std::string filename = LegacyReadOnlyGeneratedClassFileName(php_namespace, desc);
std::unique_ptr<io::ZeroCopyOutputStream> output( std::unique_ptr<io::ZeroCopyOutputStream> output(
generator_context->Open(filename)); generator_context->Open(filename));
io::Printer printer(output.get(), '^'); io::Printer printer(output.get(), '^');
GenerateHead(file, &printer); GenerateHead(file, &printer);
std::string php_namespace = RootPhpNamespace(desc, options);
if (!php_namespace.empty()) { if (!php_namespace.empty()) {
printer.Print( printer.Print(
"namespace ^name^;\n\n", "namespace ^name^;\n\n",
"name", php_namespace); "name", php_namespace);
} }
std::string newname = FullClassName(desc, options);
printer.Print("class_exists(^new^::class); // autoload the new class, which " printer.Print("class_exists(^new^::class); // autoload the new class, which "
"will also create an alias to the deprecated class\n", "will also create an alias to the deprecated class\n",
"new", GeneratedClassNameImpl(desc)); "new", classname);
printer.Print("@trigger_error(__NAMESPACE__ . '\\^old^ is deprecated and will be removed in " printer.Print("@trigger_error(__NAMESPACE__ . '\\^old^ is deprecated and will be removed in "
"the next major release. Use ^fullname^ instead', E_USER_DEPRECATED);\n\n", "the next major release. Use ^fullname^ instead', E_USER_DEPRECATED);\n\n",
"old", desc->name(), "old", desc->name(),
"fullname", newname); "fullname", classname);
} }
void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en, void GenerateEnumFile(const FileDescriptor* file, const EnumDescriptor* en,

Loading…
Cancel
Save