Skip to content

PHP namespaces for nested messages and enums#4536

Merged
TeBoring merged 26 commits intoprotocolbuffers:php-generated-devfrom
bshaffer:php-submessage-namespaces
May 23, 2018
Merged

PHP namespaces for nested messages and enums#4536
TeBoring merged 26 commits intoprotocolbuffers:php-generated-devfrom
bshaffer:php-submessage-namespaces

Conversation

@bshaffer
Copy link
Copy Markdown
Contributor

The proto

message TestMessage {
  NestedEnum nested_enum = 1;
  NestedMessage nested_message = 2;

  enum NestedEnum {
      ZERO = 0;
  }

  // Test nested messages
  message NestedMessage {
      int32 a = 1;
  }
}

Will result in the files

TestMessage.php
TestMessage/NestedEnum.php
TestMessage/NestedMessage.php

With namespaces corresponding to the classes, e.g.

namespace TestMessage;

class NestedEnum
{
    // ...
}

@anandolee
Copy link
Copy Markdown
Contributor

ping @TeBoring

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClassNamePrefix?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shall we merge the above branch and here?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a test in which containing message has reserved name? For example:

message Empty {
  message Nested {}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

@TeBoring
Copy link
Copy Markdown
Contributor

TeBoring commented May 3, 2018

Previously, we didn't take into account reserved name in namespace. Can we also fix it?

@TeBoring
Copy link
Copy Markdown
Contributor

TeBoring commented May 3, 2018

@bshaffer bshaffer force-pushed the php-submessage-namespaces branch 3 times, most recently from 7d15c80 to 3f276bb Compare May 3, 2018 21:47
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do

if (!php_namespace.empty() ||
    (!file->options().has_php_namespace() && !file->package().empty()) ||
    lastindex != string::npos) {
...
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, I think we just need to check lastindex != string::npos

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

@xfxyjwf
Copy link
Copy Markdown
Contributor

xfxyjwf commented May 14, 2018

Ping @TeBoring

@TeBoring TeBoring force-pushed the php-generated-dev branch from f073b35 to a77d3a9 Compare May 15, 2018 16:50
@bshaffer bshaffer force-pushed the php-submessage-namespaces branch 3 times, most recently from d07aac6 to 54c9685 Compare May 16, 2018 19:01
@bshaffer bshaffer force-pushed the php-submessage-namespaces branch from 54c9685 to 46311fc Compare May 17, 2018 14:42
@TeBoring
Copy link
Copy Markdown
Contributor

I think in php_generattor.cc, you need to replace FullClassName with LegacyFullClassName in several places in case depended message is still in old generated code.

$this->markTestSkipped('Protobuf extension fails when phpunit runs in a separate process');
}
$this->legacyEnum(new NestedEnum);
$this->legacyEnum(new TestLegacyMessage\NestedEnum);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply set new TestLegacyMessage_NestedEnum?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you just need new TestLegacyMessage_NestedEnum

$this->markTestSkipped('Protobuf extension fails when phpunit runs in a separate process');
}
$this->legacyMessage(new Sub);
$this->legacyMessage(new TestLegacyMessage\NestedMessage);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use TestLegacyMessage_NestedMessage

@TeBoring
Copy link
Copy Markdown
Contributor

Currently, it still generate GPBMetadata/Proto/Empty/Echo.php for empty/echo.proto

@bshaffer
Copy link
Copy Markdown
Contributor Author

here is an example of the deprecated class file and message. I would love to get @dwsupplee and @michaelbausor 's thoughts on this!

@michaelbausor
Copy link
Copy Markdown
Contributor

@bshaffer
Copy link
Copy Markdown
Contributor Author

bshaffer commented May 22, 2018

@michaelbausor Yes, it ensures that if someone creates the deprecated class, the new class is loaded and the call to class_alias(new::class, old::class) is executed. So overall the file allows a user's code to continue using the old class name, but it issues a deprecated warning in any custom logger/error handler, and will show up as @deprecated in the IDE

@TeBoring TeBoring merged commit 6ae6e1b into protocolbuffers:php-generated-dev May 23, 2018
@TeBoring
Copy link
Copy Markdown
Contributor

@michaelbausor and @dwsupplee, if you have other issues, we can fix them in another PR.

@bshaffer bshaffer deleted the php-submessage-namespaces branch May 24, 2018 18:12
TeBoring pushed a commit that referenced this pull request May 24, 2018
* uses namespaces for nested messages and enums

* fixes namespaces for PHP dist

* fixes namespace for Descriptors, adds Cardinality and Kind

* fixes nested namespaces for reserved words and adds tests

* adds tests and generator fix for php class prefixes

* fixes escaping of protobuf packages, enum comments, misc others

* nice refactor of generated code

* adds class files for backwards compatibility

* simplifies code with templates

* adds compatibility files to makefile

* cleanup of generator and fixes nested namespace bug

* regenerates proto types

* remove internal BC classes

* adds deprecated warning, adds methods back

* simplifies if statement

* fixes dist files

* addresses review comments

* adds back TYPE_URL_PREFIX constant

* adds @deprecated to old nested class files

* skips tests which require a separate process when protobuf.so is enabled

* Adds tests for legacy nested classes that do not require separate processes to test

* uses legacy names for GPBUtil message check

* adds block for IDE @deprecated message

* Namespace for nested message/enum in c extension

* Remove unused code
TeBoring pushed a commit to TeBoring/protobuf that referenced this pull request May 25, 2018
* uses namespaces for nested messages and enums

* fixes namespaces for PHP dist

* fixes namespace for Descriptors, adds Cardinality and Kind

* fixes nested namespaces for reserved words and adds tests

* adds tests and generator fix for php class prefixes

* fixes escaping of protobuf packages, enum comments, misc others

* nice refactor of generated code

* adds class files for backwards compatibility

* simplifies code with templates

* adds compatibility files to makefile

* cleanup of generator and fixes nested namespace bug

* regenerates proto types

* remove internal BC classes

* adds deprecated warning, adds methods back

* simplifies if statement

* fixes dist files

* addresses review comments

* adds back TYPE_URL_PREFIX constant

* adds @deprecated to old nested class files

* skips tests which require a separate process when protobuf.so is enabled

* Adds tests for legacy nested classes that do not require separate processes to test

* uses legacy names for GPBUtil message check

* adds block for IDE @deprecated message

* Namespace for nested message/enum in c extension

* Remove unused code
@xfxyjwf xfxyjwf added the php label Jun 22, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants