Skip to content

Commit

Permalink
updating View
Browse files Browse the repository at this point in the history
- SfpStreamView is now has-a
- TemplatePathReolver moved into View
  • Loading branch information
sasezaki committed Oct 30, 2015
1 parent 86e97ac commit 0a755a0
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
22 changes: 1 addition & 21 deletions src/Backbeard/Dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ class Dispatcher
*/
private $router;

/**
* @var TemplatePathResolverInterface
*/
private $templatePathResolver;

public function __construct(Generator $routing, ViewInterface $view, RouterInterface $router)
{
$this->routing = $routing;
Expand Down Expand Up @@ -94,18 +89,6 @@ public function dispatch(Request $request, Response $response)
return new DispatchResult(false);
}

/**
* @return \Backbeard\TemplatePathResolverInterface
*/
public function getTemplatePathResolver()
{
if (!$this->templatePathResolver) {
$this->templatePathResolver = new TemplatePathResolver();
}

return $this->templatePathResolver;
}

/**
* @return bool
* @throws InvalidArgumentException
Expand Down Expand Up @@ -158,12 +141,9 @@ protected function handleActionResult(RouteMatch $routeMatch, $actionResult, Res
{
if (is_string($actionResult)) {
$response->getBody()->write($actionResult);

return $response;
} elseif (is_array($actionResult)) {
$template = $this->getTemplatePathResolver()->resolve($routeMatch);

$model = new ViewModel($actionResult, $template);
$model = $this->view->factoryModel($actionResult, $routeMatch);
return $this->view->marshalResponse($model, $response);
} elseif ($actionResult instanceof Response) {
return $actionResult;
Expand Down
40 changes: 35 additions & 5 deletions src/Backbeard/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,48 @@

namespace Backbeard;

use SfpStreamView\View as BaseView;
use SfpStreamView\View as StreamView;
use Psr\Http\Message\ResponseInterface;

class View extends BaseView implements ViewInterface
class View implements ViewInterface
{

/**
* @var TemplatePathResolverInterface
*/
private $templatePathResolver;

public function __construct(StreamView $streamView)
{
$this->streamView = $streamView;
}

/**
* @return \Backbeard\TemplatePathResolverInterface
*/
public function getTemplatePathResolver()
{
if (!$this->templatePathResolver) {
$this->templatePathResolver = new TemplatePathResolver();
}
return $this->templatePathResolver;
}

public function factoryModel($vars, RouteMatch $routeMatch)
{
$template = $this->getTemplatePathResolver()->resolve($routeMatch);
return new ViewModel($vars, $template);
}

public function marshalResponse(ViewModelInterface $model, ResponseInterface $response)
{
$template = $model->getTemplate();
$vars = $model->getVariables();

$this->assign($vars);
$this->renderResponse($template, $response);

$streamView = clone $this->streamView;

$streamView->assign($vars);
$streamView->renderResponse($template, $response);

return $response;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Backbeard/ViewInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

interface ViewInterface
{

public function factoryModel($vars, RouteMatch $routeMatch);

/**
* @param ViewModelInterface $model
* @param ResponseInterface $response
Expand Down
5 changes: 3 additions & 2 deletions tests/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
use Backbeard\ValidationError;
use Backbeard\View;
use Backbeard\Router;
use Backbeard\RouteMatch;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\ResponseInterface;
use Zend\Diactoros\ServerRequestFactory;
use Zend\Diactoros\Response;
use Zend\Diactoros\Uri;
use Backbeard\RouteMatch;
use SfpStreamView\View as StreamView;

class DispatcherTest extends \PHPUnit_Framework_TestCase
{
Expand All @@ -21,7 +22,7 @@ class DispatcherTest extends \PHPUnit_Framework_TestCase

public function setUp()
{
$this->view = new View(__DIR__.'/_files/views');
$this->view = new View(new StreamView(__DIR__.'/_files/views'));
$this->router = new Router(new \FastRoute\RouteParser\Std());
}

Expand Down

0 comments on commit 0a755a0

Please sign in to comment.