Skip to content
Permalink
Browse files

Implement necessary ExceptionRendererInterface for clean code.

  • Loading branch information...
dereuromark committed Dec 18, 2016
1 parent b2703a7 commit 09c45b6a221dd48c40491380f4dfcd8a016d4a8e
@@ -231,7 +231,7 @@ public function handleFatalError($code, $description, $file, $line)
* Increases the PHP "memory_limit" ini setting by the specified amount
* in kilobytes
*
* @param string $additionalKb Number in kilobytes
* @param int $additionalKb Number in kilobytes
* @return void
*/
public function increaseMemoryLimit($additionalKb)
@@ -242,7 +242,7 @@ public function increaseMemoryLimit($additionalKb)
}
$limit = trim($limit);
$units = strtoupper(substr($limit, -1));
$current = substr($limit, 0, strlen($limit) - 1);
$current = (int)substr($limit, 0, strlen($limit) - 1);
if ($units === 'M') {
$current = $current * 1024;
$units = 'K';
@@ -94,7 +94,7 @@ public function __construct($options = [])
$defaults = [
'log' => true,
'trace' => false,
'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
'exceptionRenderer' => ExceptionRenderer::class,
];
$this->_options = $options + $defaults;
}
@@ -127,13 +127,14 @@ protected function _displayError($error, $debug)
*/
protected function _displayException($exception)
{
$renderer = App::className($this->_options['exceptionRenderer'], 'Error');
$rendererClassName = App::className($this->_options['exceptionRenderer'], 'Error');
try {
if (!$renderer) {
throw new Exception("$renderer is an invalid class.");
if (!$rendererClassName) {
throw new Exception("$rendererClassName is an invalid class.");
}
$error = new $renderer($exception);
$response = $error->render();
/* @var \Cake\Error\ExceptionRendererInterface $renderer */
$renderer = new $rendererClassName($exception);
$response = $renderer->render();
$this->_clearOutput();
$this->_sendResponse($response);
} catch (Exception $e) {
@@ -47,7 +47,7 @@
* Using a subclass of ExceptionRenderer gives you full control over how Exceptions are rendered, you
* can configure your class in your config/app.php.
*/
class ExceptionRenderer
class ExceptionRenderer implements ExceptionRendererInterface
{
/**
@@ -121,6 +121,7 @@ protected function _getController()
try {
$class = App::className('Error', 'Controller', 'Controller');
/* @var \Cake\Controller\Controller $controller */
$controller = new $class($request, $response);
$controller->startupProcess();
$startup = true;
@@ -0,0 +1,26 @@
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.4.0
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Error;
interface ExceptionRendererInterface
{
/**
* Renders the response for the exception.
*
* @return \Cake\Network\Response The response to be sent.
*/
public function render();
}
@@ -18,6 +18,7 @@
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Error\ExceptionRenderer;
use Cake\Error\ExceptionRendererInterface;
use Cake\Log\Log;
use Exception;
@@ -55,7 +56,7 @@ class ErrorHandlerMiddleware
/**
* Exception render.
*
* @var \Cake\Error\ExceptionRenderer|string|null
* @var \Cake\Error\ExceptionRendererInterface|string|null
*/
protected $exceptionRenderer;
@@ -127,7 +128,7 @@ public function handleException($exception, $request, $response)
* Get a renderer instance
*
* @param \Exception $exception The exception being rendered.
* @return \Cake\Error\BaseErrorHandler The exception renderer.
* @return \Cake\Error\ExceptionRendererInterface The exception renderer.
* @throws \Exception When the renderer class cannot be found.
*/
protected function getRenderer($exception)
@@ -139,7 +140,13 @@ protected function getRenderer($exception)
if (is_string($this->exceptionRenderer)) {
$class = App::className($this->exceptionRenderer, 'Error');
if (!$class) {
throw new Exception("The '{$this->exceptionRenderer}' renderer class could not be found.");
throw new Exception(sprintf("The '%s' renderer class could not be found.",
$this->exceptionRenderer));
}
if (!$class instanceof ExceptionRendererInterface) {
throw new Exception(sprintf("The '%s' renderer class must implement %s.",
$this->exceptionRenderer,
ExceptionRendererInterface::class));
}
return new $class($exception);

0 comments on commit 09c45b6

Please sign in to comment.
You can’t perform that action at this time.