2022-12-28 13:31:44 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2023-01-03 10:49:19 +01:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
2022-12-28 13:31:44 +01:00
|
|
|
* https://www.d3data.de
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
2023-01-03 10:49:19 +01:00
|
|
|
* @link https://www.oxidmodule.com
|
2022-12-28 13:31:44 +01:00
|
|
|
*/
|
|
|
|
|
2023-01-03 10:49:19 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2022-12-28 13:31:44 +01:00
|
|
|
namespace D3\DIContainerHandler;
|
|
|
|
|
2022-12-30 23:05:33 +01:00
|
|
|
use d3DIContainerCache;
|
2022-12-28 13:31:44 +01:00
|
|
|
use Exception;
|
2023-07-26 11:56:01 +02:00
|
|
|
use OxidEsales\Eshop\Core\Config;
|
2022-12-28 13:31:44 +01:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
|
|
|
use OxidEsales\Facts\Config\ConfigFile;
|
|
|
|
use Symfony\Component\Config\FileLocator;
|
2023-11-23 15:01:44 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Container;
|
2022-12-28 13:31:44 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
|
|
use Symfony\Component\DependencyInjection\Dumper\PhpDumper;
|
|
|
|
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
|
|
|
|
|
|
|
|
class d3DicHandler implements d3DicHandlerInterface
|
|
|
|
{
|
2024-01-31 19:25:06 +01:00
|
|
|
protected static Container|null $_instance = null;
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-01-31 19:25:06 +01:00
|
|
|
public static array $circularReferenceMethodNames = [
|
2024-01-31 21:17:51 +01:00
|
|
|
'getViewConfig',
|
2024-01-31 19:25:06 +01:00
|
|
|
];
|
2022-12-28 13:31:44 +01:00
|
|
|
|
|
|
|
/**
|
2024-05-26 00:14:19 +02:00
|
|
|
* @return Container
|
2024-02-17 22:33:41 +01:00
|
|
|
* @throws d3DicException
|
2022-12-28 13:31:44 +01:00
|
|
|
*/
|
2023-11-23 15:01:44 +01:00
|
|
|
public static function getInstance(): Container
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
2024-02-17 22:33:41 +01:00
|
|
|
try {
|
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
|
|
|
$caller = $trace[1];
|
|
|
|
$functionName = $caller['function'];
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-02-17 22:33:41 +01:00
|
|
|
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");
|
|
|
|
}
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-02-17 22:33:41 +01:00
|
|
|
if (null == self::$_instance) {
|
|
|
|
$oDicHandler = oxNew(d3DicHandler::class);
|
|
|
|
self::$_instance = $oDicHandler->buildContainer();
|
|
|
|
}
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw new d3DicException($exception);
|
2022-12-28 13:31:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get instance
|
2024-02-17 22:33:41 +01:00
|
|
|
* @throws d3DicException
|
2022-12-28 13:31:44 +01:00
|
|
|
*/
|
2023-11-23 15:01:44 +01:00
|
|
|
public static function getUncompiledInstance(): Container
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
2024-02-17 22:33:41 +01:00
|
|
|
try {
|
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
|
|
|
|
$caller = $trace[1];
|
|
|
|
$functionName = $caller['function'];
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-02-17 22:33:41 +01:00
|
|
|
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");
|
|
|
|
}
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-02-17 22:33:41 +01:00
|
|
|
$oDicHandler = oxNew(d3DicHandler::class);
|
|
|
|
self::$_instance = $oDicHandler->buildContainer(false);
|
|
|
|
} catch (Exception $exception) {
|
|
|
|
throw new d3DicException($exception);
|
|
|
|
}
|
2022-12-28 13:31:44 +01:00
|
|
|
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public static function removeInstance(): void
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
self::$_instance = null;
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public function d3GetConfig(): Config
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
return Registry::getConfig();
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public function d3GetCacheFilePath(): string
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
2024-01-31 19:25:06 +01:00
|
|
|
return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.Registry::getConfig()->getShopId().'.php';
|
2022-12-28 13:31:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public function d3GetCacheContainer(): Container
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
require_once $this->d3GetCacheFilePath();
|
2024-01-31 19:25:06 +01:00
|
|
|
return oxNew(d3DIContainerCache::class);
|
2022-12-28 13:31:44 +01:00
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public function d3GetFileLoader(ContainerBuilder $container): YamlFileLoader
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
/** @var YamlFileLoader $fileLoader */
|
2024-01-31 21:17:51 +01:00
|
|
|
$fileLoader = oxNew(
|
|
|
|
YamlFileLoader::class,
|
2022-12-28 13:31:44 +01:00
|
|
|
$container,
|
2023-07-26 11:56:01 +02:00
|
|
|
oxNew(FileLocator::class, d3DicUtilities::getVendorDir())
|
2022-12-28 13:31:44 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $fileLoader;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-05-26 00:14:19 +02:00
|
|
|
* @param ContainerBuilder $container
|
|
|
|
* @return void
|
2023-11-23 15:01:44 +01:00
|
|
|
* @throws Exception
|
2022-12-28 13:31:44 +01:00
|
|
|
*/
|
2023-11-23 15:01:44 +01:00
|
|
|
public function loadFiles(ContainerBuilder $container): void
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
$loader = $this->d3GetFileLoader($container);
|
|
|
|
|
2022-12-30 23:05:33 +01:00
|
|
|
$fileContainer = oxNew(definitionFileContainer::class);
|
|
|
|
foreach ($fileContainer->getYamlDefinitions() as $file) {
|
2023-07-26 11:56:01 +02:00
|
|
|
$fullPath = d3DicUtilities::getVendorDir().$file;
|
2022-12-28 13:31:44 +01:00
|
|
|
if (is_file($fullPath)) {
|
|
|
|
$loader->load($file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 19:25:06 +01:00
|
|
|
protected function isNotInTest(): bool
|
|
|
|
{
|
|
|
|
return false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST');
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function cacheFileExists(): bool
|
|
|
|
{
|
|
|
|
return file_exists($this->d3GetCacheFilePath());
|
|
|
|
}
|
|
|
|
|
2022-12-28 13:31:44 +01:00
|
|
|
/**
|
2024-05-26 00:14:19 +02:00
|
|
|
* @param bool $compileAndDump
|
|
|
|
* @return Container
|
2023-11-23 15:01:44 +01:00
|
|
|
* @throws Exception
|
2022-12-28 13:31:44 +01:00
|
|
|
*/
|
2023-11-23 15:01:44 +01:00
|
|
|
public function buildContainer(bool $compileAndDump = true): Container
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
2024-05-26 00:15:19 +02:00
|
|
|
startProfile(__METHOD__);
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-04-25 13:29:58 +02:00
|
|
|
if ($this->d3UseCachedContainer()) {
|
2022-12-28 13:31:44 +01:00
|
|
|
$container = $this->d3GetCacheContainer();
|
|
|
|
} else {
|
|
|
|
$container = $this->getContainerBuilder();
|
|
|
|
$this->loadFiles($container);
|
|
|
|
|
|
|
|
if ($compileAndDump) {
|
|
|
|
$container->compile();
|
|
|
|
|
2024-01-31 19:25:06 +01:00
|
|
|
if ($this->isNotInTest()) {
|
2022-12-28 13:31:44 +01:00
|
|
|
$dumper = new PhpDumper($container);
|
2024-01-31 21:17:51 +01:00
|
|
|
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
|
2022-12-28 13:31:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-26 00:15:19 +02:00
|
|
|
stopProfile(__METHOD__);
|
2022-12-28 13:31:44 +01:00
|
|
|
|
|
|
|
return $container;
|
|
|
|
}
|
|
|
|
|
2024-04-25 13:29:58 +02:00
|
|
|
protected function d3UseCachedContainer(): bool
|
|
|
|
{
|
|
|
|
$config = $this->d3GetConfig();
|
|
|
|
|
|
|
|
return $config->isProductiveMode()
|
|
|
|
&& !$config->getConfigParam('iDebug')
|
|
|
|
&& $this->isNotInTest()
|
|
|
|
&& $this->cacheFileExists();
|
|
|
|
}
|
|
|
|
|
2023-11-23 15:01:44 +01:00
|
|
|
public function getContainerBuilder(): ContainerBuilder
|
2022-12-28 13:31:44 +01:00
|
|
|
{
|
|
|
|
return oxNew(ContainerBuilder::class);
|
|
|
|
}
|
|
|
|
|
2024-01-31 21:17:51 +01:00
|
|
|
public function __clone()
|
|
|
|
{
|
2024-02-19 08:36:41 +01:00
|
|
|
/** keep clear */
|
2024-01-31 21:17:51 +01:00
|
|
|
}
|
2022-12-28 13:31:44 +01:00
|
|
|
|
2024-01-31 21:17:51 +01:00
|
|
|
public function __construct()
|
|
|
|
{
|
2024-02-19 08:36:41 +01:00
|
|
|
/** keep clear */
|
2024-01-31 21:17:51 +01:00
|
|
|
}
|
2022-12-28 13:31:44 +01:00
|
|
|
}
|