Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

php namespace support for GPBMetadata #3970

Closed
cc5092 opened this issue Nov 29, 2017 · 14 comments
Closed

php namespace support for GPBMetadata #3970

cc5092 opened this issue Nov 29, 2017 · 14 comments
Assignees

Comments

@cc5092
Copy link

cc5092 commented Nov 29, 2017

#3162

TeBoring commented on 2 Jun
I don't think we need to change the namespace of GPBMetadata files. These files are transparent to users.

As you said, these GPBMetadata files are transparent to users. But I think it still makes sense to add namespace for GPBMetadata file.

Suppose I have 2 grpc go services and are developed by different teams. They define the protobuf file by "user.proto". this "user.proto" will generate a GPBMetadata\User.php file.

My laravel project needs to use both two grpc services they provided and for the psr-4 autoload, the composer.json file needs to be defined as follows:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    },
    "classmap": [
        "app/FirstService/GPBMetadata/",
        "app/SecondService/GPBMetadata/"
    ]
}

Executing "composer dumpautoload" in the root directory of project, and we will get notice like this:

Warning: Ambiguous class resolution, "GPBMetadata\User" was found in both "$baseDir . 'app/FirstService/GPBMetadata/User.php" and "path_to_proj/app/SecondService/GPBMetadata/User.php", the first will be used.

So I think it makes sense to add GPBMetadata namespace support.

Looking forward to your reply.

ping @TeBoring

@TeBoring TeBoring added the php label Nov 29, 2017
@TeBoring
Copy link
Contributor

I see this is a problem. The current namespace of GPBMetatdata file is related to the place of proto file, e.g., foo/bar.proto will become GPBMetadata/Foo/Bar.php
One way to solve this is the way you mentioned.
The other way is that user could put their proto files in a unique place, e.g., first_service/user.proto and second_service/user.proto will become GPBMetadata/FirstService/User.php and GPBMetadata/SecondService/User.php.
As far as I see, both ways needs action from the user side. However, the second option has fewer maintaining cost, because user only need to put all proto files there. The first option will need to add the file option in every proto file.

@cc5092
Copy link
Author

cc5092 commented Nov 29, 2017

Hi
I can't do like your second option to generate GPBMetadata/FirstService/User.php.
Here is my code tree path.
image

current pwd is path_to_proj/proto/ and the compile command is

protoc --proto_path=first/ --php_out=. --grpc_out=. --plugin=protoc-gen-grpc=/usr/local/bin/grpc_php_plugin first/*.proto

The Pb class generate namespace successfully by option php_namespace in proto file. But the GPBMetadata still generate as GPBMetadata\User.php.

@TeBoring
Copy link
Contributor

Is --proto_path=first/ required? If it is removed, you can get GPBMetadata\First\User.php

@TeBoring
Copy link
Contributor

The import in your proto file may need to be changed accordingly.

@cc5092
Copy link
Author

cc5092 commented Dec 4, 2017

Ok, I tried second option, it works fine. thank you. @TeBoring

The remain problem is the namespace of GPBMetadata is not the same with the stub class.

For example. stub class namespace is "FirstService\User.php", but GPBMetadata is "GPBMetadata\FirstService\User.php". It's better if is "FirstService\GPBMetadata\User.php".

@cc5092
Copy link
Author

cc5092 commented Dec 4, 2017

And for psr-4 autoload, namespace like "FirstService\GPBMetadata\User.php" makes sense for create standalone library for other developer to import.

@fiboknacky
Copy link
Contributor

+1 for this.
It'd be great if the GPBMetadata is generated based on the proto option for specifying namespaces.

@andydeng
Copy link

+1. This issue affects our project too. It will be great if we can choose GPBMetadata's namespace.

@atrauzzi
Copy link

atrauzzi commented Mar 31, 2018

Looking forward to seeing this resolved, would hate to get collisions in any projects that rely on multiple PB-based implementations.

The GPBNamespace without a prefix requires us to modify our autoloader with a very specific namespace which is inconvenient. If it followed the namespace of the classes themselves, I could have protoc output to a .gitignored directory.

@atrauzzi
Copy link

Separate note: It would also be nice if while all this was being done, GPBMetadata was switched to GpbMetadata so as to follow PSR conventions.

@TeBoring
Copy link
Contributor

TeBoring commented May 1, 2018

I will add a file option to add GPBMetadata namespace.

@atrauzzi
Copy link

atrauzzi commented May 2, 2018

Can you follow community+PSR conventions and make it GpbMetadata? Or at least let it be configurable to be PSR compliant?

@TeBoring
Copy link
Contributor

Added in #4622

@atrauzzi
Copy link

atrauzzi commented Jul 1, 2018

🎉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants