Skip to content

Commit

Permalink
chore: update PHP examples
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Sep 27, 2022
1 parent 925b19d commit f011823
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 50 deletions.
2 changes: 1 addition & 1 deletion Makefile
Expand Up @@ -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)

Expand Down
5 changes: 0 additions & 5 deletions WORKSPACE
Expand Up @@ -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",
Expand Down
49 changes: 28 additions & 21 deletions docs/source/guides/grpc.rst
Expand Up @@ -466,28 +466,31 @@ gRPC server:
.. tab-item:: Swift
:sync: swift

:bdg-info:`Requirements:` Make sure to follow the :github:`instructions <grpc/grpc-swift/blob/main/docs/quick-start.md#prerequisites>` to install ``protoc`` plugin for gRPC Swift.
.. tab-item:: PHP
:sync: php

Additionally, you will need to also have :github:`gRPC Swift <grpc/grpc-swift>` and :github:`protocolbuffers/protobuf` available locally.
:bdg-info:`Requirements:` Make sure to follow the :github:`instructions <grpc/grpc/blob/master/src/php/README.md>` to install ``grpc`` via either `pecl <https://pecl.php.net/>`_ or from source.

We will create our Swift client in the directory ``~/workspace/iris_swift_client/``:
We will then use `bazel <bazel.build>`_, `composer <https://getcomposer.org/>`_ 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

Expand All @@ -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::

Expand Down Expand Up @@ -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 <grpc/grpc/blob/master/src/php/README.md#overview>` overview for PHP client implementation.

.. tab-item:: Ruby
:sync: ruby

Expand Down
20 changes: 10 additions & 10 deletions grpc-client/README.md
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion grpc-client/go/client
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions 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
```
14 changes: 10 additions & 4 deletions grpc-client/php/client
Expand Up @@ -2,18 +2,24 @@

set -e

if ! $(which php >/dev/null); then
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 ./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.so}

php -d extension="${COMPILED_PATH}" -d max_execution_time=300 BentoServiceClient.php
11 changes: 4 additions & 7 deletions grpc-client/php/codegen
Expand Up @@ -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..."
Expand All @@ -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
2 changes: 1 addition & 1 deletion grpc-client/swift/client
Expand Up @@ -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
Expand Down

0 comments on commit f011823

Please sign in to comment.