diff --git a/src/FuzeWorks/Configurator.php b/src/FuzeWorks/Configurator.php index 824c6cf..af22039 100644 --- a/src/FuzeWorks/Configurator.php +++ b/src/FuzeWorks/Configurator.php @@ -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) diff --git a/src/FuzeWorks/Core.php b/src/FuzeWorks/Core.php index f6a983e..ce0c8e5 100644 --- a/src/FuzeWorks/Core.php +++ b/src/FuzeWorks/Core.php @@ -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'); + } } diff --git a/src/FuzeWorks/Logger.php b/src/FuzeWorks/Logger.php index 36a6c17..7b19595 100644 --- a/src/FuzeWorks/Logger.php +++ b/src/FuzeWorks/Logger.php @@ -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); } /** diff --git a/src/FuzeWorks/Plugins.php b/src/FuzeWorks/Plugins.php index d75bdcd..a77cd98 100644 --- a/src/FuzeWorks/Plugins.php +++ b/src/FuzeWorks/Plugins.php @@ -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 diff --git a/test/plugins/TestDisabledPlugin/TestInvalidClass.php b/test/plugins/TestDisabledPlugin/plugin.testinvalidclass.php similarity index 100% rename from test/plugins/TestDisabledPlugin/TestInvalidClass.php rename to test/plugins/TestDisabledPlugin/plugin.testinvalidclass.php diff --git a/test/plugins/TestInvalidClass/TestInvalidClass.php b/test/plugins/TestInvalidClass/plugin.testinvalidclass.php similarity index 100% rename from test/plugins/TestInvalidClass/TestInvalidClass.php rename to test/plugins/TestInvalidClass/plugin.testinvalidclass.php diff --git a/test/plugins/TestLoadHeader/loadHeader/Plug.php b/test/plugins/TestLoadHeader/loadHeader/plugin.plug.php similarity index 100% rename from test/plugins/TestLoadHeader/loadHeader/Plug.php rename to test/plugins/TestLoadHeader/loadHeader/plugin.plug.php diff --git a/test/plugins/TestLoadHeaderNotIPluginHeader/TestLoadPlugin.php b/test/plugins/TestLoadHeaderNotIPluginHeader/plugin.testloadplugin.php similarity index 100% rename from test/plugins/TestLoadHeaderNotIPluginHeader/TestLoadPlugin.php rename to test/plugins/TestLoadHeaderNotIPluginHeader/plugin.testloadplugin.php diff --git a/test/plugins/TestLoadPlugin/TestLoadPlugin.php b/test/plugins/TestLoadPlugin/plugin.testloadplugin.php similarity index 100% rename from test/plugins/TestLoadPlugin/TestLoadPlugin.php rename to test/plugins/TestLoadPlugin/plugin.testloadplugin.php diff --git a/test/plugins/TestReloadPlugin/TestReloadPlugin.php b/test/plugins/TestReloadPlugin/plugin.testreloadplugin.php similarity index 100% rename from test/plugins/TestReloadPlugin/TestReloadPlugin.php rename to test/plugins/TestReloadPlugin/plugin.testreloadplugin.php