Skip to content

Commit

Permalink
v2
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsmartinez committed Apr 18, 2017
1 parent 918158e commit 894f87b
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 51 deletions.
52 changes: 22 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<errorentry>\n";
$err .= "\t<datetime>" . date("Y-m-d H:i:s (T)") . "</datetime>\n";
$err .= "\t<errornum>" . $errno . "</errornum>\n";
$err .= "\t<errortype>" . $errtype . "</errortype>\n";
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";

$err = "<errorentry>\n";
$err .= "\t<datetime>" . date("Y-m-d H:i:s (T)") . "</datetime>\n";
$err .= "\t<errornum>" . $errno . "</errornum>\n";
$err .= "\t<errortype>" . $errtype . "</errortype>\n";
$err .= "\t<errormsg>" . $errmsg . "</errormsg>\n";
$err .= "\t<scriptname>" . $filename . "</scriptname>\n";
$err .= "\t<scriptlinenum>" . $linenum . "</scriptlinenum>\n";
$err .= "</errorentry>\n\n";

$err .= "</errorentry>\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
Expand All @@ -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:

Expand Down
75 changes: 54 additions & 21 deletions src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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];
Expand All @@ -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()) {
Expand All @@ -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();
Expand All @@ -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'])) {
Expand All @@ -124,6 +154,9 @@ private function setAutoloads()
}
}

/**
* @param $classname
*/
private function load($classname)
{

Expand Down

0 comments on commit 894f87b

Please sign in to comment.