Skip to content

Commit

Permalink
Fix failing deprecated method use in ExceptionRenderer tests.
Browse files Browse the repository at this point in the history
Fix a bunch of related things as they were in the call stack as well.
  • Loading branch information
markstory committed Nov 16, 2017
1 parent 32ef626 commit a158d43
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 178 deletions.
9 changes: 5 additions & 4 deletions src/Controller/Component/RequestHandlerComponent.php
Expand Up @@ -331,7 +331,7 @@ public function beforeRender(Event $event)
if ($this->ext && $isRecognized) {
$this->renderAs($controller, $this->ext);
} else {
$response->charset(Configure::read('App.encoding'));
$controller->response = $response->withCharset(Configure::read('App.encoding'));
}

if ($this->_config['checkHttpCache'] &&
Expand Down Expand Up @@ -652,14 +652,15 @@ public function respondAs($type, array $options = [])
return false;
}
if (!$request->getParam('requested')) {
$response->type($cType);
$response = $response->withType($cType);
}
if (!empty($options['charset'])) {
$response->charset($options['charset']);
$response = $response->withCharset($options['charset']);
}
if (!empty($options['attachment'])) {
$response->download($options['attachment']);
$response = $response->withDownload($options['attachment']);
}
$controller->response = $response;

return true;
}
Expand Down
11 changes: 9 additions & 2 deletions src/Core/Exception/Exception.php
Expand Up @@ -86,11 +86,11 @@ public function getAttributes()
/**
* Get/set the response header to be used
*
* See also Cake\Http\Response::header()
* See also Cake\Http\Response::withHeader()
*
* @param string|array|null $header An array of header strings or a single header string
* - an associative array of "header name" => "header value"
* - an array of string headers is also accepted
* - an array of string headers is also accepted (deprecated)
* @param string|null $value The header value.
* @return array
*/
Expand All @@ -100,6 +100,13 @@ public function responseHeader($header = null, $value = null)
return $this->_responseHeaders;
}
if (is_array($header)) {
if (isset($header[0])) {
deprecationWarning(
'Passing a list string headers to Exception::responseHeader() is deprecated. ' .
'Use an associative array instead.'
);
}

return $this->_responseHeaders = $header;
}
$this->_responseHeaders = [$header => $value];
Expand Down
21 changes: 13 additions & 8 deletions src/Error/ExceptionRenderer.php
Expand Up @@ -21,7 +21,7 @@
use Cake\Core\Exception\MissingPluginException;
use Cake\Event\Event;
use Cake\Http\Response;
use Cake\Http\ServerRequest;
use Cake\Http\ServerRequestFactory;
use Cake\Network\Exception\HttpException;
use Cake\Routing\DispatcherFactory;
use Cake\Routing\Router;
Expand Down Expand Up @@ -115,7 +115,7 @@ protected function _unwrap($exception)
protected function _getController()
{
if (!$request = Router::getRequest(true)) {
$request = ServerRequest::createFromGlobals();
$request = ServerRequestFactory::fromGlobals();
}
$response = new Response();
$controller = null;
Expand Down Expand Up @@ -169,11 +169,15 @@ public function render()

$message = $this->_message($exception, $code);
$url = $this->controller->request->getRequestTarget();
$response = $this->controller->response;

if ($exception instanceof CakeException) {
$this->controller->response->header($exception->responseHeader());
foreach ((array)$exception->responseHeader() as $key => $value) {
$response = $response->withHeader($key, $value);
}
}
$this->controller->response->statusCode($code);
$response = $response->withStatus($code);

$viewVars = [
'message' => $message,
'url' => h($url),
Expand All @@ -197,6 +201,7 @@ public function render()
$this->controller->set($unwrapped->getAttributes());
}

$this->controller->response = $response;
return $this->_outputMessage($template);
}

Expand All @@ -212,8 +217,7 @@ protected function _customMethod($method, $exception)
$result = call_user_func([$this, $method], $exception);
$this->_shutdown();
if (is_string($result)) {
$this->controller->response->body($result);
$result = $this->controller->response;
$result = $this->controller->response->withStringBody($result);
}

return $result;
Expand Down Expand Up @@ -361,8 +365,9 @@ protected function _outputMessageSafe($template)
->setTemplatePath('Error');
$view = $this->controller->createView('View');

