do refactoring

This commit is contained in:
Daniel Seifert 2024-01-31 19:25:06 +01:00
parent 8cc981902b
commit f5afd4617f
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
6 changed files with 71 additions and 81 deletions

View File

@ -18,6 +18,7 @@ use Symfony\Component\DependencyInjection\Container;
/**
* @return Container
* @throws Exception
*/
function d3GetOxidDIC(): Container
{

View File

@ -22,7 +22,11 @@
"MIT"
],
"require": {
"symfony/dependency-injection": "^3.1|^5.4.11|^6.0"
"symfony/dependency-injection": "^3.1|^5.4.11|^6.0",
"beberlei/assert": "^3.3"
},
"require-dev": {
"phpunit/phpunit": "^9.6"
},
"autoload": {
"psr-4": {
@ -31,5 +35,8 @@
"files": [
"autoload/functions_oxDIC.php"
]
},
"scripts": {
"phpunit": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/"
}
}

View File

@ -28,13 +28,11 @@ use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
class d3DicHandler implements d3DicHandlerInterface
{
/**
* instance
* @var null|Container
*/
protected static ?Container $_instance = null;
protected static Container|null $_instance = null;
public static array $circularReferenceMethodNames = ['getViewConfig'];
public static array $circularReferenceMethodNames = [
'getViewConfig'
];
/**
* get instance
@ -48,14 +46,12 @@ class d3DicHandler implements d3DicHandlerInterface
$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) {
if (null == self::$_instance) {
$oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer();
}
@ -75,11 +71,9 @@ class d3DicHandler implements d3DicHandlerInterface
$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);
@ -103,8 +97,7 @@ class d3DicHandler implements d3DicHandlerInterface
public function d3GetCacheFilePath(): string
{
return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.
Registry::getConfig()->getShopId().'.php';
return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.Registry::getConfig()->getShopId().'.php';
}
/**
@ -113,10 +106,7 @@ class d3DicHandler implements d3DicHandlerInterface
public function d3GetCacheContainer(): Container
{
require_once $this->d3GetCacheFilePath();
/** @var Container $container */
$container = oxNew(d3DIContainerCache::class); /** @phpstan-ignore-line */
return $container;
return oxNew(d3DIContainerCache::class);
}
/**
@ -127,8 +117,7 @@ class d3DicHandler implements d3DicHandlerInterface
public function d3GetFileLoader(ContainerBuilder $container): YamlFileLoader
{
/** @var YamlFileLoader $fileLoader */
$fileLoader = oxNew(
YamlFileLoader::class,
$fileLoader = oxNew(YamlFileLoader::class,
$container,
oxNew(FileLocator::class, d3DicUtilities::getVendorDir())
);
@ -154,6 +143,22 @@ class d3DicHandler implements d3DicHandlerInterface
}
}
/**
* @return bool
*/
protected function isNotInTest(): bool
{
return false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST');
}
/**
* @return bool
*/
protected function cacheFileExists(): bool
{
return file_exists($this->d3GetCacheFilePath());
}
/**
* @param bool $compileAndDump
*
@ -162,16 +167,14 @@ class d3DicHandler implements d3DicHandlerInterface
*/
public function buildContainer(bool $compileAndDump = true): Container
{
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
startProfile(__METHOD__);
}
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) startProfile(__METHOD__);
$config = $this->d3GetConfig();
if ( $config->isProductiveMode()
&& ! $config->getConfigParam( 'iDebug' )
&& (! defined('OXID_PHP_UNIT') || defined('D3_MODCFG_TEST'))
&& file_exists($this->d3GetCacheFilePath())
&& $this->isNotInTest()
&& $this->cacheFileExists()
) {
$container = $this->d3GetCacheContainer();
} else {
@ -181,16 +184,14 @@ class d3DicHandler implements d3DicHandlerInterface
if ($compileAndDump) {
$container->compile();
if (! defined('OXID_PHP_UNIT')) {
if ($this->isNotInTest()) {
$dumper = new PhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(array('class' => 'd3DIContainerCache')));
}
}
}
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
stopProfile(__METHOD__);
}
if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) stopProfile(__METHOD__);
return $container;
}
@ -203,14 +204,10 @@ class d3DicHandler implements d3DicHandlerInterface
/**
* clone
*/
public function __clone()
{
}
public function __clone() {}
/**
* constructor
*/
public function __construct()
{
}
public function __construct() {}
}

View File

@ -17,10 +17,7 @@ namespace D3\DIContainerHandler;
use Symfony\Component\DependencyInjection\Container;
/**
* Interface d3DicHandlerInterface
*/
interface d3DicHandlerInterface
{
public static function getInstance(): ?Container;
public static function getInstance(): Container;
}

View File

@ -23,7 +23,7 @@ class d3DicUtilities
*
* @return string
*/
public static function getServiceId(string $classNameSpace, ?string $additional = null): string
public static function getServiceId(string $classNameSpace, string $additional = null): string
{
return strtolower(
($additional ? $additional.'.' : '').
@ -51,6 +51,6 @@ class d3DicUtilities
*/
public static function getVendorDir(): string
{
return rtrim(dirname(dirname(dirname(__FILE__))), '/').'/';
return rtrim( dirname( __FILE__, 3 ), '/') . '/';
}
}

View File

@ -15,60 +15,54 @@ declare(strict_types=1);
namespace D3\DIContainerHandler;
use InvalidArgumentException;
use Assert\Assert;
use Assert\InvalidArgumentException;
class definitionFileContainer
{
public const TYPE_YAML = 'yml';
protected array $definitionFiles = [
self::TYPE_YAML => [],
self::TYPE_YAML => []
];
protected array $allowedTypes = [
self::TYPE_YAML,
self::TYPE_YAML
];
public function __construct()
{
$this->addYamlDefinitions('d3/modcfg/Config/services.yaml');
}
/**
* @param string $definitionFile
* @param string $type
* @param $definitionFile
* @param $type
* @throws InvalidArgumentException
*
* @return void
*/
public function addDefinitions(string $definitionFile, string $type): void
public function addDefinitions($definitionFile, $type): void
{
if (!in_array($type, $this->allowedTypes)) {
throw new InvalidArgumentException('invalid definition file type');
}
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');
Assert::that(rtrim(dirname(__FILE__, 3).'/').$definitionFile)->file('invalid definition file');
$this->definitionFiles[$type][md5($definitionFile)] = $definitionFile;
}
/**
* @param string $definitionFile
*
* @return void
*/
public function addYamlDefinitions(string $definitionFile): void
public function addYamlDefinitions($definitionFile): void
{
$this->addDefinitions($definitionFile, self::TYPE_YAML);
}
/**
* @param string $type
* @param $type
*
* @return array
* @throws InvalidArgumentException
*/
public function getDefinitions(string $type): array
public function getDefinitions($type): array
{
if (!in_array($type, $this->allowedTypes)) {
throw new InvalidArgumentException('invalid definition file type');
}
Assert::that($type)->inArray($this->allowedTypes, 'invalid definition file type');
return $this->definitionFiles[$type];
}
@ -82,14 +76,8 @@ class definitionFileContainer
}
/**
* @param string $definitionFile
* @return bool
* @return array[]
*/
public function has(string $definitionFile): bool
{
return isset($this->definitionFiles[md5($definitionFile)]);
}
public function getAll(): array
{
return $this->definitionFiles;