DIContainer/d3DicHandler.php

217 lines
5.7 KiB
PHP
Raw Normal View History

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;
use d3DIContainerCache;
2022-12-28 13:31:44 +01:00
use Exception;
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
{
/**
* instance
2023-11-23 15:01:44 +01:00
* @var null|Container
2022-12-28 13:31:44 +01:00
*/
2023-11-23 15:01:44 +01:00
protected static ?Container $_instance = null;
2022-12-28 13:31:44 +01:00
2023-11-23 15:01:44 +01:00
public static array $circularReferenceMethodNames = ['getViewConfig'];
2022-12-28 13:31:44 +01:00
/**
* get instance
2023-11-23 15:01:44 +01:00
*
* @return Container
* @throws Exception
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
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
$functionName = $caller['function'];
2023-11-23 15:01:44 +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
}
2023-11-23 15:01:44 +01:00
if (null === self::$_instance) {
2022-12-28 13:31:44 +01:00
$oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer();
}
return self::$_instance;
}
/**
* get instance
2023-11-23 15:01:44 +01:00
*
* @return Container
* @throws Exception
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
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
$functionName = $caller['function'];
2023-11-23 15:01:44 +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
}
$oDicHandler = oxNew(d3DicHandler::class);
self::$_instance = $oDicHandler->buildContainer(false);
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;
}
/**
* @return Config
2022-12-28 13:31:44 +01:00
*/
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
{
2023-11-23 15:01:44 +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
* @return Container
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();
2023-11-23 15:01:44 +01:00
/** @var Container $container */
$container = oxNew(d3DIContainerCache::class); /** @phpstan-ignore-line */
return $container;
2022-12-28 13:31:44 +01:00
}
/**
2023-11-23 15:01:44 +01:00
* @param ContainerBuilder $container
*
2022-12-28 13:31:44 +01:00
* @return YamlFileLoader
*/
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 */
2023-11-23 15:05:40 +01:00
$fileLoader = oxNew(
YamlFileLoader::class,
2022-12-28 13:31:44 +01:00
$container,
oxNew(FileLocator::class, d3DicUtilities::getVendorDir())
2022-12-28 13:31:44 +01:00
);
return $fileLoader;
}
/**
2023-11-23 15:01:44 +01:00
* @param ContainerBuilder $container
*
* @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);
$fileContainer = oxNew(definitionFileContainer::class);
foreach ($fileContainer->getYamlDefinitions() as $file) {
$fullPath = d3DicUtilities::getVendorDir().$file;
2022-12-28 13:31:44 +01:00
if (is_file($fullPath)) {
$loader->load($file);
}
}
}
/**
* @param bool $compileAndDump
2023-11-23 15:01:44 +01:00
*
* @return Container
* @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
{
2023-11-23 15:05:40 +01:00
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
startProfile(__METHOD__);
}
2022-12-28 13:31:44 +01:00
$config = $this->d3GetConfig();
2023-11-23 15:05:40 +01:00
if ($config->isProductiveMode()
&& ! $config->getConfigParam('iDebug')
&& (! defined('OXID_PHP_UNIT') || defined('D3_MODCFG_TEST'))
2022-12-28 13:31:44 +01:00
&& file_exists($this->d3GetCacheFilePath())
) {
$container = $this->d3GetCacheContainer();
} else {
$container = $this->getContainerBuilder();
$this->loadFiles($container);
if ($compileAndDump) {
$container->compile();
2023-11-23 15:05:40 +01:00
if (! defined('OXID_PHP_UNIT')) {
2022-12-28 13:31:44 +01:00
$dumper = new PhpDumper($container);
2023-11-23 15:05:40 +01:00
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
2022-12-28 13:31:44 +01:00
}
}
}
2023-11-23 15:05:40 +01:00
if (Registry::get(ConfigFile::class)->getVar('iDebug')) {
stopProfile(__METHOD__);
}
2022-12-28 13:31:44 +01:00
return $container;
}
2023-11-23 15:01:44 +01:00
public function getContainerBuilder(): ContainerBuilder
2022-12-28 13:31:44 +01:00
{
return oxNew(ContainerBuilder::class);
}
/**
* clone
*/
2023-11-23 15:05:40 +01:00
public function __clone()
{
}
2022-12-28 13:31:44 +01:00
/**
* constructor
*/
2023-11-23 15:05:40 +01:00
public function __construct()
{
}
2022-12-28 13:31:44 +01:00
}