Skip to content

Commit

Permalink
Moving view vars assigment to dispatcher
Browse files Browse the repository at this point in the history
  • Loading branch information
silvamfilipe committed Apr 10, 2016
1 parent 4f01ad9 commit 914da58
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 11 deletions.
21 changes: 16 additions & 5 deletions src/Controller.php
Expand Up @@ -35,6 +35,11 @@ abstract class Controller implements ControllerInterface
*/
protected $response;

/**
* @var array
*/
protected $vars = [];

/**
* For URL parsing
*/
Expand Down Expand Up @@ -103,6 +108,16 @@ public function set($key, $value = null)
return $this;
}

/**
* Returns the data that was set on controller action
*
* @return array
*/
public function getViewVars()
{
return $this->vars;
}

/**
* Enables or disables rendering
*
Expand Down Expand Up @@ -178,11 +193,7 @@ public function getRouteAttributes($name = null, $default = null)
*/
protected function registerVar($key, $value)
{
$attrName = ControllerInterface::REQUEST_ATTR_VIEW_DATA;
$attributes = $this->request->getAttributes();
$attributes[$attrName][$key] = $value;
$this->request = $this->request
->withAttribute($attrName, $attributes[$attrName]);
$this->vars[$key] = $value;
return $this;
}

Expand Down
7 changes: 7 additions & 0 deletions src/ControllerInterface.php
Expand Up @@ -104,4 +104,11 @@ public function setView($template);
* @return mixed
*/
public function getRouteAttributes($name = null, $default = null);

/**
* Returns the data that was set on controller action
*
* @return array
*/
public function getViewVars();
}
24 changes: 23 additions & 1 deletion src/Dispatcher.php
Expand Up @@ -69,14 +69,36 @@ public function handle(
$this->checkAction($class);

call_user_func_array([$controller, $this->action], $this->args);
$request = $controller->getRequest();
$request = $this->setViewVars($controller, $request);

return $this->executeNext(
$controller->getRequest(),
$request,
$controller->getResponse()
);

}

/**
* Sets the data values into request
*
* @param ControllerInterface $controller
* @param ServerRequestInterface $request
*
* @return ServerRequestInterface|static
*/
protected function setViewVars(
ControllerInterface $controller, ServerRequestInterface $request
) {
$key = $controller::REQUEST_ATTR_VIEW_DATA;
$data = $request->getAttribute($key, []);
$request = $request->withAttribute(
$key,
array_merge($data, $controller->getViewVars())
);
return $request;
}

/**
* Creates the controller with provided class name
*
Expand Down
7 changes: 2 additions & 5 deletions tests/ControllerTest.php
Expand Up @@ -73,12 +73,10 @@ public function getResponse()
*/
public function setSingleValue()
{
$index = ControllerInterface::REQUEST_ATTR_VIEW_DATA;
$this->controller->set('foo', 'bar');
$request = $this->controller->getRequest();
$this->assertEquals(
'bar',
$request->getAttribute($index)['foo']
$this->controller->getViewVars()['foo']
);
}

Expand All @@ -90,12 +88,11 @@ public function setMultipleValues()
{
$foo = 'bar';
$baz = 'moo';
$index = ControllerInterface::REQUEST_ATTR_VIEW_DATA;
$expected = ['foo' => 'bar', 'baz' => 'moo'];
$this->controller->set(compact('foo', 'baz'));
$this->assertEquals(
$expected,
$this->controller->getRequest()->getAttribute($index)
$this->controller->getViewVars()
);
}

Expand Down

0 comments on commit 914da58

Please sign in to comment.