Skip to content

spajak/flow

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flow

Simple PHP HTTP 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 = 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->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

License

MIT

About

Simple PHP application base

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages