GithubHelp home page GithubHelp logo

jasonmccreary / adrenaline Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bitexpert/adrenaline

0.0 1.0 0.0 73 KB

A PSR-7 micro framework built on top of the Adroit middleware to speed up your development ;)

License: Apache License 2.0

PHP 100.00%

adrenaline's Introduction

bitexpert/adrenaline

A PSR-7 micro framework built on top of the Adroit middleware to speed up your development ;)

Build Status Dependency Status

Getting started

The preferred way of installing bitexpert/adrenaline is through Composer. Simply add bitexpert/adrenaline as a dependency:

composer.phar require bitexpert/adrenaline

Prototyping

If you want to use Adrenaline for fast prototyping you precede as follows:

  • Create an index.php in your application's root directory.
  • Add the following lines to it:
<?php
use bitExpert\Adrenaline\Adrenaline;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;

$adrenaline = new Adrenaline();

$adrenaline->get('home', '/', function (ServerRequestInterface $request, ResponseInterface $response) {
    $response->getBody()->rewind();
    $response->getBody()->write('Home');

    return $response;
});

$request = ServerRequestFactory::fromGlobals();
$response = new Response();
$adrenaline($request, $response);
  • Start a server by using php -S localhost:8082 inside the same directoy.
  • Browse to http://localhost:8082 and you should see "Home" on the screen.

So that's the basic setup. You may add more actions by using the implicit routing functions of Adrenaline and of course you may use middleware hooks, error handler and custom resolvers just as you would do in a productive application. This is just a possibility to give you a quick result for prototyping.

How to configure action resolvers

You may configure custom action resolvers for Adrenaline. By default bitexpert/adroit's CallableActionResolver is added if no action resolvers are provided with the constructor which allows you to use simple Closures as actions (e.g. for protoyping case)

Be aware that you have to add this resolver explicitly if you define your own set of action resolvers if you still want to use it.

<?php
$customActionResolver = new MyCustomActionResolver();
$adrenaline = new Adrenaline([$customActionResolver]);

How to configure responder resolvers

You may configure custom action resolvers for Adrenaline. By default bitexpert/adroit's CallableResponderResolver is added if no responder resolvers are provided with the constructor which allows you to use simple Closures as responders (e.g. for protoyping case)

Be aware that you have to add this resolver explicitly if you define your own set of responder resolvers if you still want to use it.

<?php
$customResponderResolver = new MyCustomResponderResolver();
$adrenaline = new Adrenaline([], [$customResponderResolver]);

How to configure routing

With Adrenaline you may also use custom routers. Your custom router needs to inherit bitexpert/pathfinder's Router interface.

<?php
$customRouter = new MyCustomRouter();
$adrenaline = new Adrenaline([], [], $customRouter);

If you want to use a custom route class within the implicit route creation functions (get, post, etc.) you can set the custom route route class:

<?php
$adrenaline = new Adrenaline();
$adrenaline->setDefaultRouteClass(MyRoute::class);

For standard route definition, please have a look at the pathfinder docs.

How to implement an action

See bitexpert/adroit docs.

How to implement a responder

See bitexpert/adroit docs.

How to use the middleware hooks

Adrenaline offers the possibility to integrate your middlewares into the default processing of a request by using the provided middleware hooks:

<?php
$myMiddleware = function (ServerRequestInterface $request, ResponseInterface $response, callable $next = null) {
};

$adrenaline->beforeRouting($myMiddleware) //Middleware piped before routing middleware
$adrenaline->beforeResolveAction($myMiddleware) //Middleware piped before action resolver middleware
$adrenaline->beforeExecuteAction($myMiddleware) //Middleware piped before action executor middleware
$adrenaline->beforeResolveResponder($myMiddleware) //Middleware piped before responder resolver middleware
$adrenaline->beforeExecuteResponder($myMiddleware) //Middleware piped before responder executor middleware
$adrenaline->beforeEmit($myMiddleware) //Middleware piped before emitter

These hooks are chainable and you may call them multiple times. Each call will push the provided middleware to the according stack. As middlewares you may either use a simple closures or use your own dedicated classes implementing the __invoke method with the according signature.

How to use an error handler

You also may define an error handler which will be used for uncaught errors occuring while Adrenaline processes the request. You may either use a simple closure or implement your own dedicated class for error handling:

<?php

// simple closure
$adrenaline->setErrorHandler(function (ServerRequestInterface $request, ResponseInterface $response, $err) {
    return $response->withStatus(500);
});

// class which implements __invoke with same signature as above
$adrenaline->setErrorHandler(new MyCustomErrorHandlerClass());

How to integrate with a DI container

If you want to use a DI container, you may use the according resolvers to make use of it:

<?php
/** @var \Interop\Container\ContainerInterface $container */
$actionResolver = new \bitExpert\Adroit\Action\Resolver\ContainerActionResolver($container);
/** @var \Interop\Container\ContainerInterface $container */
$responderResolver = new \bitExpert\Adroit\Responder\Resolver\ContainerAwareResponderResolver($container);

// Adrenaline will use your containers for resolving actions and responders
$adrenaline = new Adrenaline([$actionResolver], [$responderResolver]);

For further instructions you may also have a look at the bitexpert/adroit docs.

##License

Adrenaline is released under the Apache 2.0 license.

adrenaline's People

Contributors

dropdevcoding avatar shochdoerfer avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.