浏览代码

Make error handler classes use InstanceConfigTrait.

ADmad 6 年之前
父节点
当前提交
dfcaa63ed2
共有 3 个文件被更改,包括 24 次插入19 次删除
  1. 6 5
      src/Console/ConsoleErrorHandler.php
  2. 11 8
      src/Error/BaseErrorHandler.php
  3. 7 6
      src/Error/ErrorHandler.php

+ 6 - 5
src/Console/ConsoleErrorHandler.php

@@ -36,16 +36,17 @@ class ConsoleErrorHandler extends BaseErrorHandler
     /**
      * Constructor
      *
-     * @param array $options Options for the error handler.
+     * @param array $config Config options for the error handler.
      */
-    public function __construct(array $options = [])
+    public function __construct(array $config = [])
     {
-        $options += [
+        $config += [
             'stderr' => new ConsoleOutput('php://stderr'),
             'log' => false,
         ];
-        $this->_stderr = $options['stderr'];
-        $this->_options = $options + $this->_options;
+
+        $this->setConfig($config);
+        $this->_stderr = $this->_config['stderr'];
     }
 
     /**

+ 11 - 8
src/Error/BaseErrorHandler.php

@@ -17,6 +17,7 @@ declare(strict_types=1);
 namespace Cake\Error;
 
 use Cake\Core\Configure;
+use Cake\Core\InstanceConfigTrait;
 use Cake\Log\Log;
 use Cake\Routing\Router;
 use Psr\Http\Message\ServerRequestInterface;
@@ -31,14 +32,17 @@ use Throwable;
  */
 abstract class BaseErrorHandler
 {
+    use InstanceConfigTrait;
+
     /**
      * Options to use for the Error handling.
      *
      * @var array
      */
-    protected $_options = [
+    protected $_defaultConfig = [
         'log' => true,
         'trace' => false,
+        'skipLog' => [],
         'errorLogger' => ErrorLogger::class,
     ];
 
@@ -85,8 +89,8 @@ abstract class BaseErrorHandler
     public function register(): void
     {
         $level = -1;
-        if (isset($this->_options['errorLevel'])) {
-            $level = $this->_options['errorLevel'];
+        if (isset($this->_config['errorLevel'])) {
+            $level = $this->_config['errorLevel'];
         }
         error_reporting($level);
         set_error_handler([$this, 'handleError'], $level);
@@ -95,7 +99,7 @@ abstract class BaseErrorHandler
             if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && $this->_handled) {
                 return;
             }
-            $megabytes = $this->_options['extraFatalErrorMemory'] ?? 4;
+            $megabytes = $this->_config['extraFatalErrorMemory'] ?? 4;
             if ($megabytes > 0) {
                 $this->increaseMemoryLimit($megabytes * 1024);
             }
@@ -294,7 +298,7 @@ abstract class BaseErrorHandler
             $data['file'],
             $data['line']
         );
-        if (!empty($this->_options['trace'])) {
+        if (!empty($this->_config['trace'])) {
             $trace = Debugger::trace([
                 'start' => 1,
                 'format' => 'log',
@@ -320,8 +324,7 @@ abstract class BaseErrorHandler
      */
     public function logException(Throwable $exception, ?ServerRequestInterface $request = null): bool
     {
-        $config = $this->_options;
-        if (empty($config['log'])) {
+        if (empty($this->_config['log'])) {
             return false;
         }
 
@@ -337,7 +340,7 @@ abstract class BaseErrorHandler
     {
         if ($this->logger === null) {
             /** @var \Cake\Error\ErrorLogger $logger */
-            $logger = new $this->_options['errorLogger']($this->_options);
+            $logger = new $this->_config['errorLogger']($this->_config);
             $this->logger = $logger;
         }
 

+ 7 - 6
src/Error/ErrorHandler.php

@@ -93,14 +93,15 @@ class ErrorHandler extends BaseErrorHandler
     /**
      * Constructor
      *
-     * @param array $options The options for error handling.
+     * @param array $config The options for error handling.
      */
-    public function __construct(array $options = [])
+    public function __construct(array $config = [])
     {
-        $defaults = [
+        $config += [
             'exceptionRenderer' => ExceptionRenderer::class,
         ];
-        $this->_options = $options + $defaults + $this->_options;
+
+        $this->setConfig($config);
     }
 
     /**
@@ -154,7 +155,7 @@ class ErrorHandler extends BaseErrorHandler
         Throwable $exception,
         ?ServerRequestInterface $request = null
     ): ExceptionRendererInterface {
-        $renderer = $this->_options['exceptionRenderer'];
+        $renderer = $this->_config['exceptionRenderer'];
 
         if (is_string($renderer)) {
             $class = App::className($renderer, 'Error');
@@ -198,7 +199,7 @@ class ErrorHandler extends BaseErrorHandler
     protected function _logInternalError(Throwable $exception): void
     {
         // Disable trace for internal errors.
-        $this->_options['trace'] = false;
+        $this->_config['trace'] = false;
         $message = sprintf(
             "[%s] %s\n%s", // Keeping same message format
             get_class($exception),