From 9aec87cf9e8366b99ec7744eaa69bd0d83c7fab0 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Mon, 26 Sep 2022 17:08:55 -0700 Subject: [PATCH] chore: update PHP examples Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com> --- Makefile | 2 +- WORKSPACE | 5 ---- docs/source/guides/grpc.rst | 49 +++++++++++++++++++++---------------- grpc-client/README.md | 20 +++++++-------- grpc-client/go/client | 2 +- grpc-client/php/README.md | 15 ++++++++++++ grpc-client/php/client | 18 +++++++++++--- grpc-client/php/codegen | 11 +++------ grpc-client/swift/client | 2 +- 9 files changed, 74 insertions(+), 50 deletions(-) create mode 100644 grpc-client/php/README.md diff --git a/Makefile b/Makefile index 3ed9c6ef51..8d781d9802 100644 --- a/Makefile +++ b/Makefile @@ -69,7 +69,7 @@ install-docs-deps: ## Install documentation dependencies # Docs watch-docs: install-docs-deps ## Build and watch documentation - sphinx-autobuild docs/source docs/build/html --watch $(GIT_ROOT)/bentoml --ignore "*/bazel-*" + sphinx-autobuild docs/source docs/build/html --watch $(GIT_ROOT)/bentoml --ignore "bazel-*" spellcheck-docs: ## Spell check documentation sphinx-build -b spelling ./docs/source ./docs/build || (echo "Error running spellchecker.. You may need to run 'make install-spellchecker-deps'"; exit 1) diff --git a/WORKSPACE b/WORKSPACE index c8083e9919..2f66ccc402 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -61,11 +61,6 @@ go_register_toolchains(version = "1.19") # Currently c3714eced8c51db9092e0adc2a1dfb715655c795 address # some build issues with upb for C++. # TODO: Update this to v1.50.0 when 1.50.0 is out. -http_archive( - name = "com_google_protobuf", - strip_prefix = "protobuf-3.19.4", - urls = ["https://github.com/protocolbuffers/protobuf/archive/v3.19.4.zip"], -) http_archive( name = "com_github_grpc_grpc", strip_prefix = "grpc-c3714eced8c51db9092e0adc2a1dfb715655c795", diff --git a/docs/source/guides/grpc.rst b/docs/source/guides/grpc.rst index 0f429afa2e..50f179976b 100644 --- a/docs/source/guides/grpc.rst +++ b/docs/source/guides/grpc.rst @@ -466,28 +466,31 @@ gRPC server: .. tab-item:: Swift :sync: swift - :bdg-info:`Requirements:` Make sure to follow the :github:`instructions ` to install ``protoc`` plugin for gRPC Swift. + .. tab-item:: PHP + :sync: php - Additionally, you will need to also have :github:`gRPC Swift ` and :github:`protocolbuffers/protobuf` available locally. + :bdg-info:`Requirements:` Make sure to follow the :github:`instructions ` to install ``grpc`` via either `pecl `_ or from source. - We will create our Swift client in the directory ``~/workspace/iris_swift_client/``: + We will then use `bazel `_, `composer `_ to build and run the client. + + We will create our PHP client in the directory ``~/workspace/iris_php_client/``: .. code-block:: bash - » mkdir -p ~/workspace/iris_swift_client - » cd ~/workspace/iris_swift_client + » mkdir -p ~/workspace/iris_php_client + » cd ~/workspace/iris_php_client - Create a new Swift package: + Create a new PHP package: .. code-block:: bash - » swift package init --name BentoServiceClient + » composer init - .. dropdown:: An example ``Package.swift`` for the client: + .. dropdown:: An example ``composer.json`` for the client: :icon: code - .. literalinclude:: ../../../grpc-client/swift/Package.swift - :language: swift + .. literalinclude:: ../../../grpc-client/php/composer.json + :language: json .. include:: ./snippets/grpc/additional_setup.rst @@ -496,15 +499,16 @@ gRPC server: .. code-block:: bash » protoc -I . -I ./thirdparty/protobuf/src \ - --swift_out=Source/ --swift_opt=Visibility=Public \ - --grpc-swift_out=Source/ --grpc-swift_opt=Visibility=Public \ + --php_out=. \ + --grpc_out=. \ + --plugin=protoc-gen-grpc=$(which grpc_php_plugin) \ bentoml/grpc/v1alpha1/service.proto - Proceed to create a ``Sources/BentoServiceClient/main.swift`` file with the following content: + Proceed to create a ``BentoServiceClient.php`` file with the following content: - .. literalinclude:: ../../../grpc-client/swift/Sources/BentoServiceClient/main.swift - :language: swift - :caption: `main.swift` + .. literalinclude:: ../../../grpc-client/php/BentoServiceClient.php + :language: php + :caption: `BentoServiceClient.php` .. TODO:: @@ -629,16 +633,19 @@ Then you can proceed to run the client scripts: » swift run BentoServiceClient + .. tab-item:: PHP + :sync: php + + .. code-block:: bash + + » php -d extension=/path/to/grpc.so -d max_execution_time=300 BentoServiceClient.php + + .. dropdown:: Additional language support for client implementation :icon: triangle-down .. tab-set:: - .. tab-item:: PHP - :sync: php - - :bdg-primary:`Note:` Please check out the :github:`gRPC PHP ` overview for PHP client implementation. - .. tab-item:: Ruby :sync: ruby diff --git a/grpc-client/README.md b/grpc-client/README.md index f7c7fde6ae..da4ade4d5f 100644 --- a/grpc-client/README.md +++ b/grpc-client/README.md @@ -26,16 +26,16 @@ bazel build ... The following table contains command to run clients: -| Language | Command | -| ------------------ | -------------------------------------------- | -| [Python](./python) | `bazel run //grpc-client/python:client` | -| [C++](./cpp) | `bazel run //grpc-client/cpp:client` | -| [Go](./go) | `bazel run //grpc-client/go:client` | -| [Java](./java) | `bazel run //grpc-client/java:client` | -| [Kotlin](./kotlin) | `bazel run //grpc-client/kotlin:client` | -| [Swift](./swift) | `pushd swift && client && popd` | -| [Node.js](./node) | `pushd node && yarn client && popd` | -| [PHP](./php) | `pushd php && ./codegen && ./client && popd` | +| Language | Command | +| ------------------ | --------------------------------------- | +| [Python](./python) | `bazel run //grpc-client/python:client` | +| [C++](./cpp) | `bazel run //grpc-client/cpp:client` | +| [Go](./go) | `bazel run //grpc-client/go:client` | +| [Java](./java) | `bazel run //grpc-client/java:client` | +| [Kotlin](./kotlin) | `bazel run //grpc-client/kotlin:client` | +| [Swift](./swift) | `pushd swift && client && popd` | +| [Node.js](./node) | `pushd node && yarn client && popd` | +| [PHP](./php) | See [PHP instruction](./php/README.md) | > For Swift client, make sure to compile gRPC Swift `protoc` beforehand to generate the client stubs. diff --git a/grpc-client/go/client b/grpc-client/go/client index dc8ed55cbf..fa007a19e4 100755 --- a/grpc-client/go/client +++ b/grpc-client/go/client @@ -4,7 +4,7 @@ set -e pushd .. >/dev/null -if ! $(which protoc-gen-go-grpc &>/dev/null); then +if ! which protoc-gen-go-grpc &>/dev/null; then echo "protoc-gen-go-grpc is missing. Make sure to install it and add it to your PATH." exit 1 fi diff --git a/grpc-client/php/README.md b/grpc-client/php/README.md new file mode 100644 index 0000000000..b262b7c204 --- /dev/null +++ b/grpc-client/php/README.md @@ -0,0 +1,15 @@ +### Instruction + +Make sure to have [`grpc extension`](https://github.com/grpc/grpc/blob/master/src/php/README.md) installed + +Generate the stubs: + +```bash +./codegen +``` + +The run the client: + +```bash +COMPILED_GRPC_SO=/path/to/grpc.so ./client +``` diff --git a/grpc-client/php/client b/grpc-client/php/client index a78011bde8..f3e9073f5c 100755 --- a/grpc-client/php/client +++ b/grpc-client/php/client @@ -2,18 +2,28 @@ set -e -if ! $(which php >/dev/null); then +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" + +cd "$SCRIPT_DIR" || exit 1 + +if ! which php >/dev/null; then echo "PHP is required." exit 0 fi -if ! $(which composer >/dev/null); then +if ! which composer >/dev/null; then echo "composer is required." exit 0 fi -[[ -f ./composer.lock ]] && composer update || composer install +if ! [[ -f $SCRIPT_DIR/composer.lock ]]; then + composer install +else + composer update +fi echo "Running PHP client" -php -d extension=grpc -d max_execution_time=300 BentoServiceClient.php +COMPILED_PATH=${COMPILED_GRPC_SO:-grpc} + +php -d extension="${COMPILED_PATH}" -d max_execution_time=300 BentoServiceClient.php diff --git a/grpc-client/php/codegen b/grpc-client/php/codegen index cae9aee899..254d8e4b8c 100755 --- a/grpc-client/php/codegen +++ b/grpc-client/php/codegen @@ -2,7 +2,8 @@ set -e -pushd "$(dirname "$0")"/../.. >/dev/null +SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" &>/dev/null && pwd)" +pushd "$SCRIPT_DIR"/../.. >/dev/null # We will use BentoML tools/bazel.rc echo "Building shared C++ gRPC..." @@ -14,13 +15,9 @@ if ! [ -f "bazel-bin/src/compiler/grpc_php_plugin" ]; then bazel build @com_github_grpc_grpc//src/compiler:grpc_php_plugin fi -PROTOC=$(pwd)/bazel-bin/external/com_google_protobuf/protoc -PLUGIN=protoc-gen-grpc=$(pwd)/bazel-bin/src/compiler/grpc_php_plugin -popd >/dev/null - -pushd .. >/dev/null +PLUGIN=protoc-gen-grpc=$(pwd)/bazel-bin/external/com_github_grpc_grpc/src/compiler/grpc_php_plugin echo "Generate PHP stubs." -"$PROTOC" -I . -I ./thirdparty/protobuf/src --php_out=php --grpc_out=php --plugin="$PLUGIN" bentoml/grpc/v1alpha1/service.proto +bazel run @com_google_protobuf//:protoc -- -I ./grpc-client -I ./grpc-client/thirdparty/protobuf/src --php_out=grpc-client/php --grpc_out=grpc-client/php --plugin="$PLUGIN" grpc-client/bentoml/grpc/v1alpha1/service.proto popd >/dev/null diff --git a/grpc-client/swift/client b/grpc-client/swift/client index b3faa67d1f..aee92be6ba 100755 --- a/grpc-client/swift/client +++ b/grpc-client/swift/client @@ -4,7 +4,7 @@ set -e pushd .. >/dev/null -if ! $(which protoc-gen-grpc-swift &>/dev/null); then +if ! which protoc-gen-grpc-swift &>/dev/null; then echo "protoc-gen-grpc-swift is missing. Make sure to compile it first and add it to your PATH." exit 1 fi