Skip to content

Commit

Permalink
Deprecate request/response on Component.
Browse files Browse the repository at this point in the history
Start deprecating the request/response properties on components. By not
storing references to PSR7 objects all over the place, we can migrate to
using immutable methods more easily in the future. I don't think it will
be easy to start using the immutable methods _now_ as user-land
components could easily be referencing old public properties.
  • Loading branch information
markstory committed Nov 15, 2016
1 parent 585453b commit e6d4ea5
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 91 deletions.
14 changes: 14 additions & 0 deletions src/Controller/Component.php
Expand Up @@ -66,13 +66,17 @@ class Component implements EventListenerInterface
* Request object
*
* @var \Cake\Http\ServerRequest
* @deprecated 3.4.0 Storing references to the request is deprecated. Use Component::getController()
* or callback $event->subject() to access the controller & request instead.
*/
public $request;

/**
* Response object
*
* @var \Cake\Network\Response
* @deprecated 3.4.0 Storing references to the response is deprecated. Use Component::getController()
* or callback $event->subject() to access the controller & response instead.
*/
public $response;

Expand Down Expand Up @@ -129,6 +133,16 @@ public function __construct(ComponentRegistry $registry, array $config = [])
$this->initialize($config);
}

/**
* Get the controller this component is bound to.
*
* @return \Cake\Controller\Controller The bound controller.
*/
public function getController()
{
return $this->_registry->getController();
}

/**
* Constructor hook method.
*
Expand Down
62 changes: 33 additions & 29 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -41,16 +41,16 @@ class RequestHandlerComponent extends Component
{

/**
* Determines whether or not callbacks will be fired on this component
*
* @var bool
* @deprecated 3.4.0 Unused. Will be removed in 4.0.0
*/
public $enabled = true;

/**
* Holds the reference to Controller::$response
*
* @var \Cake\Network\Response
* @deprecated 3.4.0 Will be removed in 4.0.0
*/
public $response;

Expand Down Expand Up @@ -128,20 +128,12 @@ public function implementedEvents()
}

/**
* Checks to see if a specific content type has been requested and sets RequestHandler::$ext
* accordingly. Checks the following in order: 1. The '_ext' value parsed by the Router. 2. A specific
* AJAX type request indicated by the presence of a header. 3. The Accept header. With the exception
* of an AJAX request indicated using the second header based method above, the type must have
* been configured in {@link Cake\Routing\Router}.
*
* @param array $config The config data.
* @return void
* @see \Cake\Routing\Router::extensions()
* @deprecated 3.4.0 Unused. To be removed in 4.0.0
*/
public function initialize(array $config)
{
$controller = $this->_registry->getController();
$this->response =& $controller->response;
}

/**
Expand Down Expand Up @@ -198,12 +190,13 @@ public function startup(Event $event)
{
$controller = $event->subject();
$request = $controller->request;
$response = $controller->response;

if ($request->param('_ext')) {
$this->ext = $request->param('_ext');
}
if (!$this->ext || in_array($this->ext, ['html', 'htm'])) {
$this->_setExtension($request, $this->response);
$this->_setExtension($request, $response);
}

$request->params['isAjax'] = $request->is('ajax');
Expand Down Expand Up @@ -278,6 +271,7 @@ public function beforeRedirect(Event $event, $url, Response $response)
list($url, $querystr) = explode('?', $url, 2);
parse_str($querystr, $query);
}
/* @var \Cake\Controller\Controller $controller */
$controller = $event->subject();
$response->body($controller->requestAction($url, [
'return',
Expand All @@ -286,7 +280,7 @@ public function beforeRedirect(Event $event, $url, Response $response)
'REQUEST_METHOD' => 'GET'
],
'query' => $query,
'cookies' => $request->cookies
'cookies' => $request->getCookieParams()
]));

return $response->withStatus(200);
Expand Down Expand Up @@ -317,21 +311,24 @@ public function beforeRedirect(Event $event, $url, Response $response)
*/
public function beforeRender(Event $event)
{
/* @var \Cake\Controller\Controller $controller */
$controller = $event->subject();
$response = $controller->response;
$request = $controller->request;

$isRecognized = (
!in_array($this->ext, ['html', 'htm']) &&
$this->response->getMimeType($this->ext)
$response->getMimeType($this->ext)
);

if ($this->ext && $isRecognized) {
/* @var \Cake\Controller\Controller $controller */
$controller = $event->subject();
$this->renderAs($controller, $this->ext);
} else {
$this->response->charset(Configure::read('App.encoding'));
$response->charset(Configure::read('App.encoding'));
}

if ($this->_config['checkHttpCache'] &&
$this->response->checkNotModified($this->request)
$response->checkNotModified($request)
) {
return false;
}
Expand Down Expand Up @@ -418,8 +415,9 @@ public function isWap()
*/
public function accepts($type = null)
{
$request = $this->request;
$response = $this->response;
$controller = $this->getController();
$request = $controller->request;
$response = $controller->response;
$accepted = $request->accepts();

if (!$type) {
Expand Down Expand Up @@ -452,7 +450,10 @@ public function accepts($type = null)
*/
public function requestedWith($type = null)
{
$request = $this->request;
$controller = $this->getController();
$request = $controller->request;
$response = $controller->response;

if (!$request->is('post') &&
!$request->is('put') &&
!$request->is('patch') &&
Expand All @@ -471,7 +472,6 @@ public function requestedWith($type = null)
}

list($contentType) = explode(';', $request->contentType());
$response = $this->response;
if ($type === null) {
return $response->mapType($contentType);
}
Expand All @@ -498,8 +498,9 @@ public function requestedWith($type = null)
*/
public function prefers($type = null)
{
$request = $this->request;
$response = $this->response;
$controller = $this->getController();
$request = $controller->request;
$response = $controller->response;
$acceptRaw = $request->parseAccept();

if (empty($acceptRaw)) {
Expand Down Expand Up @@ -597,7 +598,7 @@ public function renderAs(Controller $controller, $type, array $options = [])
$builder->layoutPath($type);
}

$response = $this->response;
$response = $controller->response;
if ($response->getMimeType($type)) {
$this->respondAs($type, $options);
}
Expand Down Expand Up @@ -630,7 +631,10 @@ public function respondAs($type, array $options = [])
$options += $defaults;

$cType = $type;
$response = $this->response;
$controller = $this->getController();
$response = $controller->response;
$request = $controller->request;

if (strpos($type, '/') === false) {
$cType = $response->getMimeType($type);
}
Expand All @@ -649,7 +653,7 @@ public function respondAs($type, array $options = [])
if (!$type) {
return false;
}
if (!$this->request->param('requested')) {
if (!$request->param('requested')) {
$response->type($cType);
}
if (!empty($options['charset'])) {
Expand All @@ -670,7 +674,7 @@ public function respondAs($type, array $options = [])
*/
public function responseType()
{
$response = $this->response;
$response = $this->getController()->response;

return $response->mapType($response->type());
}
Expand All @@ -688,7 +692,7 @@ public function mapAlias($alias)
if (is_array($alias)) {
return array_map([$this, 'mapAlias'], $alias);
}
$response = $this->response;
$response = $this->getController()->response;
$type = $response->getMimeType($alias);
if ($type) {
if (is_array($type)) {
Expand Down

0 comments on commit e6d4ea5

Please sign in to comment.