Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move default into class so simpler ::class syntax can be used. #9806

Merged
merged 3 commits into from
Dec 9, 2016
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/Error/Middleware/ErrorHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Error\ExceptionRenderer;
use Cake\Log\Log;
use Exception;

Expand Down Expand Up @@ -51,17 +52,28 @@ class ErrorHandlerMiddleware
'trace' => false,
];

/**
* Exception render.
*
* @var \Cake\Error\ExceptionRenderer|string|null
*/
protected $exceptionRenderer;

/**
* Constructor
*
* @param string|callable|null $renderer The renderer or class name
* to use or a callable factory.
* @param string|callable|null $exceptionRenderer The renderer or class name
* to use or a callable factory. If null, Configure::read('Error.exceptionRenderer')
* will be used.
* @param array $config Configuration options to use. If empty, `Configure::read('Error')`
* will be used.
*/
public function __construct($renderer = null, array $config = [])
public function __construct($exceptionRenderer = null, array $config = [])
{
$this->renderer = $renderer ?: 'Cake\Error\ExceptionRenderer';
if ($exceptionRenderer) {
$this->exceptionRenderer = $exceptionRenderer;
}

$config = $config ?: Configure::read('Error');
$this->config($config);
}
Expand Down Expand Up @@ -120,15 +132,19 @@ public function handleException($exception, $request, $response)
*/
protected function getRenderer($exception)
{
if (is_string($this->renderer)) {
$class = App::className($this->renderer, 'Error');
if (!$this->exceptionRenderer) {
$this->exceptionRenderer = $this->config('exceptionRender') ?: ExceptionRenderer::class;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still needs to be exceptionRenderer.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh that name.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the config value doesn't!atch the one in the app skeleton

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason this couldn't be done in the constructor? Are you trying to avoid the class load?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see the need to check for default / fallback class so early until it needs to be actually instantiated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this a case for using $_defaultConfig?

Copy link
Member

@markstory markstory Nov 28, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, but that would introduce strings again, which is what I think @dereuromark is trying to remove.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I only wanted to not have it outside to be a requirement to be passed in :) See the linked app PR


if (is_string($this->exceptionRenderer)) {
$class = App::className($this->exceptionRenderer, 'Error');
if (!$class) {
throw new Exception("The '{$this->renderer}' renderer class could not be found.");
throw new Exception("The '{$this->exceptionRenderer}' renderer class could not be found.");
}

return new $class($exception);
}
$factory = $this->renderer;
$factory = $this->exceptionRenderer;

return $factory($exception);
}
Expand Down