mrjulio/rapture-adr

Rapture ADR component

v1.0.2 2017-11-18 19:08 UTC

This package is not auto-updated.

Last update: 2024-11-02 16:40:33 UTC


README

PhpVersion License

Action-Domain-Responder pattern implementation

More info here: https://github.com/pmjones/adr

Requirements

  • PHP v7.0
  • php-json

Install

composer require mrjulio/rapture-adr

Quick start

# action

namespace Demo\Action\User;

class View extends Action
{
    public function __invoke():array
    {
        $userId = $this->request()->getAttribute('id');
        
        $user = \Demo\Domain\Model\UserQuery::create()
            ->filterById($userId)
            ->findOne();
            
        if (!$user) {
            throw new HttpNotFoundException('User not found');
        }

        return [
            'user' => $user
        ];
    }
}

# Responder

namespace Demo\Responder\User;

class View extends Responder
{
    // demo
    public function preInvoke(array $data)
    {
        $this->template = new Template($this->getTemplateName(), $data);
    }
    
    // demo
    public function __invoke(array $data)
    {
        $stream = new Stream(fopen('php://memory', 'r+'));
        $stream->write($this->template->render());

        $this->response->withBody($stream)->send();
    }
}

# Dispatcher

(new Dispatcher('Demo', $router))->dispatch($request, $response);

About

Author

Iulian N. rapture@iuliann.ro

Credits

License

Rapture PHP ADR is licensed under the MIT License - see the LICENSE file for details.