Hawkbit
PSR-7 Micro PHP framework - advanced derivate of Proton by Alex Bilbie
Install / Use
/learn @HawkBitPhp/HawkbitREADME
Hawkbit\Application
[![Latest Version on Packagist][ico-version]][link-packagist] ![Software License][ico-license] [![Build Status][ico-travis]][link-travis] [![Total Downloads][ico-downloads]][link-downloads] [![Coverage Status][ico-coveralls]][link-coveralls]
Hawkbit\Application micro framework is a high customizable, event driven and compatible with PSR-7, StackPHP and Zend Stratigility.
Hawkbit\Application uses latest versions of League\Route for routing, League\Container for dependency injection, League\Event for event dispatching, Zend Config for configuration.
Hawkbit\Application is an advanced derivate of Proton and part of Hawkbit\Application Component collection by Marco Bunge. Hawkbit\Application 1.x is also known as Blast Hawkbit\Application.
Quick start
Please see public/ for example usage and read documentation.
Integrations
Hawkbit\Application delivers also optional packages:
- Database:
hawkbit/database - Doctrine ORM Wrapper:
hawkbit/doctrine - Plates View Engine:
hawkbit/presentation
Motivation
My vision is to provide a micro framework which is able to handle HTTP and CLI in the same fashion. The developer should be able to reuse it's code, design it's business layer by his needs. Hawkbit should be a supporting tool instead of predefined framework. And yes it is under active development.
I love PSR, phpleague und a minimal set of dependecies and want to create a micro framework which is used the best packages out there and bundeld in a nice application layer. I'm also love the style of component-based development.
Hawkbit is built on top phpleague packages and keep PSR in mind. Hawkbit is designed to co-exist whith your code instead of replace code base. Hawkbit does has a small dependency footprint. Last but not least Hawkbit does not force the developer how to design the application bussiness logic, since we prefer to use POPO's for Controllers / Commands (the accessor to bussiness logic).
At the moment I design and develop all Hawkbit packages and manage the whole codebase. I would be appreciate for support or even better: for contributors!
Please refer to Issue #33 for details.
Special thanks
Thank you for PR, identifing Bus, or any other Improvements!
Install
Using Composer
Hawkbit\Application is available on Packagist and can be installed using Composer. This can be done by running the following command or by updating your composer.json file.
composer require hawkbit/hawkbit
composer.json
{
"require": {
"hawkbit/hawkbit": "~2.0"
}
}
Be sure to also include your Composer autoload file in your project:
<?php
require __DIR__ . '/vendor/autoload.php';
Downloading .zip file
This project is also available for download as a .zip file on GitHub. Visit the releases page, select the version you want, and click the "Source code (zip)" download button.
Requirements
The following versions of PHP are supported by this version.
- PHP 5.5
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
Setup
Create a new app
<?php
require __DIR__.'/../vendor/autoload.php';
$app = new \Hawkbit\Application();
Create a new app with configuration
<?php
$config = [
'key' => 'value'
];
$app = new \Hawkbit\Application($config);
Add routes
<?php
/** @var Hawkbit\Application $app */
$app->get('/', function ($request, $response) {
$response->getBody()->write('<h1>It works!</h1>');
return $response;
});
$app->get('/hello/{name}', function ($request, $response, $args) {
$response->getBody()->write(
sprintf('<h1>Hello, %s!</h1>', $args['name'])
);
return $response;
});
Run application
<?php
$app->run();
See also our example at /public/index.php.
Configuration
Add additional configuration to application
Hawkbit\Application Configuration is managed by zend-config.
<?php
//add many values
$app->setConfig([
'database' => [
'default' => 'mysql://root:root@localhost/acmedb',
],
'services' => [
'Acme\Services\ViewProvider',
]
]);
//add a single value
$app->setConfig('baseurl', 'localhost/');
$app->getConfig()->baseurl = 'localhost/';
$app->getConfig()['baseurl'] = 'localhost/';
Access configuration
<?php
//access all configuration
$app->getConfig();
//get configuration item
$default = $app->getConfig('database')->default; // returns 'mysql://root:root@localhost/acmedb
$default = $app->getConfig()->database->default; // returns 'mysql://root:root@localhost/acmedb
$default = $app->getConfig('database')['default']; // returns 'mysql://root:root@localhost/acmedb
$default = $app->getConfig()['database']['default']; // returns 'mysql://root:root@localhost/acmedb
Middlewares
Hawkbit\Application middlewares allows advanced control of lifecycle execution.
<?php
$app->addMiddleware(new Acme\SomeMiddleware);
Hawkbit\Application uses it's own runner Hawkbit\Application\MiddelwareRunner
Routing
Hawkbit\Application uses routing integration of league/route and allows access to route collection methods directly.
Basic usage with anonymous functions:
<?php
// index.php
$app->get('/', function ($request, $response) {
$response->getBody()->write('<h1>It works!</h1>');
return $response;
});
$app->get('/hello/{name}', function ($request, $response, $args) {
$response->getBody()->write(
sprintf('<h1>Hello, %s!</h1>', $args['name'])
);
return $response;
});
$app->run();
Access app from anonymous function
Hawkbit\Application allows to access itself from anonymous function through closure binding.
<?php
$app->get('/hello/{name}', function ($request, $response, $args) {
// access Hawkbit\Application
$app = $this;
$response->getBody()->write(
sprintf('<h1>Hello, %s!</h1>', $args['name'])
);
return $response;
});
Basic usage with controllers:
<?php
require __DIR__.'/../vendor/autoload.php';
$app = new Hawkbit\Application();
$app->get('/', 'HomeController::index'); // calls index method on HomeController class
$app->run();
<?php
// HomeController.php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class HomeController
{
public function index(ServerRequestInterface $request, ResponseInterface $response, array $args)
{
$response->getBody()->write('<h1>It works!</h1>');
return $response;
}
}
Automatic constructor injection of controllers:
<?php
// index.php
require __DIR__.'/../vendor/autoload.php';
$app = new Hawkbit\Application();
$app->getContainer()->add('CustomService', new CustomService);
$app->get('/', 'HomeController::index'); // calls index method on HomeController class
$app->run();
Please use boot method in Service Providers for correct injection of services into controller!
<?php
// HomeController.php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
class HomeController
{
/**
* @var CustomService
*/
private $service;
/**
* @param CustomService $application
*/
public function __construct(CustomService $service = null)
{
$this->service = $service;
}
/**
* @return CustomService
*/
public function getService()
{
return $this->service;
}
public function index(ServerRequestInterface $request, ResponseInterface $response, array $args)
{
//do somehing with service
$service = $this->getService();
return $response;
}
}
For more information about routes read this guide
Route groups
Hawkbit\Application add support for route groups.
<?php
$app->group('/admin', function (\League\Route\RouteGroup $route) {
//access app container (or any other method!)
$app = $this;
$route->map('GET', '/acme/route1', 'AcmeController::actionOne');
$route->map('GET', '/acme/route2', 'AcmeController::actionTwo');
$route->map('GET', '/acme/route3', 'AcmeController::actionThree');
});
Available vars
$route-\League\Route\RouteGroup$this-\Hawkbit\Application
Middleware integrations
StackPHP
Basic usage with StackPHP (using Stack\Builder and Stack\Run):
<?php
// index.php
require __DIR__.'/../vendor/autoload.php';
$app = new Hawkbit\Application();
$app->get('/', function ($request, $response) {
$response->setContent('<h1>Hello World</h1>');
return $response;
});
$httpKernel = new Hawkbit\Application\Symfony\HttpKernelAdapter($app);
$stack = (new \Stack\Builder())
->push('Some/MiddleWare') // This will execute first
->push('Some/MiddleWare') // This will execute second
->push('Some/MiddleWare'); // This will execute third
$app = $stack->resolve($httpKernel);
\Stack\run($httpKernel); // The app will run after all the middlewares have run
Zend Stratigility
Basic usage with Stratigility (using `Zend\Stratigil
