Implemented all changes requested by FuzeWorks\Application.

- Implemented a exception and error handler which other methods register to. These are executed in the order provided by the Priority class
- Logger now uses the $isEnabled property to determine if it is enabled. $print_to_screen now has its own methods.
- Plugins now use a 'plugin.pluginName.php' file by default
-
This commit is contained in:
Abel Hoogeveen 2019-03-04 21:18:08 +01:00
parent e9eb858a2d
commit 68c2aaa210
No known key found for this signature in database
GPG Key ID: 96C2234920BF4292
10 changed files with 151 additions and 8 deletions

View File

@ -351,7 +351,13 @@ class Configurator
$container = Core::init();
Logger::newLevel("Creating container...");
if ($debug == true)
{
define('ENVIRONMENT', 'DEVELOPMENT');
Logger::enable();
}
else
define('ENVIRONMENT', 'PRODUCTION');
// Load components
foreach ($this->components as $componentSuperClass => $component)

View File

@ -70,6 +70,20 @@ class Core
public static $logDir;
/**
* Array of exception handlers, sorted by priority
*
* @var array
*/
protected static $exceptionHandlers = [];
/**
* Array of error handlers, sorted by priority
*
* @var array
*/
protected static $errorHandlers = [];
/**
* Initializes the core.
*
@ -92,6 +106,8 @@ class Core
// Load basics
ignore_user_abort(true);
register_shutdown_function(array('\FuzeWorks\Core', 'shutdown'));
set_error_handler(array('\FuzeWorks\Core', 'errorHandler'), E_ALL);
set_exception_handler(array('\FuzeWorks\Core', 'exceptionHandler'));
// Return the Factory
return new Factory();
@ -146,6 +162,93 @@ class Core
return (PHP_SAPI === 'cli' OR defined('STDIN'));
}
public static function exceptionHandler()
{
for ($i = Priority::getHighestPriority(); $i <= Priority::getLowestPriority(); $i++)
{
if (!isset(self::$exceptionHandlers[$i]))
continue;
foreach (self::$exceptionHandlers[$i] as $handler)
call_user_func_array($handler, func_get_args());
}
}
public static function errorHandler()
{
for ($i = Priority::getHighestPriority(); $i <= Priority::getLowestPriority(); $i++)
{
if (!isset(self::$errorHandlers[$i]))
continue;
foreach (self::$errorHandlers[$i] as $handler)
call_user_func_array($handler, func_get_args());
}
}
/**
* Add an exception handler to be called when an exception occurs
*
* @param callable $callback
* @param int $priority
*/
public static function addExceptionHandler(callable $callback, int $priority = Priority::NORMAL)
{
if (!isset(self::$exceptionHandlers[$priority]))
self::$exceptionHandlers[$priority] = [];
if (!in_array($callback, self::$exceptionHandlers[$priority]))
self::$exceptionHandlers[$priority][] = $callback;
}
/**
* Remove an exception handler from the list
*
* @param callable $callback
* @param int $priority
*/
public static function removeExceptionHandler(callable $callback, int $priority = Priority::NORMAL)
{
if (isset(self::$exceptionHandlers[$priority]) && in_array($callback, self::$exceptionHandlers[$priority]))
{
foreach (self::$exceptionHandlers[$priority] as $i => $_callback)
if ($callback == $_callback)
unset(self::$exceptionHandlers[$priority][$i]);
}
}
/**
* Add an error handler to be called when an error occurs
*
* @param callable $callback
* @param int $priority
*/
public static function addErrorHandler(callable $callback, int $priority = Priority::NORMAL)
{
if (!isset(self::$errorHandlers[$priority]))
self::$errorHandlers[$priority] = [];
if (!in_array($callback, self::$errorHandlers[$priority]))
self::$errorHandlers[$priority][] = $callback;
}
/**
* Remove an error handler from the list
*
* @param callable $callback
* @param int $priority
*/
public static function removeErrorHandler(callable $callback, int $priority = Priority::NORMAL)
{
if (isset(self::$errorHandlers[$priority]) && in_array($callback, self::$errorHandlers[$priority]))
{
foreach (self::$errorHandlers[$priority] as $i => $_callback)
if ($callback == $_callback)
unset(self::$errorHandlers[$priority][$i]);
}
}
/**
* Tests for file writability
*
@ -189,4 +292,14 @@ class Core
fclose($fp);
return TRUE;
}
/**
* Whether the current environment is a production environment
*
* @return bool
*/
public static function isProduction(): bool
{
return (ENVIRONMENT === 'PRODUCTION');
}
}

View File

@ -66,6 +66,13 @@ class Logger {
*/
private static $print_to_screen = false;
/**
* Whether the Logger has been enabled or not
*
* @var bool
*/
private static $isEnabled = false;
/**
* whether to output the log of the last entire request to a file after FuzeWorks has run.
*
@ -137,7 +144,7 @@ class Logger {
*/
public static function enable()
{
self::$print_to_screen = true;
self::$isEnabled = true;
}
/**
@ -145,7 +152,7 @@ class Logger {
*/
public static function disable()
{
self::$print_to_screen = false;
self::$isEnabled = false;
}
/**
@ -153,7 +160,24 @@ class Logger {
*/
public static function isEnabled(): bool
{
return self::$print_to_screen;
return self::$isEnabled;
}
/**
* Enable outputting the debugger after the request has been processed
*/
public static function enableScreenLog()
{
if (!Core::isProduction())
self::$print_to_screen = true;
}
/**
* Disable outputting the debugger after the request has been processed
*/
public static function disableScreenLog()
{
self::$print_to_screen = false;
}
/**
@ -164,8 +188,8 @@ class Logger {
*/
public static function enableHandlers()
{
set_error_handler(array('\FuzeWorks\Logger', 'errorHandler'), E_ALL);
set_exception_handler(array('\FuzeWorks\Logger', 'exceptionHandler'));
Core::addErrorHandler(['\FuzeWorks\Logger', 'errorHandler'], Priority::NORMAL);
Core::addExceptionHandler(['\FuzeWorks\Logger', 'exceptionHandler'], Priority::NORMAL);
}
/**
@ -176,8 +200,8 @@ class Logger {
*/
public static function disableHandlers()
{
restore_error_handler();
restore_exception_handler();
Core::removeErrorHandler(['\FuzeWorks\Logger', 'errorHandler'], Priority::NORMAL);
Core::removeExceptionHandler(['\FuzeWorks\Logger', 'exceptionHandler'], Priority::NORMAL);
}
/**

View File

@ -266,7 +266,7 @@ class Plugins
// @todo Find a more reliable method for determining header directory
$headerReflection = new ReflectionClass( get_class($header) );
$directory = dirname($headerReflection->getFileName());
$classFile = (isset($header->classFile) ? $directory.DS.$header->classFile : $directory.DS.$pluginName.".php");
$classFile = (isset($header->classFile) ? $directory.DS.$header->classFile : $directory.DS.'plugin.'.strtolower($pluginName).".php");
$className = (isset($header->className) ? $header->className : '\Application\Plugin\\'.$pluginName);
// Try to access the file