itsjavi/philae

RESTFul, modern and minimal framework for PHP, built on top of PSR-7 and PSR-15 components.

0.7.0 2017-05-27 07:38 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

RESTFul, modern and minimal framework for PHP, built on top of PSR-7 and PSR-15 components.

This project is compliant with PSR-1, PSR-2, PSR-4, PSR-7, PSR-11 and PSR-15. If you notice compliance oversights, please send a patch via pull request.

Philae and Rosetta

Goals

  • Simple: Keep the core source code as reduced as possible.
  • Flexible: Keep dependencies as minimum as possible.
  • Predictable: Be the glue of essential modern components, following standards and with proper documentation.
  • Powerful: Well tested and robust code, suitable for any scenario and environment.

Components

Install

Via Composer

$ composer require itsjavi/philae

Via Git

$ git clone https://github.com/itsjavi/philae.git

Create a project skeleton with philae-skeleton

$ composer create-project itsjavi/philae-skeleton myproject

Usage

<?php

use Middlewares\ErrorHandlerDefault;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

include __DIR__ . '/vendor/autoload.php';

// Initialize the app with the default dependencies and some essential middleware.
$app = new Philae\Application();
$app->addServiceProvider(new \Philae\DefaultServiceProvider());
$app->setMiddlewares(
    [
        // error handler
        (new Middlewares\ErrorHandler(ErrorHandlerDefault::class))->catchExceptions(true),
        // dispatch the request
        new Philae\Middlewares\RouteDispatcher(),
        // transform the handler response
        new Philae\Middlewares\ResponseTransformer(),
        // execute the request handler
        (new Philae\Middlewares\RequestHandler())->arguments($app->getResponse()),
    ]
);

// Define the routes
$router = $app->getRouter();

$router->get('/', function (ServerRequestInterface $req, ResponseInterface $res) {
    $res->getBody()->write('<h1>Hello, World!</h1>' . PHP_EOL);
    return $res;
});
$router->get('/{name}', function (ServerRequestInterface $req, ResponseInterface $res, array $params) {
    $res->getBody()->write('<h1>Hello, ' . $params['name'] . '!</h1>' . PHP_EOL);
    return $res;
});

$router->get('/foo', MyController::class); // Compatible also with callable objects implementing __invoke
$router->post('/foo/{id}', MyController::class . '::create'); // and with any other callable definition
$router->put('/foo/{id}', [MyController::class, 'edit']);

// Process the middlewares and send the response:
$app->execute();

Testing

$ composer test

or

$ vendor/bin/phpunit
$ vendor/bin/phpcs

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.