Compare commits

...

4 Commits

9 changed files with 78 additions and 59 deletions

View File

@ -3,11 +3,11 @@
# Dependency Injection Container handler for OXID eShop
Enables the simple use of a class container outside of the OXID DI Services.
Enables the simple use of a class container outside the OXID DI Services.
## Install
This package requires an Composer installed OXID eShop as defined in [composer.json](composer.json).
This package requires a Composer installed OXID eShop as defined in [composer.json](composer.json).
Open a command line interface and navigate to the shop root directory (parent of source and vendor). Execute the following command. Adapt the paths to your environment.
@ -31,7 +31,7 @@ Then empty the TMP folder.
## Changelog
See [CHANGELOG](CHANGELOG.md) for further informations.
See [CHANGELOG](CHANGELOG.md) for further information.
## Contributing

View File

@ -13,11 +13,12 @@
declare(strict_types=1);
use D3\DIContainerHandler\d3DicException;
use D3\DIContainerHandler\d3DicHandler;
use Symfony\Component\DependencyInjection\Container;
/**
* @throws Exception
* @throws d3DicException
*/
function d3GetOxidDIC(): Container
{

View File

@ -47,6 +47,7 @@
"phpstan-report": "./vendor/bin/phpstan --configuration=vendor/d3/ordermanager/phpstan.neon analyse --error-format=json > vendor/d3/ordermanager/tests/phpstan.report.json",
"phpunit": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/",
"phpunit-coverage": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/ --coverage-html=vendor/d3/oxid-dic-handler/tests/result/coverage",
"rector": "./vendor/bin/rector process --dry-run --config ./vendor/d3/oxid-dic-handler/rector.php"
}

26
d3DicException.php Normal file
View File

@ -0,0 +1,26 @@
<?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
{
public function __construct(Exception $previous)
{
parent::__construct($previous->getMessage(), $previous->getCode(), $previous);
}
}

View File

@ -36,22 +36,25 @@ class d3DicHandler implements d3DicHandlerInterface
/**
* get instance
*
* @throws Exception
* @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 +62,25 @@ class d3DicHandler implements d3DicHandlerInterface
/**
* get instance
*
* @throws Exception
* @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;
}
@ -142,17 +148,11 @@ class d3DicHandler implements d3DicHandlerInterface
*/
public function buildContainer(bool $compileAndDump = true): Container
{
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
startProfile(__METHOD__);
}
$config = $this->d3GetConfig();
if ($config->isProductiveMode()
&& ! $config->getConfigParam('iDebug')
&& $this->isNotInTest()
&& $this->cacheFileExists()
) {
if ($this->d3UseCachedContainer()) {
$container = $this->d3GetCacheContainer();
} else {
$container = $this->getContainerBuilder();
@ -168,29 +168,35 @@ class d3DicHandler implements d3DicHandlerInterface
}
}
if ((bool) Registry::get(ConfigFile::class)->getVar('iDebug')) {
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
stopProfile(__METHOD__);
}
return $container;
}
protected function d3UseCachedContainer(): bool
{
$config = $this->d3GetConfig();
return $config->isProductiveMode()
&& !$config->getConfigParam('iDebug')
&& $this->isNotInTest()
&& $this->cacheFileExists();
}
public function getContainerBuilder(): ContainerBuilder
{
return oxNew(ContainerBuilder::class);
}
/**
* clone
*/
public function __clone()
{
/** keep clear */
}
/**
* constructor
*/
public function __construct()
{
/** keep clear */
}
}

View File

@ -16,7 +16,6 @@ declare(strict_types=1);
namespace D3\DIContainerHandler;
use Assert\Assert;
use Assert\InvalidArgumentException;
class definitionFileContainer
{
@ -32,6 +31,7 @@ class definitionFileContainer
public function __construct()
{
/** keep clear */
}
public function addDefinitions(string $definitionFile, string $type): void

View File

@ -2,27 +2,10 @@
declare(strict_types=1);
use Rector\CodeQuality\Rector\Class_\CompleteDynamicPropertiesRector;
use Rector\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector;
use Rector\CodeQuality\Rector\Foreach_\ForeachItemsAssignToEmptyArrayToAssignRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\Assign\RemoveDoubleAssignRector;
use Rector\DeadCode\Rector\If_\RemoveUnusedNonEmptyArrayBeforeForeachRector;
use Rector\DeadCode\Rector\StaticCall\RemoveParentCallWithoutParentRector;
use Rector\Php80\Rector\FunctionLike\MixedTypeRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
use Rector\TypeDeclaration\Rector\ClassMethod\AddVoidReturnTypeWhereNoReturnRector;
use Rector\TypeDeclaration\Rector\ClassMethod\BoolReturnTypeFromStrictScalarReturnsRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ParamTypeByMethodCallTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromReturnDirectArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictBoolReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictNewArrayRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictScalarReturnExprRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnTypeFromStrictTypedCallRector;
use Rector\TypeDeclaration\Rector\ClassMethod\ReturnUnionTypeRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictArrayParamDimFetchRector;
use Rector\TypeDeclaration\Rector\ClassMethod\StrictStringParamConcatRector;
return static function (RectorConfig $rectorConfig): void {
$rectorConfig->paths([

View File

@ -30,6 +30,8 @@ class functions_oxDICTest extends TestCase
*/
public function d3GetOxidDICTest(): void
{
error_reporting(E_ALL & ~E_NOTICE & ~E_WARNING);
$this->assertInstanceOf(
ContainerBuilder::class,
d3GetOxidDIC()

View File

@ -35,7 +35,7 @@ class d3DicUtilitiesTest extends TestCase
* @covers \D3\DIContainerHandler\d3DicUtilities::getServiceId
* @dataProvider getServiceIdTestDataProvider
*/
public function getServiceIdTest(string $className, string $additional = null, string $expected): void
public function getServiceIdTest(string $className, ?string $additional, string $expected): void
{
$sut = oxNew(d3DicUtilities::class);