2017 © Pedro Peláez
 

library responder-bundle

Responder for action domain responder(adr) pattern

image

foa/responder-bundle

Responder for action domain responder(adr) pattern

  • Thursday, November 20, 2014
  • by harikt
  • Repository
  • 1 Watchers
  • 6 Stars
  • 143 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 6 Versions
  • 11 % Grown

The README.md

FOA.Responder_Bundle

AbstractResponder of action domain responder by Paul M Jones., (*1)

Foreword

Installation

It is installable and autoloadable via composer as foa/responder-bundle., (*2)

composer require foa/responder-bundle

Quality

Scrutinizer Code Quality Code Coverage Build Status, (*3)

This library attempts to comply with PSR-1, PSR-2, and PSR-4. If you notice compliance oversights, please send a patch via pull request., (*4)

Community

To ask questions, provide feedback, or otherwise communicate with the Aura community, please join our Google Group, follow @auraphp on Twitter, or chat with us on #auraphp on Freenode., (*5)

Example usage

It is recommend you first go through the action domain responder paper and example code., (*6)

In this examples we are using few aura components, but foa/responder-bundle is not specifically for Aura framework. You can integrate in any framework if you like the concept using its classes., (*7)

Consider you are having a blog application, which you can browse the posts. Let us see how it looks in ADR. You should pay special attention to responder only. Action and Service layer components may differ with what you are using., (*8)

Action

<?php
// BlogBrowseAction.php
namespace Vendor\Blog\Web\Action;

use FOA\DomainPayload\PayloadFactory;
use Vendor\Blog\Web\Responder\BlogBrowseResponder;

class BlogBrowseAction
{
    protected $domain;
    protected $responder;

    public function __construct(
        BlogService $domain,
        BlogBrowseResponder $responder
    ) {
        $this->domain = $domain;
        $this->responder = $responder;
    }

    public function __invoke()
    {
        $payload_factory = new PayloadFactory();
        $payload = $payload_factory->found(
            array(
                array('title' => 'Some awesome title', 'author' => 'Hari KT'),
                array('title' => 'Some awesome post', 'author' => 'Paul M Jones'),
                array('title' => 'Some awesome content', 'author' => 'Justin'),
            )
        );
        // Rather than the above code, you should actually do something like
        // $payload = $this->domain->fetchPage($page, $paging);
        $this->responder->setPayload($payload);
        return $this->responder->__invoke();
    }
}

Responder

<?php
// BlogBrowseResponder.php
namespace Vendor\Blog\Web\Responder;

use FOA\Responder_Bundle\AbstractResponder;

class BlogBrowseResponder extends AbstractResponder
{
    protected $available = array(
        'text/html' => '',
        'application/json' => '.json',
    );

    protected $payload_method = array(
        'FOA\DomainPayload\Found' => 'display'
    );

    public function display()
    {
        if ($this->negotiateMediaType()) {
            $content_type = $this->response->content->getType();
            if ($content_type) {
                $view .= $this->available[$content_type];
            }
            $this->renderView('browse', 'layout');
        }
    }
}

First instantiate your templating engine of choice. Eg usage with Aura.View. See other templating engines supported below., (*9)

<?php
$factory = new \Aura\Html\HelperLocatorFactory;
$helpers = $factory->newInstance();

$engine = new \Aura\View\View(
    new \Aura\View\TemplateRegistry,
    new \Aura\View\TemplateRegistry,
    $helpers
);

$renderer = new \FOA\Responder_Bundle\Renderer\AuraView($engine);

Create your responder object

<?php
$web_factory = new \Aura\Web\WebFactory($GLOBALS);
$response = $web_factory->newResponse();

$accept_factory = new \Aura\Accept\AcceptFactory($_SERVER);
$accept = $accept_factory->newInstance();

$responder = new \Vendor\Blog\Web\Responder\BlogBrowseResponder($accept, $response, $renderer);

Rendering and Setting content to response

<?php
use FOA\DomainPayload\PayloadFactory;

$payload_factory = new PayloadFactory();
$payload = $payload_factory->found(array('name' => 'Hari KT'));
$responder->setPayload($payload);

$responder->__invoke();

Calling __invoke will render and set the content on the response object. Now you can either use Aura\Web\ResponseSender to send the response, or get the headers from response object and set to your favourite library response., (*10)

<?php
echo $response->content->get();

Aura framework integration with Aura.Di

In your project config/Common.php define method add the below lines., (*11)

$di->params['FOA\Responder_Bundle\Renderer\AuraView']['engine'] = $di->lazyNew('Aura\View\View');
// responder
$di->params['FOA\Responder_Bundle\AbstractResponder']['response'] = $di->lazyGet('aura/web-kernel:response');
$di->params['FOA\Responder_Bundle\AbstractResponder']['renderer'] = $di->lazyNew('FOA\Responder_Bundle\Renderer\AuraView');
$di->params['FOA\Responder_Bundle\AbstractResponder']['accept'] = $di->lazyNew('Aura\Accept\Accept');

Don't forget to change the renderer with the one you like., (*12)

Integrated templating engines

Responder bundle integrates below templating engines. Feel free to choose the one you love., (*13)

  1. aura/view
  2. league/plates
  3. mustache/mustache
  4. twig/twig
  5. smarty/smarty

Integrating other templating engines

<?php
namespace FOA\Responder_Bundle\Renderer;

use FOA\Responder_Bundle\Renderer\RendererInterface;
use Your\TemplateEngine;

class YourTemplateEngine implements RendererInterface
{
    public function __construct(TemplateEngine $engine)
    {
        $this->engine = $engine;
    }

    public function render($data, $view, $layout = null)
    {
        // according to how the rendering engine works. See other implementations
        // $this->engine->render
    }
}

Yes, and we love tests!., (*14)

The Versions

20/11 2014

dev-master

9999999-dev

Responder for action domain responder(adr) pattern

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Paul M Jones

20/11 2014
20/11 2014
18/11 2014

0.3.0

0.3.0.0

Responder for action domain responder(adr) pattern

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Paul M Jones

15/11 2014

0.2.0

0.2.0.0

Responder for action domain responder(adr) pattern

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

by Paul M Jones

13/11 2014

0.1.0

0.1.0.0

Responder for action domain responder(adr) pattern

  Sources   Download

BSD-2-Clause

The Requires

 

by Paul M Jones