Skip to content

Commit 15d1195

Browse files
ChiragAgg5kloks0n
andauthored
Move resource ownership into utopia-php/di (#220)
* Use utopia-php/di for resource injection * Move resource ownership into utopia-php/di * Update DI branch dependency * update getting started * update * update * update appwrite base version * update to use php 8.2 * fix: restore php 8.2 test runtime * chore: use container scopes * remove utopia keyword * remove optional container in run * remove optional container in run * renaming * remove public getContainer * fix getcontainer * fix getcontainer * update * remove tests * make public * remove tests * add scoped request containers * cleanup * feat: request scopes * fixes --------- Co-authored-by: loks0n <22452787+loks0n@users.noreply.github.com>
1 parent d60c9ce commit 15d1195

File tree

18 files changed

+388
-409
lines changed

18 files changed

+388
-409
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
- name: Setup PHP
1414
uses: shivammathur/setup-php@v2
1515
with:
16-
php-version: '8.1'
16+
php-version: '8.2'
1717

1818
- name: Validate composer.json and composer.lock
1919
run: composer validate --strict

Dockerfile.fpm

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ RUN composer install --ignore-platform-reqs --optimize-autoloader \
1313
--no-plugins --no-scripts --prefer-dist \
1414
`if [ "$TESTING" != "true" ]; then echo "--no-dev"; fi`
1515

16-
FROM php:8.0-cli-alpine as final
16+
FROM php:8.2-fpm-alpine AS final
1717
LABEL maintainer="team@appwrite.io"
1818

1919
ENV DEBIAN_FRONTEND=noninteractive \
20-
PHP_VERSION=8
20+
PHP_FPM_POOL_CONF=/usr/local/etc/php-fpm.d/www.conf
2121

2222
RUN \
2323
apk add --no-cache --virtual .deps \
24-
supervisor php$PHP_VERSION php$PHP_VERSION-fpm php$PHP_VERSION-mbstring nginx bash
24+
supervisor nginx bash
2525

2626

2727
# Nginx Configuration (with self-signed ssl certificates)
2828
COPY ./tests/docker/nginx.conf /etc/nginx/nginx.conf
2929

3030
# PHP Configuration
3131
RUN mkdir -p /var/run/php
32-
COPY ./tests/docker/www.conf /etc/php/$PHP_VERSION/fpm/pool.d/www.conf
32+
COPY ./tests/docker/www.conf /usr/local/etc/php-fpm.d/www.conf
3333

3434
# Script
3535
COPY ./tests/docker/start /usr/local/bin/start

Dockerfile.swoole

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ RUN composer install --ignore-platform-reqs --optimize-autoloader \
1313
--no-plugins --no-scripts --prefer-dist \
1414
`if [ "$TESTING" != "true" ]; then echo "--no-dev"; fi`
1515

16-
FROM appwrite/base:0.4.3 as final
16+
FROM appwrite/base:0.5.0 AS final
1717
LABEL maintainer="team@appwrite.io"
1818

1919
WORKDIR /usr/src/code

README.md

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
Utopia HTTP is a PHP MVC based framework with minimal must-have features for professional, simple, advanced and secure web development. This library is maintained by the [Appwrite team](https://appwrite.io).
1010

11-
Utopia HTTP is dependency-free. Any extra features, such as authentication or caching are available as standalone models in order to keep the framework core clean, light, and easy to learn.
11+
Utopia HTTP keeps routing and request lifecycle concerns separate from resource wiring by relying on the standalone Utopia DI package for dependency injection.
1212

1313
## Getting Started
1414

@@ -23,11 +23,14 @@ Init your first application in `src/server.php`:
2323
```php
2424
require_once __DIR__.'/../vendor/autoload.php';
2525

26+
use Utopia\DI\Container;
2627
use Utopia\Http\Http;
2728
use Utopia\Http\Request;
2829
use Utopia\Http\Response;
2930
use Utopia\Http\Adapter\FPM\Server;
3031

32+
$container = new Container();
33+
3134
Http::get('/hello-world') // Define Route
3235
->inject('request')
3336
->inject('response')
@@ -43,7 +46,7 @@ Http::get('/hello-world') // Define Route
4346

4447
Http::setMode(Http::MODE_TYPE_PRODUCTION);
4548

46-
$http = new Http(new Server(), 'America/New_York');
49+
$http = new Http(new Server(), 'America/New_York', $container);
4750
$http->start();
4851
```
4952

@@ -66,10 +69,13 @@ The library supports server adapters to be able to run on any PHP setup. You cou
6669
#### Use PHP FPM server
6770

6871
```php
72+
use Utopia\DI\Container;
6973
use Utopia\Http\Http;
7074
use Utopia\Http\Response;
7175
use Utopia\Http\Adapter\FPM\Server;
7276

77+
$container = new Container();
78+
7379
Http::get('/')
7480
->inject('response')
7581
->action(
@@ -78,7 +84,7 @@ Http::get('/')
7884
}
7985
);
8086

81-
$http = new Http(new Server(), 'America/New_York');
87+
$http = new Http(new Server(), 'America/New_York', $container);
8288
$http->start();
8389
```
8490

@@ -87,11 +93,14 @@ $http->start();
8793
#### Using Swoole server
8894

8995
```php
96+
use Utopia\DI\Container;
9097
use Utopia\Http\Http;
9198
use Utopia\Http\Request;
9299
use Utopia\Http\Response;
93100
use Utopia\Http\Adapter\Swoole\Server;
94101

102+
$container = new Container();
103+
95104
Http::get('/')
96105
->inject('request')
97106
->inject('response')
@@ -101,7 +110,7 @@ Http::get('/')
101110
}
102111
);
103112

104-
$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York');
113+
$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York', $container);
105114
$http->start();
106115
```
107116

@@ -208,35 +217,37 @@ Groups are designed to be actions that run during the lifecycle of requests to e
208217

209218
### Resources
210219

211-
Resources allow you to prepare dependencies for requests such as database connection or the user who sent the request. A new instance of a resource is created for every request.
220+
Resources allow you to prepare dependencies for requests such as database connections or shared services. Register application dependencies on the DI container with `set()`. Runtime values such as `request`, `response`, `route`, `error`, and `context` are scoped by `Http` for each request.
212221

213-
Define a resource:
222+
Define a dependency on the DI container:
214223

215224
```php
216-
Http::setResource('timing', function() {
225+
$container->set('bootTime', function () {
217226
return \microtime(true);
218227
});
219228
```
220229

221230
Inject resource into endpoint action:
222231

223232
```php
233+
$http = new Http(new Server(), 'America/New_York', $container);
234+
224235
Http::get('/')
225-
->inject('timing')
236+
->inject('bootTime')
226237
->inject('response')
227-
->action(function(float $timing, Response $response) {
228-
$response->send('Request Unix timestamp: ' . \strval($timing));
238+
->action(function(float $bootTime, Response $response) {
239+
$response->send('Process started at: ' . \strval($bootTime));
229240
});
230241
```
231242

232243
Inject resource into a hook:
233244

234245
```php
235246
Http::shutdown()
236-
->inject('timing')
237-
->action(function(float $timing) {
238-
$difference = \microtime(true) - $timing;
239-
\var_dump("Request took: " . $difference . " seconds");
247+
->inject('bootTime')
248+
->action(function(float $bootTime) {
249+
$uptime = \microtime(true) - $bootTime;
250+
\var_dump("Process uptime: " . $uptime . " seconds");
240251
});
241252
```
242253

@@ -248,7 +259,7 @@ To learn more about architecture and features for this library, check out more i
248259

249260
## System Requirements
250261

251-
Utopia HTTP requires PHP 8.1 or later. We recommend using the latest PHP version whenever possible.
262+
Utopia HTTP requires PHP 8.2 or later. We recommend using the latest PHP version whenever possible.
252263

253264
## More from Utopia
254265

composer.json

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,23 @@
2929
"bench": "vendor/bin/phpbench run --report=benchmark"
3030
},
3131
"require": {
32-
"php": ">=8.0",
33-
"ext-swoole": "*",
34-
"utopia-php/validators": "0.2.*"
32+
"php": ">=8.2",
33+
"utopia-php/di": "0.3.*",
34+
"utopia-php/validators": "0.2.*",
35+
"ext-swoole": "*"
36+
},
37+
"config": {
38+
"allow-plugins": {
39+
"php-http/discovery": true,
40+
"tbachert/spi": true
41+
}
3542
},
3643
"require-dev": {
37-
"phpunit/phpunit": "^9.5.25",
44+
"doctrine/instantiator": "^1.5",
3845
"laravel/pint": "1.*",
39-
"swoole/ide-helper": "4.8.3",
46+
"phpbench/phpbench": "^1.2",
4047
"phpstan/phpstan": "1.*",
41-
"phpbench/phpbench": "^1.2"
48+
"phpunit/phpunit": "^9.5.25",
49+
"swoole/ide-helper": "4.8.3"
4250
}
4351
}

0 commit comments

Comments
 (0)