From 894f87bdcbad419a42ba2c5741501ee6edd34ae4 Mon Sep 17 00:00:00 2001 From: Ferran Martinez Date: Tue, 18 Apr 2017 18:30:41 +0200 Subject: [PATCH] v2 --- README.md | 52 +++++++++++++----------------- src/Config/Config.php | 75 +++++++++++++++++++++++++++++++------------ 2 files changed, 76 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index d539a15..b8b8148 100644 --- a/README.md +++ b/README.md @@ -81,43 +81,35 @@ $config->get('local.bbdd.database); You need handleErrorInterface for parse Errors Ex: ```php -class HandleError implements HandleErrorInterface -{ - - private $logPath; - private $debugMode; - - public function __construct($debugmode, $logPath = null) - { - $this->logPath = $logPath; - $this->debugMode = $debugmode; - } +$config->setupErrors($parseError, true); - public function getDebugMode() - { - return $this->debugMode; - } +$parseError = function($errtype, $errno, $errmsg, $filename, $linenum) +{ - public function parseError($errtype, $errno, $errmsg, $filename, $linenum) - { + $err = "\n"; + $err .= "\t" . date("Y-m-d H:i:s (T)") . "\n"; + $err .= "\t" . $errno . "\n"; + $err .= "\t" . $errtype . "\n"; + $err .= "\t" . $errmsg . "\n"; + $err .= "\t" . $filename . "\n"; + $err .= "\t" . $linenum . "\n"; - $err = "\n"; - $err .= "\t" . date("Y-m-d H:i:s (T)") . "\n"; - $err .= "\t" . $errno . "\n"; - $err .= "\t" . $errtype . "\n"; - $err .= "\t" . $errmsg . "\n"; - $err .= "\t" . $filename . "\n"; - $err .= "\t" . $linenum . "\n"; + $err .= "\n\n"; - $err .= "\n\n"; + error_log($err, 3, __DIR__'/errors/error.log'); - error_log($err, 3, $this->logPath . "/error.log"); + $redirect_errors = array( + E_ERROR, + E_CORE_ERROR, + E_COMPILE_ERROR + ); + if (in_array($errno, $redirect_errors)) { + return 'error'; + die; } -} -$handle = new HandleError(true, __DIR__); -$config->setupErrors($handle); +} ``` * if debugMode is true show errors * if debugMode is false parseErro is called @@ -126,7 +118,7 @@ $config->setupErrors($handle); The MIT License (MIT) -Copyright (c) 2016 +Copyright (c) 2017 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/src/Config/Config.php b/src/Config/Config.php index 2905b02..849ba1f 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -3,15 +3,27 @@ class Config { - private $configs = []; /** - * @var HandleErrorInterface + * @var array */ - private $handleError; + private $configs = []; + /** + * @var mixed + */ private $autoloads; - public function __construct($configs, $autoloads = false) + /** + * @var mixed + */ + private $callback = false; + + /** + * Config constructor. + * @param array $configs + * @param bool $autoloads + */ + public function __construct(array $configs, $autoloads = false) { $this->configs = $configs; if ($autoloads) { @@ -20,17 +32,15 @@ public function __construct($configs, $autoloads = false) } } + /** + * @param $name + * @return array|bool|mixed + */ public function get($name) { if (isset($this->configs[$name])) { return $this->configs[$name]; } elseif (strpos($name, '.') !== false) { - - if (substr($name, 0, strlen('environment.')) == 'environment.') { - $name = str_replace('environment.', 'environments.' . $this->get('environment') . '.', $name); - return $this->get($name); - } - $loc = &$this->configs; foreach (explode('.', $name) as $part) { $loc = &$loc[$part]; @@ -40,25 +50,40 @@ public function get($name) return false; } + /** + * @param $name + * @param $value + */ public function set($name, $value) { $this->configs[$name] = $value; } - public function setupErrors(HandleErrorInterface $handleError) + /** + * @param callable $callback + * @param bool $dev + */ + public function setupErrors(callable $callback, $dev = true) { - $this->handleError = $handleError; - if (!$this->handleError->getDebugMode()) { + $this->callback = $callback; + if (!$dev) { set_error_handler(array($this, "handleErrors")); register_shutdown_function(array($this, "shutdownFunction")); } - $display_errors = $this->handleError->getDebugMode() ? "1" : "0"; + $display_errors = $dev ? "1" : "0"; error_reporting(E_ALL | E_STRICT); ini_set('display_errors', $display_errors); ini_set('html_errors', $display_errors); } + /** + * @param $errno + * @param $errmsg + * @param $filename + * @param $linenum + * @return bool + */ public function handleErrors($errno, $errmsg, $filename, $linenum) { if (0 == error_reporting()) { @@ -78,18 +103,21 @@ public function handleErrors($errno, $errmsg, $filename, $linenum) E_USER_WARNING => 'User Warning', E_USER_NOTICE => 'User Notice', E_STRICT => 'Runtime Notice', - E_RECOVERABLE_ERROR => 'Catchable Fatal Error', - E_DEPRECATED => 'Deprecated', - E_USER_DEPRECATED => 'User Deprecated' + E_RECOVERABLE_ERROR => 'Catchable Fatal Error' ); $errtype = (isset($errortype[$errno])) ? $errortype[$errno] : 'Unknow'; - $this->handleError->parseError($errtype, $errno, $errmsg, $filename, $linenum); + $fn = $this->callback; + $fn($errtype, $errno, $errmsg, $filename, $linenum); + return true; } + /** + * + */ public function shutdownFunction() { $error = error_get_last(); @@ -106,12 +134,14 @@ public function shutdownFunction() E_COMPILE_ERROR => 'Fatal error (Compile Error)' ); - $this->handleError->parseError($errortypes[$error['type']], $error['type'], $error['message'], - $error['file'], $error['line']); - + $fn = $this->callback; + $fn($errortypes[$error['type']], $error['type'], $error['message'], $error['file'], $error['line']); } } + /** + * + */ private function setAutoloads() { if (!empty($this->autoloads['files'])) { @@ -124,6 +154,9 @@ private function setAutoloads() } } + /** + * @param $classname + */ private function load($classname) {