Giter Site home page Giter Site logo

backbeard's Introduction

Backbeard

Backbeard is yet another DSLish minimum oriented framework for PHP.

Build Status Coverage Status

backbeard

Principle

yield $router($request) => $action();

Usage

<?php
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Backbeard\Dispatcher;
use Backbeard\ValidationError;

$routingFactory = function ($serviceLocator) {
    yield '/hello' => 'hello';

    $error = (yield ['POST' => '/entry/{id:[0-9]}'] => function ($id) {
        if ($this->getRequest()->getPost()['NAME'] == 'wtf') {
            return ['var1' => 'baz']; // will be render entry.phtml
        } else {
            return new ValidationError(['error message!']);
        }
    });

    yield '/entry/{id:[0-9]}' => function ($id) use ($error) {
        $message = $error ? htmlspecialchars(current($error->getMessages())) :'';
        return "Hello $id ".$message.
        '<form method="POST" action="/entry/'.$id.'">'.
            'NAME<input type="text" name="NAME">'.
        '</form>';
    };

    yield [
      'GET' => '/foo',
      'Header' => [
        'User-Agent' => function($headers){
          if (!empty($headers) && strpos(current($headers), 'Coffee') === 0) {
            return true;
          }
        }
      ]
    ] => 418; // status code "I'm a teapot"

    yield (ServerRequestInterface $request) {
		return true;
    } => function () {
    	/** @var ResponseInterface $this */
    	return $this;
    };
};

(new Dispatcher($routingFactory($serviceLocator)))->dispatch(new Request, new Response);

Install with composer

  • composer require sasezaki/backbeard dev-master

Using As Middleware for zend-stratigility

https://github.com/struggle-for-php/sfp-stratigility-skeleton

  • php composer.phar create-project -s dev struggle-for-php/sfp-stratigility-skeleton path/to/install

    When install finished, you can try running with php built-in web server

  • php -S localhost:8080 -t public/ public/index.php

NOTES

THIS PROJECT IS A PROOF OF CONCEPT FOR GENERATOR BASED ROUTER, AND NOT INTENDED FOR PRODUCTION USE. PLEASE USE AT YOUR OWN RISK.

backbeard's People

Contributors

sasezaki avatar

Watchers

James Cloos 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.