/** * Returns true if and only if $value meets the validation requirements * * If $value fails validation, then this method returns false, and * getMessages() will return an array of messages that explain why the * validation failed. * * @param string $value * @param string $schema * @param bool $isSecurityCheck * @return bool */ public function isValid($value, $schema = self::LAYOUT_SCHEMA_PAGE_HANDLE, $isSecurityCheck = true) { try { //wrap XML value in the "layout" and "handle" tags to make it validatable $value = '<layout xmlns:xsi="' . self::XML_NAMESPACE_XSI . '">' . $value . '</layout>'; $this->_domConfigFactory->createDom(['xml' => $value, 'schemaFile' => $this->_xsdSchemas[$schema]]); if ($isSecurityCheck) { $value = new \Magento\Framework\Simplexml\Element($value); $value->registerXPathNamespace('xsi', self::XML_NAMESPACE_XSI); foreach ($this->_protectedExpressions as $key => $xpr) { if ($value->xpath($xpr)) { $this->_error($key); } } $errors = $this->getMessages(); if (!empty($errors)) { return false; } } } catch (\Magento\Framework\Config\Dom\ValidationException $e) { $this->_error(self::XML_INVALID, $e->getMessage()); return false; } catch (\Exception $e) { $this->_error(self::XML_INVALID); return false; } return true; }
/** * Constructor * * @param string $source * @param \Magento\Framework\Config\Dom\UrnResolver $urnResolver * @param \Magento\Framework\Config\DomFactory $domFactory * @throws \Magento\Framework\Exception\LocalizedException */ public function __construct($source, \Magento\Framework\Config\Dom\UrnResolver $urnResolver, \Magento\Framework\Config\DomFactory $domFactory) { $this->urnResolver = $urnResolver; $this->domFactory = $domFactory; $dom = $this->domFactory->createDom(['xml' => $source, 'schemaFile' => $this->getSchemaFile()]); $this->_data = $this->_extractData($dom->getDom()); }
/** * Get Dom configuration model * * @return \Magento\Framework\Config\Dom * @throws \Magento\Framework\Config\Dom\ValidationException */ protected function _getDomConfigModel() { if (null === $this->_domConfig) { $this->_domConfig = $this->domFactory->createDom(['xml' => $this->_getInitialXml(), 'idAttributes' => $this->_getIdAttributes(), 'schemaFile' => $this->getPerFileSchemaFile()]); } return $this->_domConfig; }
/** * Get persistent XML config xpath * * @return \DOMXPath * @throws \Magento\Framework\Model\Exception */ protected function _getConfigDomXPath() { if (is_null($this->_configDomXPath)) { $filePath = $this->_modulesDirectory->getRelativePath($this->_configFilePath); $isFile = $this->_modulesDirectory->isFile($filePath); $isReadable = $this->_modulesDirectory->isReadable($filePath); if (!$isFile || !$isReadable) { throw new \Magento\Framework\Model\Exception(__('We cannot load the configuration from file %1.', $this->_configFilePath)); } $xml = $this->_modulesDirectory->readFile($filePath); /** @var \Magento\Framework\Config\Dom $configDom */ $configDom = $this->_domFactory->createDom(array('xml' => $xml, 'idAttributes' => array('config/instances/blocks/reference' => 'id'), 'schemaFile' => $this->_moduleReader->getModuleDir('etc', 'Magento_Persistent') . '/persistent.xsd')); $this->_configDomXPath = new \DOMXPath($configDom->getDom()); } return $this->_configDomXPath; }
/** * Get persistent XML config xpath * * @return \DOMXPath * @throws \Magento\Framework\Exception\LocalizedException */ protected function _getConfigDomXPath() { if ($this->_configDomXPath === null) { $dir = explode("/", $this->_configFilePath); array_pop($dir); $dir = implode("/", $dir); $directoryRead = $this->readFactory->create($dir); $filePath = $directoryRead->getRelativePath($this->_configFilePath); $isFile = $directoryRead->isFile($filePath); $isReadable = $directoryRead->isReadable($filePath); if (!$isFile || !$isReadable) { throw new \Magento\Framework\Exception\LocalizedException(__('We cannot load the configuration from file %1.', $this->_configFilePath)); } $xml = $directoryRead->readFile($filePath); /** @var \Magento\Framework\Config\Dom $configDom */ $configDom = $this->_domFactory->createDom(['xml' => $xml, 'idAttributes' => ['config/instances/blocks/reference' => 'id'], 'schemaFile' => $this->_moduleReader->getModuleDir(Dir::MODULE_ETC_DIR, 'Magento_Persistent') . '/persistent.xsd']); $this->_configDomXPath = new \DOMXPath($configDom->getDom()); } return $this->_configDomXPath; }