From 20465432898b4214c970fdcf86073cd3d98bfea8 Mon Sep 17 00:00:00 2001 From: Serghei Iakovlev Date: Sun, 16 Aug 2015 23:05:46 +0300 Subject: [PATCH] Added support for formatter to Phalcon\Error\Handler --- Library/Phalcon/Error/Application.php | 6 ++--- Library/Phalcon/Error/Error.php | 9 ++++---- Library/Phalcon/Error/Handler.php | 33 +++++++++++++++------------ Library/Phalcon/Error/README.md | 9 ++++---- 4 files changed, 29 insertions(+), 28 deletions(-) diff --git a/Library/Phalcon/Error/Application.php b/Library/Phalcon/Error/Application.php index 0a1b461b8..6c4b2e814 100644 --- a/Library/Phalcon/Error/Application.php +++ b/Library/Phalcon/Error/Application.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -66,9 +66,7 @@ public function main() private function registerAutoloaders() { $loader = new Loader(); - $loader->registerNamespaces(array( - 'Phalcon\Error' => '.', - )); + $loader->registerNamespaces(['Phalcon\Error' => '.']); $loader->register(); } diff --git a/Library/Phalcon/Error/Error.php b/Library/Phalcon/Error/Error.php index e62500075..0daee35de 100644 --- a/Library/Phalcon/Error/Error.php +++ b/Library/Phalcon/Error/Error.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -21,7 +21,6 @@ class Error { - /** * @var array */ @@ -32,9 +31,9 @@ class Error * * @param array $options */ - public function __construct(array $options = array()) + public function __construct(array $options = []) { - $defaults = array( + $defaults = [ 'type' => -1, 'message' => 'No error message', 'file' => '', @@ -42,7 +41,7 @@ public function __construct(array $options = array()) 'exception' => null, 'isException' => false, 'isError' => false, - ); + ]; $options = array_merge($defaults, $options); diff --git a/Library/Phalcon/Error/Handler.php b/Library/Phalcon/Error/Handler.php index 042247d43..eb6fade21 100644 --- a/Library/Phalcon/Error/Handler.php +++ b/Library/Phalcon/Error/Handler.php @@ -3,7 +3,7 @@ +------------------------------------------------------------------------+ | Phalcon Framework | +------------------------------------------------------------------------+ - | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com) | + | Copyright (c) 2011-2015 Phalcon Team (http://www.phalconphp.com) | +------------------------------------------------------------------------+ | This source file is subject to the New BSD License that is bundled | | with this package in the file docs/LICENSE.txt. | @@ -15,15 +15,16 @@ | Authors: Andres Gutierrez | | Eduar Carvajal | | Nikita Vershinin | + | Serghei Iakovlev | +------------------------------------------------------------------------+ */ namespace Phalcon\Error; -use Phalcon\DI; +use Phalcon\Di; +use Phalcon\Logger\Formatter; class Handler { - /** * Registers itself as error and exception handler. * @@ -50,26 +51,26 @@ public static function register() return; } - $options = array( + $options = [ 'type' => $errno, 'message' => $errstr, 'file' => $errfile, 'line' => $errline, 'isError' => true, - ); + ]; static::handle(new Error($options)); }); set_exception_handler(function (\Exception $e) { - $options = array( + $options = [ 'type' => $e->getCode(), 'message' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine(), 'isException' => true, 'exception' => $e, - ); + ]; static::handle(new Error($options)); }); @@ -89,12 +90,16 @@ public static function register() */ public static function handle(Error $error) { - $di = DI::getDefault(); - $config = $di->getShared('config'); + $di = Di::getDefault(); + $config = $di->getShared('config')->error; $type = static::getErrorType($error->type()); $message = "$type: {$error->message()} in {$error->file()} on line {$error->line()}"; - $config->error->logger->log($message); + if (isset($config->formatter) && $config->formatter instanceof Formatter) { + $config->logger->setFormatter($config->formatter); + } + + $config->logger->log($message); switch ($error->type()) { case E_WARNING: @@ -119,13 +124,13 @@ public static function handle(Error $error) $view = $di->getShared('view'); $response = $di->getShared('response'); - $dispatcher->setControllerName($config->error->controller); - $dispatcher->setActionName($config->error->action); - $dispatcher->setParams(array('error' => $error)); + $dispatcher->setControllerName($config->controller); + $dispatcher->setActionName($config->action); + $dispatcher->setParams(['error' => $error]); $view->start(); $dispatcher->dispatch(); - $view->render($config->error->controller, $config->error->action, $dispatcher->getParams()); + $view->render($config->controller, $config->action, $dispatcher->getParams()); $view->finish(); return $response->setContent($view->getContent())->send(); diff --git a/Library/Phalcon/Error/README.md b/Library/Phalcon/Error/README.md index 9e1ee98ef..7c9b304e5 100644 --- a/Library/Phalcon/Error/README.md +++ b/Library/Phalcon/Error/README.md @@ -8,14 +8,14 @@ Configuration ------------- For the error handler to work properly, following section has to be created -in the configuration file (in this case php array). All options are mandatory: +in the configuration file (in this case php array). The `logger`, `controller`, `action` options are mandatory: ```php - [ 'logger' => new \Phalcon\Logger\Adapter\File(ROOT_PATH . '/log/' . APPLICATION_ENV . '.log'), + 'formatter' => new \Phalcon\Logger\Formatter\Line('[%date%][%type%] %message%', 'Y-m-d H:i:s O'), 'controller' => 'error', 'action' => 'index', ] @@ -25,6 +25,7 @@ return [ * `logger` defines an object used for logging. It has to implement `log` method in order for error handler to work properly. +* `formatter` sets the message formatter. * `controller` is the name of error controller, which will be dispatched, when an exception or error occurs. * `action` is the name of action in the error controller, which will be called, when an exception or error @@ -34,7 +35,6 @@ In the Application file (please take a look at \Phalcon\Error\Application for re error handler has to be registered. Application must also define constants for application environments: ```php - dispatcher->getParam('error'); @@ -84,4 +83,4 @@ Error message could be displayed to the user this way:
exception()->getTraceAsString(); ?>
-``` \ No newline at end of file +```