8
0
Fork 0
Dieser Commit ist enthalten in:
Daniel Seifert 2024-01-31 19:25:06 +01:00
Ursprung 8cc981902b
Commit f5afd4617f
Signiert von: DanielS
GPG-Schlüssel-ID: 8A7C4C6ED1915C6F
6 geänderte Dateien mit 71 neuen und 81 gelöschten Zeilen

Datei anzeigen

@ -17,7 +17,8 @@ use D3\DIContainerHandler\d3DicHandler;
use Symfony\Component\DependencyInjection\Container; use Symfony\Component\DependencyInjection\Container;
/** /**
* @return Container * @return Container
* @throws Exception
*/ */
function d3GetOxidDIC(): Container function d3GetOxidDIC(): Container
{ {

Datei anzeigen

@ -22,7 +22,11 @@
"MIT" "MIT"
], ],
"require": { "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": { "autoload": {
"psr-4": { "psr-4": {
@ -31,5 +35,8 @@
"files": [ "files": [
"autoload/functions_oxDIC.php" "autoload/functions_oxDIC.php"
] ]
},
"scripts": {
"phpunit": "XDEBUG_MODE=coverage ./vendor/bin/phpunit --bootstrap=source/bootstrap.php --config=vendor/d3/oxid-dic-handler/tests/"
} }
} }

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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

Datei anzeigen

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