DIContainer/d3DicHandler.php

182 lines
5.0 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
{
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
/**
* @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-05-27 13:41:52 +02:00
return oxNew(d3DicHandler::class)->createInstance();
2022-12-28 13:31:44 +01:00
}
/**
* 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-05-27 13:41:52 +02:00
return oxNew(d3DicHandler::class)->createInstance(false);
}
public static function removeInstance(): void
{
self::$_instance = null;
}
2022-12-28 13:31:44 +01:00
2024-05-27 13:41:52 +02:00
public function createInstance(bool $compiled = true): Container
{
try {
$functionName = $this->getFunctionNameFromTrace();
2024-02-17 22:33:41 +01:00
if (in_array(strtolower($functionName), array_map('strtolower', self::$circularReferenceMethodNames))) {
2024-05-27 13:41:52 +02:00
throw oxNew(Exception::class, 'method ' . $functionName . " can't use DIC due the danger of circular reference");
2024-02-17 22:33:41 +01:00
}
2022-12-28 13:31:44 +01:00
2024-05-27 13:41:52 +02:00
if (null == self::$_instance) {
self::$_instance = $this->buildContainer($compiled);
}
2024-02-17 22:33:41 +01:00
} catch (Exception $exception) {
throw new d3DicException($exception);
}
2022-12-28 13:31:44 +01:00
return self::$_instance;
}
2024-05-27 13:41:52 +02:00
protected function getFunctionNameFromTrace()
2022-12-28 13:31:44 +01:00
{
2024-05-27 13:41:52 +02:00
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
$caller = $trace[1];
return $caller['function'];
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
{
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,
oxNew(FileLocator::class, d3DicUtilities::getVendorDir())
2022-12-28 13:31:44 +01:00
);
return $fileLoader;
}
/**
* @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);
$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);
}
}
}
2024-01-31 19:25:06 +01:00
protected function cacheFileExists(): bool
{
return file_exists($this->d3GetCacheFilePath());
}
2022-12-28 13:31:44 +01: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
{
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-05-27 13:41:52 +02:00
$dumper = $this->getPhpDumper($container);
file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(['class' => 'd3DIContainerCache']));
2022-12-28 13:31:44 +01: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')
2024-04-25 13:29:58 +02:00
&& $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-05-27 13:41:52 +02:00
public function getPhpDumper(ContainerBuilder $containerBuilder): PhpDumper
2024-01-31 21:17:51 +01:00
{
2024-05-27 13:41:52 +02:00
return new PhpDumper( $containerBuilder);
2024-01-31 21:17:51 +01:00
}
2022-12-28 13:31:44 +01:00
}