Skip to content

Repository files navigation

Flow

Simple PHP HTTP & CLI application base using:

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 includes and anonymous functions.

Usage

$app = newFlow\Application;

Register services (using: php-di/php-di):

$services = [];
$services['hello'] = fn() => newclass {
publicfunctionsayHello($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 middleware (using: northwoods/broker):

$app->getBroker()->append(newMyMiddleware);

…or defer construction until after bootstrap when the middleware needs services from the container:

$app->getBroker()->appendDeferred(fn() => newMyMiddleware(
$app->getContainer()->get('hello'),
));

Register console commands (using: symfony/console):

useSymfony\Component\Console\Input\InputInterface;
useSymfony\Component\Console\Output\OutputInterface;
$app->getConsole()->register('hello')
->setDescription('Say hello')
->addArgument('name', null, 'Your name', 'World')
->setCode(function(InputInterface$input, OutputInterface$output) use ($app) {
$service = $app->getContainer()->get('hello');
$output->writeLn($service->sayHello($input->getArgument('name')));
return0;
});

…or use factories to lazy-load commands:

useFlow\Command\RequestCommand;
useFlow\Emitter\ConsoleEmitter;
$commands = [];
$commands['request'] = fn() => newRequestCommand(
$app->getServerRequestCreator(),
$app->getBroker(),
newConsoleEmitter
);
$app->getCommandLoader()->addFactories($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

Lifecycle

Append all middleware to the broker before calling $app->run(). The router middleware is appended last during bootstrap and terminates the pipeline on a matched route, so anything appended afterwards is unreachable for matched routes.

Tests & static analysis

$ composer test# phpunit
$ composer analyse # phpstan

License

MIT

About

Simple PHP application base

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages