Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/env/global.env
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ PODMAN_VERSION: 4.4.4

GOVER: 1.26

PHPVER: 8.4

KUBERNETES_VERSION: v1.32.0
KIND_VERSION: v0.26.0
KIND_IMAGE_SHA: sha256:c48c62eac5da28cdadcf560d1d8616cfa6783b58f0d94cf63ad1bf49600cb027
114 changes: 114 additions & 0 deletions .github/workflows/validate_php_quickstarts.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
#
# Copyright 2023 The Dapr Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

name: Validate php quickstart

on:
workflow_dispatch:
push:
branches:
- master
- feature/new_quickstarts
- release-*
tags:
- v*
pull_request:
branches:
- master
- feature/new_quickstarts
- release-*
jobs:
deploy:
name: Validate PHP quickstarts on ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 130
strategy:
matrix:
os: [ubuntu-latest]
fail-fast: false
steps:
- name: Check out code
uses: actions/checkout@v4
- name: Load environment variables
uses: artursouza/export-env-action@v2
with:
envFile: "./.github/env/global.env"
expand: "true"
- name: Install podman - MacOS
timeout-minutes: 15
if: matrix.os == 'macos-latest'
run: |
# Install podman
curl -sL -o podman.pkg https://github.com/containers/podman/releases/download/v${{ env.PODMAN_VERSION }}/podman-installer-macos-amd64.pkg
sudo installer -pkg podman.pkg -target /
export PATH=/opt/podman/bin:$PATH
echo "/opt/podman/bin" >> $GITHUB_PATH
# Start podman machine
sudo podman-mac-helper install
podman machine init
podman machine start --log-level debug
echo "CONTAINER_RUNTIME=podman" >> $GITHUB_ENV
- name: Install podman-compose
if: matrix.os == 'macos-latest'
run: |
sudo pip3 install podman-compose
- name: Create symbolic link between Podman and Docker
if: matrix.os == 'macos-latest'
run: |
sudo ln -s $(which podman) /usr/local/bin/docker
sudo ln -s $(which podman-compose) /usr/local/bin/docker-compose
- name: Set up PHP ${{ env.PHPVER }}
uses: shivammathur/setup-php@v2
with:
php-version: ${{ env.PHPVER }}
extensions: curl, mbstring
tools: composer
- name: Verify PHP version
run: php --version && composer --version
- name: Set up Dapr CLI - Mac/Linux
if: matrix.os != 'windows-latest'
run: wget -q ${{ env.DAPR_INSTALL_URL }}/install.sh -O - | /bin/bash -s ${{ env.DAPR_CLI_VERSION }}
- name: Set up Dapr CLI - Windows
if: matrix.os == 'windows-latest'
run: powershell -Command "\$$script=iwr -useb ${{ env.DAPR_INSTALL_URL }}/install.ps1; \$$block=[ScriptBlock]::Create(\$$script); invoke-command -ScriptBlock \$$block -ArgumentList ${{ env.DAPR_CLI_VERSION }}"
- name: Install Dapr
run: |
export GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }}
dapr init --runtime-version=${{ env.DAPR_RUNTIME_VERSION }}
dapr --version
- name: Install utilities dependencies
run: |
echo "PATH=$PATH:$HOME/.local/bin" >> $GITHUB_ENV
pip3 install setuptools wheel
pip3 install mechanical-markdown
- name: Validate building blocks with PHP
run: |
variants=("http" "sdk")
building_blocks=$(find . -maxdepth 1 -mindepth 1 -type d)
for building_block in $building_blocks; do
for variant in "${variants[@]}"
do
if [ ! -d "$building_block/php/$variant" ];
then
echo "$building_block/php/$variant does not exist."
else
pushd $building_block/php/$variant
echo "Validating $building_block/php/$variant quickstart"
make validate
popd
fi
done
done
- name: Linkcheck README.md
run: |
make validate
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ github.com
## Build generated
target/
node_modules
vendor/
composer.phar
release.properties
.mvn
mvnw
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ To run the samples, you need to have Dapr installed. Follow the [Getting Started
- Java: `make test_java_quickstarts`
- JS: `make test_javascript_quickstarts`
- C#: `make test_csharp_quickstarts`
- PHP: `make test_php_quickstarts`
- All quickstarts: `make test_all_quickstarts`

### Validating a single quickstart
Expand Down
21 changes: 19 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ MM_SHELL ?= bash -c
all: install_mm validat

# Run all tests at once
test_all_quickstarts: test_go_quickstarts test_python_quickstarts test_csharp_quickstarts test_java_quickstarts test_javascript_quickstarts
test_all_quickstarts: test_go_quickstarts test_python_quickstarts test_csharp_quickstarts test_java_quickstarts test_javascript_quickstarts test_php_quickstarts
@echo "All quickstart tests complete!"

##################################################
Expand Down Expand Up @@ -323,4 +323,21 @@ test_javascript_quickstarts:
fi; \
done; \
done
@echo "JavaScript quickstart testing complete!"
@echo "JavaScript quickstart testing complete!"

# Test PHP quickstarts
.PHONY: test_php_quickstarts
test_php_quickstarts:
@echo "Testing all PHP quickstarts..."
@building_blocks=$$(find . -maxdepth 1 -mindepth 1 -type d); \
for building_block in $$building_blocks; do \
for variant in "http" "sdk"; do \
if [ ! -d "$$building_block/php/$$variant" ]; then \
echo "$$building_block/php/$$variant does not exist."; \
else \
echo "Validating $$building_block/php/$$variant quickstart"; \
(cd $$building_block/php/$$variant && make validate) || echo "Validation failed for $$building_block/php/$$variant"; \
fi; \
done; \
done
@echo "PHP quickstart testing complete!"
118 changes: 118 additions & 0 deletions pub_sub/php/http/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
# Dapr pub/sub

In this quickstart, you'll create a publisher microservice and a subscriber microservice to demonstrate how Dapr enables a publish-subcribe pattern. The publisher will generate messages of a specific topic, while subscribers will listen for messages of specific topics. See [Why Pub-Sub](#why-pub-sub) to understand when this pattern might be a good choice for your software architecture.

Visit [this](https://docs.dapr.io/developing-applications/building-blocks/pubsub/) link for more information about Dapr and Pub-Sub.

> **Note:** This example leverages the Dapr HTTP API directly, without an SDK. If you are looking for the example using the [Dapr PHP SDK](https://docs.dapr.io/developing-applications/sdks/php/) [click here](../sdk).

This quickstart includes one publisher:

- PHP client message generator `checkout`

And one subscriber:

- PHP subscriber `order-processor`

## Run all apps with multi-app run template file:

This section shows how to run both applications at once using [multi-app run template files](https://docs.dapr.io/developing-applications/local-development/multi-app-dapr-run/multi-app-overview/) with `dapr run -f .`. This enables to you test the interactions between multiple applications.

1. Install dependencies:

<!-- STEP
name: Install PHP dependencies
-->

```bash
composer install
```
<!-- END_STEP -->

2. Open a new terminal window and run the multi app run template:


<!-- STEP
name: Run multi app run template
expected_stdout_lines:
- 'Validating config and starting app "order-processor-http"'
- 'Validating config and starting app "checkout-http"'
- 'Published data: {"orderId":1}'
- 'Subscriber received : 1'
expected_stderr_lines:
output_match_mode: substring
match_order: none
background: true
sleep: 15
timeout_seconds: 30
-->

```bash
dapr run -f .
```

The terminal console output should look similar to this:

```text
== APP - order-processor-http == Order processor listening on port 6021
== APP - checkout-http == Publishing to baseURL: http://localhost:3500, Pubsub Name: orderpubsub, Topic: orders
== APP - checkout-http == Published data: {"orderId":1}
== APP - order-processor-http == Dapr pub/sub is subscribed to: [{"pubsubname":"orderpubsub","topic":"orders","route":"orders"}]
== APP - order-processor-http == Subscriber received : 1
== APP - checkout-http == Published data: {"orderId":2}
== APP - order-processor-http == Subscriber received : 2
== APP - checkout-http == Published data: {"orderId":3}
== APP - order-processor-http == Subscriber received : 3
== APP - checkout-http == Published data: {"orderId":4}
== APP - order-processor-http == Subscriber received : 4
== APP - checkout-http == Published data: {"orderId":5}
== APP - order-processor-http == Subscriber received : 5
```

3. Stop and clean up application processes

```bash
dapr stop -f .
```
<!-- END_STEP -->

## Run a single app at a time with Dapr (Optional)

An alternative to running all or multiple applications at once is to run single apps one-at-a-time using multiple `dapr run .. -- php app.php` commands. This next section covers how to do this.

### Run PHP message subscriber with Dapr

1. Install dependencies:

```bash
composer install
```

2. Run the PHP subscriber app with Dapr:

```bash
cd ./order-processor
dapr run --app-id order-processor-http --resources-path ../../../components/ --app-port 6021 -- php app.php
```

### Run PHP message publisher with Dapr

1. Install dependencies:

```bash
composer install
```

2. Run the PHP publisher app with Dapr:

```bash
cd ./checkout
dapr run --app-id checkout-http --resources-path ../../../components/ -- php app.php
```

### Stop the apps and clean up

```bash
dapr stop --app-id checkout-http
dapr stop --app-id order-processor-http
```
38 changes: 38 additions & 0 deletions pub_sub/php/http/checkout/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env php
<?php

$base_url = (getenv('BASE_URL') ?: 'http://localhost') . ':' . (getenv('DAPR_HTTP_PORT') ?: '3500');
$pubsub_name = 'orderpubsub';
$topic = 'orders';

function http_request(string $method, string $url, ?string $body = null): int
{
$ch = curl_init($url);
curl_setopt_array($ch, [
CURLOPT_CUSTOMREQUEST => $method,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
CURLOPT_POSTFIELDS => $body,
]);
curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch);
return $status;
}

echo "Publishing to baseURL: $base_url, Pubsub Name: $pubsub_name, Topic: $topic" . PHP_EOL;

// Wait for the Dapr sidecar to become healthy before publishing
while (http_request('GET', "$base_url/v1.0/healthz") !== 204) {
sleep(1);
}

for ($i = 1; $i < 10; $i++) {
$order = ['orderId' => $i];

// Publish an event/message to Dapr via HTTP POST
http_request('POST', "$base_url/v1.0/publish/$pubsub_name/$topic", json_encode($order));

echo 'Published data: ' . json_encode($order) . PHP_EOL;
sleep(1);
}
9 changes: 9 additions & 0 deletions pub_sub/php/http/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "dapr-quickstarts/pub-sub-php-http",
"description": "Dapr pub/sub quickstart (PHP, plain HTTP).",
"type": "project",
"require": {
"php": "^8.4",
"react/http": "^1.11"
}
}
Loading