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

on:
pull_request:
push:
branches:
- main

jobs:
tests:
name: Lint and test
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4

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

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

- name: Lint
run: |
php -l global.inc.php
php -l playground.php
for file in tests/*.php; do php -l "$file"; done

- name: Test
run: composer test
6 changes: 3 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
FROM composer AS composer

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 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"]
56 changes: 51 additions & 5 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 newer installed (required by `appwrite/appwrite` 32.x).
* 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,59 @@ 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
* Request Password Recovery
* Log a User In From a Token Secret
* Create Function, Deployment and Executions

Collections and documents were renamed to tables and rows in Appwrite 1.8. The
playground uses the `TablesDB` service; the older `Databases` service still
exists in the SDK, but every one of its methods is deprecated.

### Abuse limits

The account and recovery routes are abuse limited. Reaching a limit answers
`429`, which the SDK raises as an `AppwriteException` whose code is `429` and
whose type is `general_rate_limit_exceeded`. Since
[appwrite/appwrite#15280](https://github.com/appwrite/appwrite/pull/15280) the
count is kept per project: the attempts your project is allowed for a user or an
address are its own, and another project cannot spend them for you.
`createRecovery()` in `playground.php` shows how to handle the refusal.

An API key used to be waived from those limits. Since
[appwrite/appwrite#15470](https://github.com/appwrite/appwrite/pull/15470) the
routes that confirm a secret — `updateRecovery()`, `updateRecoveryOTP()`,
`createSession()`, `updatePhoneSession()`, `updateMagicURLSession()` and
`updateMFAChallenge()` — count every caller's guesses, a key holder included. A
key is trusted with throughput, not with the inbox or handset the secret was
sent to.

So a server relaying logins or recoveries for its users has to treat the
confirmation as something it gets a handful of tries at per user:

* `401` means the secret is wrong or has expired — ask the user for a new one.
* `429` means that user's attempts are spent — wait, do not guess again.
Once a limit is spent the route refuses the correct secret too, so retrying a
mistyped code locks the user out of signing in for the rest of the window.

`createSessionFromToken()` in `playground.php` shows the shape of that handling.

## Tests

`composer test` runs the playground end to end against a stub Appwrite
(`tests/stub-server.php`) served by PHP's built-in web server, checking that
every call succeeded and hit the endpoint it was supposed to
(`tests/smoke.php`), and then drives the abuse-limited confirmation routes with
an API key to check that wrong guesses are counted for it, that the count is per
user, and that the `429` reaches the caller in a shape it can act on
(`tests/code-guesses.php`). No Appwrite instance and no network access are
needed.

## 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/code-guesses.php"
]
},
"support": {
"email": "team@appwrite.io",
"issues": "https://github.com/appwrite/playground-for-php/issues",
Expand Down
4 changes: 1 addition & 3 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
version: '3.9'

services:
playground-for-php:
build: .
volumes:
- "./:/app"
working_dir: /app
command: "php playground.php"
command: "php playground.php"
Loading
Loading