commit 150fe1861a3b1918be1d2beee37bdc83c91e2483 Author: Daniel Seifert Date: Wed Dec 28 13:31:44 2022 +0100 initial diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..df524f8 --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,22 @@ +The MIT License (MIT) + +Copyright (c) 2021 D3 Data Development (Inh. Thomas Dartsch) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + diff --git a/README.md b/README.md new file mode 100644 index 0000000..3d22bb5 --- /dev/null +++ b/README.md @@ -0,0 +1,23 @@ +Bridges for OXID services without bridges +----------------------------------------- + +Provides bridges for OXID services that are not reliably listed in the DIC cache. + +## Install + +`composer require d3/oxidservicebridges` + +## Licence of this software (d3/oxidservicebridges) [MIT] + +(06.05.2021) + +``` +Copyright (c) D3 Data Development (Inh. Thomas Dartsch) + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +``` \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..cd1117d --- /dev/null +++ b/composer.json @@ -0,0 +1,33 @@ +{ + "name": "d3/dicontainerhandler", + "description": "Provides bridges for OXID services that are not reliably listed in the DIC cache.", + "type": "library", + "keywords": [ + "oxid", + "eShop", + "services", + "bridges", + "d3" + ], + "authors": [ + { + "name": "D3 Data Development (Inh. Thomas Dartsch)", + "email": "info@shopmodule.com", + "homepage": "https://www.d3data.de", + "role": "Owner" + } + ], + "homepage": "https://www.oxidmodule.com/", + "license": [ + "MIT" + ], + "require": { + "php": "^7.0 || ^8.0", + "oxid-esales/oxideshop-ce": "6.5 - 6.13" + }, + "autoload": { + "psr-4": { + "D3\\DIContainerHandler\\": "./" + } + } +} diff --git a/d3DicHandler.php b/d3DicHandler.php new file mode 100644 index 0000000..2f4f197 --- /dev/null +++ b/d3DicHandler.php @@ -0,0 +1,248 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DIContainerHandler; + +use D3\ModCfg\Application\Model\Modulemetadata\d3moduleconfiguration; +use d3CacheContainer; +use Exception; +use OxidEsales\Eshop\Core\Module\ModuleList; +use OxidEsales\Eshop\Core\Registry; +use OxidEsales\Eshop\Core\Request; +use OxidEsales\Facts\Config\ConfigFile; +use Symfony\Component\Config\FileLocator; +use Symfony\Component\DependencyInjection\ContainerBuilder; +use Symfony\Component\DependencyInjection\Dumper\PhpDumper; +use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; + +class d3DicHandler implements d3DicHandlerInterface +{ + /** + * instance + * @var array + */ + protected static $_instance; + + public static $circularReferenceMethodNames = array( + 'getViewConfig' + ); + + /** + * get instance + * @return ContainerBuilder + */ + public static function getInstance() + { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $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 (null == self::$_instance) { + $oDicHandler = oxNew(d3DicHandler::class); + self::$_instance = $oDicHandler->buildContainer(); + } + + return self::$_instance; + } + + /** + * get instance + * @return ContainerBuilder + */ + public static function getUncompiledInstance() + { + $trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); + $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"); + } + + $oDicHandler = oxNew(d3DicHandler::class); + self::$_instance = $oDicHandler->buildContainer(false); + + return self::$_instance; + } + + public static function removeInstance() + { + self::$_instance = null; + } + + /** + * @return mixed + */ + public function d3GetConfig() + { + return Registry::getConfig(); + } + + public function d3GetCacheFilePath() + { + return $this->d3GetConfig()->getConfigParam('sCompileDir').'/d3DicContainer_'.Registry::getConfig()->getShopId().'.php'; + } + + /** + * @return d3CacheContainer|object + */ + public function d3GetCacheContainer() + { + require_once $this->d3GetCacheFilePath(); + return oxNew(d3CacheContainer::class); + } + + /** + * @param $container + * @return YamlFileLoader + */ + public function d3GetFileLoader($container) + { + /** @var YamlFileLoader $fileLoader */ + $fileLoader = oxNew(YamlFileLoader::class, + $container, + oxNew(FileLocator::class, $this->d3GetConfig()->getModulesDir()) + ); + + return $fileLoader; + } + + /** + * @param $container + */ + public function loadFiles($container) + { + $loader = $this->d3GetFileLoader($container); + + foreach ($this->getShopDefinitionFiles() as $file) { + $fullPath = $this->d3GetConfig()->getModulesDir().$file; + if (is_file($fullPath)) { + $loader->load($file); + } + } + } + + /** + * @param bool $compileAndDump + * @return ContainerBuilder + */ + public function buildContainer(bool $compileAndDump = true) + { + if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) startProfile(__METHOD__); + + $config = $this->d3GetConfig(); + + if ($config->isProductiveMode() + && false == $config->getConfigParam('iDebug') + && (false == defined('OXID_PHP_UNIT') || true == defined('D3_MODCFG_TEST')) + && file_exists($this->d3GetCacheFilePath()) + ) { + $container = $this->d3GetCacheContainer(); + } else { + $container = $this->getContainerBuilder(); + $this->loadFiles($container); + + if ($compileAndDump) { + $container->compile(); + + if (false == defined('OXID_PHP_UNIT')) { + $dumper = new PhpDumper($container); + file_put_contents($this->d3GetCacheFilePath(), $dumper->dump(array('class' => 'd3CacheContainer'))); + } + } + } + + if ((bool) Registry::get( ConfigFile::class)->getVar( 'iDebug')) stopProfile(__METHOD__); + + return $container; + } + + public function getContainerBuilder() + { + return oxNew(ContainerBuilder::class); + } + + /** + * @return array + */ + public function getShopDefinitionFiles() + { + return $this->getDefinitionFiles('d3DICDefinitionFiles'); + } + + /** + * @param $sMetaDataIdent + * @return array + */ + public function getDefinitionFiles($sMetaDataIdent) + { + $aDICDefintionFileList = []; + + /** @var ModuleList $oModuleList */ + $oModuleList = oxNew(ModuleList::class); + $aActiveModuleIdList = array_keys($oModuleList->getActiveModuleInfo()); + + if (Registry::get(Request::class)->getRequestEscapedParameter('cl') === 'module_main' + && ($sCurrentModuleId = Registry::get(Request::class)->getRequestEscapedParameter('oxid')) + && false === in_array($sCurrentModuleId, $aActiveModuleIdList) + ) { + $aActiveModuleIdList[] = $sCurrentModuleId; + } + + foreach ($aActiveModuleIdList as $sModuleId) { + $oModuleConfiguration = oxNew(d3moduleconfiguration::class); + $aDICDefintionFiles = $oModuleConfiguration->getInfo($sMetaDataIdent, $sModuleId); + if (isset($aDICDefintionFiles) && is_array($aDICDefintionFiles)) { + $aDICDefintionFileList = array_merge($aDICDefintionFileList, $aDICDefintionFiles); + } + } + + $aBaseServiceIds = array( + 'd3/modcfg' + ); + + $aDefFileList = array(); + + foreach ($aDICDefintionFileList as $sKey => $sFileName) { + foreach ($aBaseServiceIds as $sBaseKey => $sBaseClass) { + if (strstr(strtolower($sFileName), strtolower($sBaseClass))) { + $aDefFileList[$sBaseKey] = $sFileName; + unset($aDICDefintionFileList[$sKey]); + } + } + } + + ksort($aDefFileList, SORT_NUMERIC); + $aDefFileList = array_merge($aDefFileList, $aDICDefintionFileList); + + return $aDefFileList; + } + + /** + * clone + */ + public function __clone() {} + + /** + * constructor + */ + public function __construct() {} +} diff --git a/d3DicHandlerInterface.php b/d3DicHandlerInterface.php new file mode 100644 index 0000000..f81c1d7 --- /dev/null +++ b/d3DicHandlerInterface.php @@ -0,0 +1,11 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DIContainerHandler; + +class d3DicUtilities +{ + /** + * @param $classNameSpace + * @param bool $additional + * @return string + */ + public static function getServiceId($classNameSpace, $additional = false) + { + return strtolower( + ($additional ? $additional.'.' : ''). + $classNameSpace + ); + } + + /** + * @param $classNameSpace + * @param $argumentName + * @return string + */ + public static function getArgumentId($classNamespace, $argumentName) + { + return strtolower( + $classNamespace. + '.args.' . + $argumentName + ); + } +}