From afdb6ff4c101224a7f11874fbfa569dea0ba3b58 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Sat, 17 Feb 2024 22:33:41 +0100 Subject: [PATCH] throw d3DicException in case of errors --- autoload/functions_oxDIC.php | 4 ++- d3DicException.php | 29 ++++++++++++++++++ d3DicHandler.php | 58 ++++++++++++++++++++++-------------- 3 files changed, 67 insertions(+), 24 deletions(-) create mode 100644 d3DicException.php diff --git a/autoload/functions_oxDIC.php b/autoload/functions_oxDIC.php index 71ffaaf..4ba4eff 100644 --- a/autoload/functions_oxDIC.php +++ b/autoload/functions_oxDIC.php @@ -13,11 +13,13 @@ declare(strict_types=1); +use D3\DIContainerHandler\d3DicException; use D3\DIContainerHandler\d3DicHandler; use Symfony\Component\DependencyInjection\Container; /** - * @throws Exception + * @return Container + * @throws d3DicException */ function d3GetOxidDIC(): Container { diff --git a/d3DicException.php b/d3DicException.php new file mode 100644 index 0000000..466de63 --- /dev/null +++ b/d3DicException.php @@ -0,0 +1,29 @@ + + * @link https://www.oxidmodule.com + */ + +declare(strict_types=1); + +namespace D3\DIContainerHandler; + +use Exception; + +class d3DicException extends Exception +{ + /** + * @param Exception $previous + */ + public function __construct(Exception $previous) + { + parent::__construct($previous->getMessage(), $previous->getCode(), $previous); + } +} \ No newline at end of file diff --git a/d3DicHandler.php b/d3DicHandler.php index 9d33a6f..3a0783b 100644 --- a/d3DicHandler.php +++ b/d3DicHandler.php @@ -36,22 +36,26 @@ class d3DicHandler implements d3DicHandlerInterface /** * get instance - * - * @throws Exception + * @return Container + * @throws d3DicException */ public static function getInstance(): Container { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $caller = $trace[1]; - $functionName = $caller['function']; + try { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $caller = $trace[1]; + $functionName = $caller['function']; - if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { - throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); - } + if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { + throw oxNew(Exception::class, 'method ' . $functionName . " can't use DIC due the danger of circular reference"); + } - if (null == self::$_instance) { - $oDicHandler = oxNew(d3DicHandler::class); - self::$_instance = $oDicHandler->buildContainer(); + if (null == self::$_instance) { + $oDicHandler = oxNew(d3DicHandler::class); + self::$_instance = $oDicHandler->buildContainer(); + } + } catch (Exception $exception) { + throw new d3DicException($exception); } return self::$_instance; @@ -59,22 +63,26 @@ class d3DicHandler implements d3DicHandlerInterface /** * get instance - * - * @throws Exception + * @return Container + * @throws d3DicException */ public static function getUncompiledInstance(): Container { - $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); - $caller = $trace[1]; - $functionName = $caller['function']; + try { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $caller = $trace[1]; + $functionName = $caller['function']; - if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { - throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); + if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) { + throw oxNew(Exception::class, 'method '.$functionName." can't use DIC due the danger of circular reference"); + } + + $oDicHandler = oxNew(d3DicHandler::class); + self::$_instance = $oDicHandler->buildContainer(false); + } catch (Exception $exception) { + throw new d3DicException($exception); } - $oDicHandler = oxNew(d3DicHandler::class); - self::$_instance = $oDicHandler->buildContainer(false); - return self::$_instance; } @@ -112,6 +120,8 @@ class d3DicHandler implements d3DicHandlerInterface } /** + * @param ContainerBuilder $container + * @return void * @throws Exception */ public function loadFiles(ContainerBuilder $container): void @@ -138,11 +148,13 @@ class d3DicHandler implements d3DicHandlerInterface } /** + * @param bool $compileAndDump + * @return Container * @throws Exception */ public function buildContainer(bool $compileAndDump = true): Container { - if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) { + if (Registry::get(ConfigFile::class)->getVar('iDebug')) { startProfile(__METHOD__); } @@ -168,7 +180,7 @@ class d3DicHandler implements d3DicHandlerInterface } } - if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) { + if (Registry::get(ConfigFile::class)->getVar('iDebug')) { stopProfile(__METHOD__); }