spajak / flow
Simple PHP application base
dev-master
2021-03-13 10:27 UTC
Requires
- php: >=7.3
- http-interop/response-sender: ^1.0
- nikic/fast-route: ^1.3
- northwoods/broker: ^3.0
- northwoods/conditional-middleware: ^1.0
- nyholm/psr7: ^1.4
- nyholm/psr7-server: ^1.0
- php-di/invoker: ^2.3
- php-di/php-di: ^6.3
- psr/log: ~1.0
- symfony/console: ^5.2
This package is auto-updated.
Last update: 2022-05-13 13:04:10 UTC
README
Simple PHP HTTP application base using:
- PSR-7 HTTP Message / PSR-17 HTTP Factories: nyholm/psr7
- PSR-15 HTTP Handlers / Middleware: northwoods/broker
- PSR-11: Container: php-di/php-di
- Routing: nikic/fast-route
- Console: symfony/console
Rationale
- Basing on standardized interfaces and well tested components;
- Not being tied to any specific framework;
- Being able to make lightweight and customizable apps fast with just PHP's
include
s and anonymous functions;
Usage
$app = new Flow\Application;
Register services (using: php-di/php-di):
$services = []; $services['hello'] = function() { return new class { public function sayHello($name) { return "Hello {$name}!"; } }; }; $app->getContainerBuilder()->addDefinitions($services);
Register routes (using: nikic/fast-route):
$app->getRouteCollector()->get('/hello[/{name}]', function($request, $name = 'World') use ($app) { $container = $app->getContainer(); $response = $container->get('http_factory')->createResponse(200); $response->getBody()->write( $container->get('hello')->sayHello($name) ); return $response; });
Register console commands (using: symfony/console):
$app->getConsole()->register('hello') ->setDescription('Say hello') ->addArgument('name', null, 'Your name', 'World') ->setCode(function($input, $output) use ($app) { $service = $app->getContainer()->get('hello'); $output->writeLn($service->sayHello($input->getArgument('name'))); });
…or use factories to lazy-load commands:
use Flow\Command\RequestCommand; use Flow\Emitter\ConsoleEmitter; $commands = []; $commands['request'] = function() use ($app) { return new RequestCommand( $app->getServerRequestCreator(), $app->getBroker(), new ConsoleEmitter ); } $app->setConsoleCommandsLoader($commands);
At the end of the script, simply run the Application:
$app->run();
Try it from terminal:
$ php examples/application.php hello "Grim Reaper"
$ php examples/application.php request GET /hello
TODO
- Some kind of helper to simplify creation of the responses.
- Add logging capabilities.
License
MIT