$this->controller->response->body($view->render($template, 'error'));
$this->controller->response->type('html');
$this->controller->response = $this->controller->response
->withType('html')
->withStringBody($view->render($template, 'error'));

return $this->controller->response;
}
Expand Down
28 changes: 16 additions & 12 deletions src/Http/Response.php
Expand Up @@ -484,9 +484,7 @@ protected function _createStream()
*/
public function send()
{
deprecationWarning(
'Will be removed in 4.0.0'
);
deprecationWarning('Response::send() will be removed in 4.0.0');

if ($this->hasHeader('Location') && $this->_status === 200) {
$this->statusCode(302);
Expand Down Expand Up @@ -1625,7 +1623,7 @@ public function modified($time = null)
{
deprecationWarning(
'Response::modified() is deprecated. ' .
'Use withModified() instead.'
'Use withModified() or getHeaderLine("Last-Modified") instead.'
);

if ($time !== null) {
Expand Down Expand Up @@ -1668,12 +1666,14 @@ public function withModified($time)
* setting the status code to "304 Not Modified" and removing all
* conflicting headers
*
* *Warning* This method mutates the response in-place and should be avoided.
*
* @return void
*/
public function notModified()
{
$this->statusCode(304);
$this->body('');
$this->_status = 304;
$this->_createStream();

$remove = [
'Allow',
Expand Down Expand Up @@ -1790,7 +1790,7 @@ public function etag($hash = null, $weak = false)
{
deprecationWarning(
'Response::etag() is deprecated. ' .
'Use withEtag() instead.'
'Use withEtag() or getHeaderLine("Etag") instead.'
);

if ($hash !== null) {
Expand Down Expand Up @@ -1976,18 +1976,22 @@ public function withLength($bytes)
* the Last-Modified etag response header before calling this method. Otherwise
* a comparison will not be possible.
*
* *Warning* This method mutates the response in-place and should be avoided.
*
* @param \Cake\Http\ServerRequest $request Request object
* @return bool Whether the response was marked as not modified or not.
*/
public function checkNotModified(ServerRequest $request)
{
$etags = preg_split('/\s*,\s*/', (string)$request->header('If-None-Match'), 0, PREG_SPLIT_NO_EMPTY);
$modifiedSince = $request->header('If-Modified-Since');
if ($responseTag = $this->etag()) {
$etags = preg_split('/\s*,\s*/', (string)$request->getHeaderLine('If-None-Match'), 0, PREG_SPLIT_NO_EMPTY);
$responseTag = $this->getHeaderLine('Etag');
if ($responseTag) {
$etagMatches = in_array('*', $etags) || in_array($responseTag, $etags);
}
if ($modifiedSince) {
$timeMatches = strtotime($this->modified()) === strtotime($modifiedSince);

$modifiedSince = $request->getHeaderLine('If-Modified-Since');
if ($modifiedSince && $this->hasHeader('Last-Modified')) {
$timeMatches = strtotime($this->getHeaderLine('Last-Modifed')) === strtotime($modifiedSince);
}
$checks = compact('etagMatches', 'timeMatches');
if (empty($checks)) {
Expand Down
6 changes: 5 additions & 1 deletion src/Template/Error/missing_template.ctp
Expand Up @@ -28,7 +28,11 @@ $this->start('subheading');
<?= sprintf('The template %s</em> was not found.', h($file)); ?>
<?php else: ?>
<strong>Error: </strong>
<?= sprintf('The view for <em>%sController::%s()</em> was not found.', h(Inflector::camelize($this->request->controller)), h($this->request->action)); ?>
<?= sprintf(
'The view for <em>%sController::%s()</em> was not found.',
h(Inflector::camelize($this->request->getParam('controller'))),
h($this->request->getParam('action'))
); ?>
<?php endif ?>
<?php $this->end() ?>

Expand Down
2 changes: 1 addition & 1 deletion src/Template/Error/pdo_error.ctp
Expand Up @@ -14,7 +14,7 @@
*/
use Cake\Error\Debugger;

$this->layout = 'dev_error';
$this->setLayout('dev_error');

$this->assign('title', 'Database Error');
$this->assign('templateName', 'pdo_error.ctp');
Expand Down

0 comments on commit a158d43

Please sign in to comment.