Skip to content
Closed
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
31 changes: 31 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Tests

on:
push:
branches:
- main
pull_request:

jobs:
tests:
name: Playground against a stub Appwrite
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: '8.5'
extensions: curl, json
tools: composer:v2
coverage: none

- name: Install dependencies
run: composer install --no-interaction --no-progress

- name: Lint
run: find . -path ./vendor -prune -o -name '*.php' -print0 | xargs -0 -n1 php -l

- name: Run the tests
run: composer test
16 changes: 11 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
FROM composer AS composer
# appwrite/appwrite 32.x needs PHP 8.5, so both stages run on it. The composer
# image is not used as a base for the same reason: it ships its own PHP.
FROM php:8.5-cli-alpine AS vendor

COPY --from=composer/composer:2-bin /composer /usr/local/bin/composer

WORKDIR /app

COPY composer.json composer.json
COPY composer.lock composer.lock

RUN composer install --no-dev --no-interaction --no-progress --no-scripts --optimize-autoloader

FROM php:8.1.11-alpine3.16
FROM php:8.5-cli-alpine

COPY --from=composer /app/vendor /app/vendor
COPY --from=vendor /app/vendor /app/vendor
COPY global.inc.php /app/global.inc.php
COPY playground.php /app/playground.php
COPY test.txt /app/test.txt
COPY resources /app/resources

WORKDIR /app
CMD php playground.php
CMD ["php", "playground.php"]
47 changes: 41 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Appwrite playground is a simple way to explore the Appwrite API & Appwrite PHP S
This playground doesn't include any PHP best practices but rather intended to show some of the most simple examples and use cases of using the Appwrite API in your PHP application and server.

## System Requirements
* A system with PHP installed.
* A system with PHP 8.5 or later installed — that is what `appwrite/appwrite` 32.x requires.
* You have readily available Appwrite running instance (localhost in most cases).
* Create a project in Appwrite instance using console.
* Generate a secret key in the Appwrite instance using console.
Expand All @@ -25,13 +25,48 @@ This playground doesn't include any PHP best practices but rather intended to sh
6. You will see the JSON response in the console.

### API Covered in Playground.
* Create Collection
* List Collection
* Add Document
* List Documents
* Create Table
* List Tables
* Add Row
* Upload File
* List Files
* Create User
* List User
* List Users
* Mark a user verified
* Confirm an email verification code
* Create, deploy and execute a Function

Tables, rows and columns are what Appwrite 1.8 renamed collections, documents
and attributes to. The playground uses the `TablesDB` service; the `Databases`
service still answers to the old names but is deprecated.

### Verifying a user from a server

Two calls can end up with a verified user, and they are not interchangeable:

* `markUserVerified()` uses `users->updateEmailVerification()`. It needs
`users.write`, takes no code, and is the right call when the server itself is
the one deciding a user is verified.
* `confirmEmailVerification()` replays the six digit code Appwrite sent the
user. Appwrite counts every attempt against that route — including attempts
made with an API key — at 10 per user per hour, so a server that retries after
a rejected code spends the budget and then gets
`429 general_rate_limit_exceeded` for the rest of the hour, the user's own
correct code included. Treat a rejected code as final and issue a fresh one.

Set `VERIFICATION_USER_ID` and `VERIFICATION_SECRET` in `global.inc.php` to run
`confirmEmailVerification()`; it is skipped while they are empty.

### Tests

`composer test` runs the playground against a stub Appwrite served by PHP's
built-in web server, so it needs no running instance:

* `tests/smoke.php` — every playground call succeeds and hits the endpoint it
means to.
* `tests/verification.php` — the verification confirmation routes count every
caller, and the playground reports a rate limited confirmation instead of
failing the run.

## Contributing

Expand Down
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,21 @@
}
],
"require": {
"php": ">=8.0",
"php": ">=8.5",
"ext-curl": "*",
"appwrite/appwrite": "^12.0.0"
"ext-json": "*",
"appwrite/appwrite": "^32.2.0"
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist"
},
"scripts": {
"test": [
"@php tests/smoke.php",
"@php tests/verification.php"
]
},
"support": {
"email": "team@appwrite.io",
"issues": "https://github.com/appwrite/playground-for-php/issues",
Expand Down
12 changes: 12 additions & 0 deletions global.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@
\define('API_KEY', '<Your Project Secret Api key />');
}

// A user id and the verification code that user received, used by
// `confirmEmailVerification()` in `playground.php`. Both empty skips that call:
// Appwrite counts every attempt against the route — an API key included — at 10
// per user per hour, so there is nothing to be gained from guessing a code.
if (!\defined('VERIFICATION_USER_ID')) {
\define('VERIFICATION_USER_ID', '');
}

if (!\defined('VERIFICATION_SECRET')) {
\define('VERIFICATION_SECRET', '');
}

// Helper functions

/**
Expand Down
Loading
Loading