Skip to content

Commit

Permalink
Merge pull request #2 from sasezaki/view-marshal-response
Browse files Browse the repository at this point in the history
Introduce ViewModel
  • Loading branch information
sasezaki committed Jul 2, 2015
2 parents 772694e + 49fdd89 commit 388f52e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 28 deletions.
8 changes: 2 additions & 6 deletions src/Backbeard/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,8 @@ protected function handleActionResult(RouteMatch $routeMatch, $actionResult, Res
} elseif (is_array($actionResult)) {
$template = $this->getTemplatePathResolver()->resolve($routeMatch);

$view = $this->view;
$view->assign($actionResult);

$body = $view->render($template, $response->getBody());

return $response->withBody($body);
$model = new ViewModel($actionResult, $template);
return $this->view->marshalResponse($model, $response);
} elseif ($actionResult instanceof Response) {
return $actionResult;
} elseif (is_int($actionResult)) {
Expand Down
22 changes: 10 additions & 12 deletions src/Backbeard/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
namespace Backbeard;

use SfpStreamView\View as BaseView;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\Stream;

class View extends BaseView implements ViewInterface
{
/**
* {@inheritdoc}
*/
public function render($template, $stream = null)
public function marshalResponse(ViewModelInterface $model, ResponseInterface $response)
{
if ($stream instanceof StreamInterface) {
$stream = $stream->detach();
}

parent::render($template, $stream);

return new Stream($stream);
$template = $model->getTemplate();
$vars = $model->getVariables();
$stream = $response->getBody()->detach();

$this->assign($vars);
$this->render($template, $stream);

return $response->withBody(new Stream($stream));
}
}
15 changes: 5 additions & 10 deletions src/Backbeard/ViewInterface.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
<?php

namespace Backbeard;
use Psr\Http\Message\ResponseInterface;

interface ViewInterface
{
/**
* @param array $array
* @param ViewModelInterface $model
* @param ResponseInterface $response
* @return ResponseInterface
*/
public function assign($array);

/**
* @param string $template
* @param resoure|\Psr\Http\Message\StreamInterface $stream
*
* @return \Psr\Http\Message\StreamInterface
*/
public function render($template, $stream = null);
public function marshalResponse(ViewModelInterface $model, ResponseInterface $response);
}
36 changes: 36 additions & 0 deletions src/Backbeard/ViewModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace Backbeard;

class ViewModel implements ViewModelInterface
{
/**
* @var array
*/
private $variables;

/**
* @var string
*/
private $template;

/**
* @param array $variables
* @param string $template
*/
public function __construct($variables, $template)
{
$this->variables = $variables;
$this->template = $template;
}

public function getVariables()
{
return $this->variables;
}

public function getTemplate()
{
return $this->template;
}
}
16 changes: 16 additions & 0 deletions src/Backbeard/ViewModelInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Backbeard;

interface ViewModelInterface
{
/**
* @return array
*/
public function getVariables();

/**
* @return string
*/
public function getTemplate();
}

0 comments on commit 388f52e

Please sign in to comment.