Skip to content

Commit

Permalink
Implement necessary ExceptionRendererInterface for clean code.
Browse files Browse the repository at this point in the history
  • Loading branch information
dereuromark committed Dec 18, 2016
1 parent b2703a7 commit 09c45b6
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 12 deletions.
4 changes: 2 additions & 2 deletions src/Error/BaseErrorHandler.php
Expand Up @@ -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)
Expand All @@ -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';
Expand Down
13 changes: 7 additions & 6 deletions src/Error/ErrorHandler.php
Expand Up @@ -94,7 +94,7 @@ public function __construct($options = [])
$defaults = [
'log' => true,
'trace' => false,
'exceptionRenderer' => 'Cake\Error\ExceptionRenderer',
'exceptionRenderer' => ExceptionRenderer::class,
];
$this->_options = $options + $defaults;
}
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 2 additions & 1 deletion src/Error/ExceptionRenderer.php
Expand Up @@ -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
{

/**
Expand Down Expand Up @@ -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;
Expand Down
26 changes: 26 additions & 0 deletions src/Error/ExceptionRendererInterface.php
@@ -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();
}
13 changes: 10 additions & 3 deletions src/Error/Middleware/ErrorHandlerMiddleware.php
Expand Up @@ -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;

Expand Down Expand Up @@ -55,7 +56,7 @@ class ErrorHandlerMiddleware
/**
* Exception render.
*
* @var \Cake\Error\ExceptionRenderer|string|null
* @var \Cake\Error\ExceptionRendererInterface|string|null
*/
protected $exceptionRenderer;

Expand Down Expand Up @@ -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)
Expand All @@ -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);
Expand Down

0 comments on commit 09c45b6

Please sign in to comment.