throw d3DicException in case of errors

This commit is contained in:
Daniel Seifert 2024-02-17 22:33:41 +01:00
parent 943adfaa5e
commit afdb6ff4c1
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
3 changed files with 67 additions and 24 deletions

View File

@ -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
{

29
d3DicException.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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);
}
}

View File

@ -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__);
}