From eb503cdc01047137e06b8a73fc05b7ce184a7fc3 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:42:54 -0400 Subject: [PATCH 1/3] feat: updating readme --- README.md | 66 ++++++++++++++++++++++++++++++++---------- example/composer.json | 4 +-- example/src/server.php | 22 +++++++++++++- 3 files changed, 73 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 137e384c..578c62fc 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ 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). -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. +Utopia HTTP is *almost* 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. ## Getting Started @@ -20,30 +20,54 @@ composer require utopia-php/http Init your first application in `src/server.php`: + ```php require_once __DIR__.'/../vendor/autoload.php'; +use Utopia\DI\Container; +use Utopia\DI\Dependency; use Utopia\Http\Http; use Utopia\Http\Request; use Utopia\Http\Response; use Utopia\Http\Adapter\FPM\Server; -Http::get('/hello-world') // Define Route - ->inject('request') - ->inject('response') +class User { + public string $name; + + public function __construct(string $name) + { + $this->name = $name; + } +} +// Creating the dependency injection container +$container = new Container(); + +// Adding a user dependency to the container +$user = new Dependency(); +$user + ->setName('user') + ->setCallback(fn () => new User('John Doe')); + +$container->add($user); + +// Defining Route +Http::get('/hello-world') + ->inject('request') // Auto-injected each request + ->inject('response') // Auto-injected each request + ->inject('user') ->action( - function(Request $request, Response $response) { + function(Request $request, Response $response, User $user) { $response ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Expires', '0') ->addHeader('Pragma', 'no-cache') - ->json(['Hello' => 'World']); + ->json(['Hello' => 'World', 'User is' => $user->name]); } ); Http::setMode(Http::MODE_TYPE_PRODUCTION); -$http = new Http(new Server(), 'America/New_York'); +$http = new Http(new Server(), $container, 'America/New_York'); $http->start(); ``` @@ -66,6 +90,7 @@ The library supports server adapters to be able to run on any PHP setup. You cou #### Use PHP FPM server ```php +use Utopia\DI\Container; use Utopia\Http\Http; use Utopia\Http\Response; use Utopia\Http\Adapter\FPM\Server; @@ -78,7 +103,7 @@ Http::get('/') } ); -$http = new Http(new Server(), 'America/New_York'); +$http = new Http(new Server(), new Container() 'America/New_York'); $http->start(); ``` @@ -87,6 +112,7 @@ $http->start(); #### Using Swoole server ```php +use Utopia\DI\Container; use Utopia\Http\Http; use Utopia\Http\Request; use Utopia\Http\Response; @@ -101,13 +127,13 @@ Http::get('/') } ); -$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York'); +$http = new Http(new Server('0.0.0.0', '80' , ['open_http2_protocol' => true]), new Container(), 'America/New_York'); $http->start(); ``` > When using Swoole, you can use the command `php src/server.php` to run the HTTP server locally, but you need Swoole installed. For setup with Docker, check out our [example application](/example) -### Parameters +### Parameters Parameters are used to receive input into endpoint action from the HTTP request. Parameters could be defined as URL parameters or in a body with a structure such as JSON. @@ -206,16 +232,24 @@ Http::init() Groups are designed to be actions that run during the lifecycle of requests to endpoints that have some logic in common. Groups allow you to prevent code duplication and are designed to be defined anywhere in your source code to allow flexibility. -### Resources +### Injections -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. +Injections 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. -Define a resource: +We define an injection using a Container: ```php -Http::setResource('timing', function() { - return \microtime(true); -}); +use Utopia\DI\Container; +use Utopia\DI\Dependency; + +$container = new Container(); + +$timing = new Dependency(); +$timing + ->setName('timing') + ->setCallback(fn () => \microtime(true)); + +$container->add($timing); ``` Inject resource into endpoint action: diff --git a/example/composer.json b/example/composer.json index 383d90aa..2bcb8607 100644 --- a/example/composer.json +++ b/example/composer.json @@ -1,6 +1,6 @@ { "name": "utopia-php/http-app", "require": { - "utopia-php/http": "latest" + "utopia-php/http": "1.0.*" } -} \ No newline at end of file +} diff --git a/example/src/server.php b/example/src/server.php index 4eb07f71..ad55b973 100644 --- a/example/src/server.php +++ b/example/src/server.php @@ -2,11 +2,24 @@ require_once __DIR__.'/../vendor/autoload.php'; +use Utopia\DI\Container; +use Utopia\DI\Dependency; use Utopia\Http\Http; use Utopia\Http\Response; use Utopia\Http\Adapter\Swoole\Server; use Utopia\Http\Validator\Text; + +class User { + function __construct(public $name) {} +} + +$container = new Container(); + +$user = new Dependency(); +$user->setName('user')->setCallback(fn() => new User("Demo user")); +$container->set($user); + Http::get('/') ->param('name', 'World', new Text(256), 'Name to greet. Optional, max length 256.', true) ->inject('response') @@ -14,5 +27,12 @@ $response->send('Hello ' . $name); }); -$http = new Http(new Server('0.0.0.0', '80'), 'America/New_York'); +Http::get('/user') + ->inject('response') + ->inject('user') + ->action(function (Response $response, User $user) { + $response->send('Hello ' . $user->name); + }); + +$http = new Http(new Server('0.0.0.0', '80'), $container,'America/New_York'); $http->start(); From 17d87f26cfa129362c7e0bbc90a82ec5aa79b89a Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:44:39 -0400 Subject: [PATCH 2/3] chore: formatting --- example/src/server.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/example/src/server.php b/example/src/server.php index ad55b973..0697870a 100644 --- a/example/src/server.php +++ b/example/src/server.php @@ -9,15 +9,17 @@ use Utopia\Http\Adapter\Swoole\Server; use Utopia\Http\Validator\Text; - -class User { - function __construct(public $name) {} +class User +{ + public function __construct(public $name) + { + } } $container = new Container(); $user = new Dependency(); -$user->setName('user')->setCallback(fn() => new User("Demo user")); +$user->setName('user')->setCallback(fn () => new User("Demo user")); $container->set($user); Http::get('/') @@ -34,5 +36,5 @@ function __construct(public $name) {} $response->send('Hello ' . $user->name); }); -$http = new Http(new Server('0.0.0.0', '80'), $container,'America/New_York'); +$http = new Http(new Server('0.0.0.0', '80'), $container, 'America/New_York'); $http->start(); From 5669f39f833876e0b9fdbc7c344cfbeb2745c910 Mon Sep 17 00:00:00 2001 From: Binyamin Yawitz <316103+byawitz@users.noreply.github.com> Date: Fri, 6 Sep 2024 09:59:31 -0400 Subject: [PATCH 3/3] fix: reviews --- README.md | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 578c62fc..cfb3c250 100644 --- a/README.md +++ b/README.md @@ -31,14 +31,6 @@ use Utopia\Http\Request; use Utopia\Http\Response; use Utopia\Http\Adapter\FPM\Server; -class User { - public string $name; - - public function __construct(string $name) - { - $this->name = $name; - } -} // Creating the dependency injection container $container = new Container(); @@ -46,7 +38,8 @@ $container = new Container(); $user = new Dependency(); $user ->setName('user') - ->setCallback(fn () => new User('John Doe')); + ->inject('request') // We can insert and use other injections as well + ->setCallback(fn (Request $request) => $request->getHeader('x-user-id', 'John Doe')); $container->add($user); @@ -56,12 +49,12 @@ Http::get('/hello-world') ->inject('response') // Auto-injected each request ->inject('user') ->action( - function(Request $request, Response $response, User $user) { + function(Request $request, Response $response, string $user) { $response ->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate') ->addHeader('Expires', '0') ->addHeader('Pragma', 'no-cache') - ->json(['Hello' => 'World', 'User is' => $user->name]); + ->json(['message' => 'Hello World', 'user' => $user]); } );