b374k
m1n1 1.01
Apache/2.4.41 (Ubuntu)
Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64
uid=33(www-data) gid=33(www-data) groups=33(www-data)
server ip : 62.171.164.128 | your ip : 127.0.0.1
safemode OFF
 >  / home / a / home / dev2.destoffenstraat.com / app / Firebear / ImportExport / Model / Output /
Filename/home/a/home/dev2.destoffenstraat.com/app/Firebear/ImportExport/Model/Output/Xslt.php
Size4.68 kb
Permissionrw-rw-r--
Ownerroot : root
Create time21-Aug-2025 12:26
Last modified07-Nov-2022 06:44
Last accessed23-Aug-2025 01:35
Actionsedit | rename | delete | download (gzip)
Viewtext | code | image
<?php
/*
* @copyright: Copyright © 2018 Firebear Studio. All rights reserved.
* @author : Firebear Studio <fbeardev@gmail.com>
*/
declare(strict_types=1);

namespace Firebear\ImportExport\Model\Output;

use DOMDocument;
use Exception;
use Firebear\ImportExport\Exception\XmlException;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\DomDocument\DomDocumentFactory;
use Magento\Framework\Event\ManagerInterface;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\XsltProcessor\XsltProcessorFactory;
use XSLTProcessor;

class Xslt
{
const XML_PATH_HANDLE_PROCESSOR_ERRORS = 'firebear_importexport/xslt/handle_processor_errors';
const EVENT_KEY = 'firebear_xslt_';

/**
* @var array|null
*/
protected $restrictedPHPFunction = null;

/**
* @var ManagerInterface
*/
protected $eventManager;

/**
* @var DomDocumentFactory
*/
private $domDocumentFactory;

/**
* @var XsltProcessorFactory
*/
private $xsltProcessorFactory;

/**
* @var ScopeConfigInterface
*/
private $scopeConfig;

/**
* Xslt constructor.
* @param DomDocumentFactory $domDocumentFactory
* @param XsltProcessorFactory $xsltProcessorFactory
* @param ScopeConfigInterface $scopeConfig
* @param ManagerInterface $eventManager
* @param array $data
*/
public function __construct(
DomDocumentFactory $domDocumentFactory,
XsltProcessorFactory $xsltProcessorFactory,
ScopeConfigInterface $scopeConfig,
ManagerInterface $eventManager,
array $data = []
) {
$this->domDocumentFactory = $domDocumentFactory;
$this->xsltProcessorFactory = $xsltProcessorFactory;
$this->scopeConfig = $scopeConfig;
$this->restrictedPHPFunction = isset($data['restrict_php_functions']) ?: null;
$this->eventManager = $eventManager;
}

/**
* @param string $file
* @param string $xsl
* @return false|string
* @throws LocalizedException
* @throws XmlException
*/
public function convert(string $file, string $xsl)
{
if (!class_exists('\XSLTProcessor')) {
throw new LocalizedException(__(
'The XSLTProcessor class could not be found. This means your PHP installation is missing XSL features.'
));
}
$result = '';
$useErrors = libxml_use_internal_errors(false);
if ($this->getScopeConfig()->isSetFlag(self::XML_PATH_HANDLE_PROCESSOR_ERRORS)) {
$useErrors = libxml_use_internal_errors(true);
}

try {
/**
* Load Original XML Content
*/
$xmlDoc = $this->getDom();
$xmlDoc->loadXML($file, LIBXML_COMPACT | LIBXML_PARSEHUGE | LIBXML_NOWARNING);

/**
* Load XSLT template
*/
$xslDoc = $this->getDom();
$xslDoc->loadXML($xsl, LIBXML_COMPACT | LIBXML_PARSEHUGE | LIBXML_NOWARNING);

$proc = $this->getXSLTProcessor();
$proc->registerPHPFunctions($this->restrictedPHPFunction);
$proc->importStylesheet($xslDoc);

$this->eventManager->dispatch(
self::EVENT_KEY . 'before_transform_to_doc',
[
'adapter' => $this,
'xmlDoc' => $xmlDoc,
'xslDoc' => $xslDoc
]
);

$newDom = $proc->transformToDoc($xmlDoc);

$this->eventManager->dispatch(
self::EVENT_KEY . 'after_transform_to_doc',
[
'adapter' => $this,
'xmlDoc' => $xmlDoc,
'xslDoc' => $xslDoc,
'newDom' => $newDom
]
);

if ($this->scopeConfig->isSetFlag(self::XML_PATH_HANDLE_PROCESSOR_ERRORS)
&& libxml_get_errors()
) {
throw new XmlException(libxml_get_errors());
}

$result = $newDom->saveXML();
} catch (Exception $e) {
throw new XmlException($e->getMessage());
} finally {
libxml_clear_errors();
libxml_use_internal_errors($useErrors);
}

return $result;
}

/**
* @return ScopeConfigInterface
*/
public function getScopeConfig()
{
return $this->scopeConfig;
}

/**
* @return DOMDocument
*/
public function getDom()
{
return $this->domDocumentFactory->create();
}

/**
* @return XSLTProcessor
*/
public function getXSLTProcessor()
{
return $this->xsltProcessorFactory->create();
}
}