Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
XmlParsingException example
if
(
!\
extension_loaded
(
'dom'
)
)
{
throw
new
\
LogicException
(
'Extension DOM is required.'
)
;
}
$internalErrors
=
libxml_use_internal_errors
(
true
)
;
libxml_clear_errors
(
)
;
$dom
=
new
\
DOMDocument
(
)
;
$dom
->validateOnParse = true;
if
(
!
$dom
->
loadXML
(
$content
, \LIBXML_NONET | \LIBXML_COMPACT
)
)
{
throw
new
XmlParsingException
(
implode
(
"\n",
static
::
getXmlErrors
(
$internalErrors
)
)
)
;
}
$dom
->
normalizeDocument
(
)
;
libxml_use_internal_errors
(
$internalErrors
)
;
foreach
(
$dom
->childNodes
as
$child
)
{
if
(
\XML_DOCUMENT_TYPE_NODE ===
$child
->nodeType
)
{
throw
new
XmlParsingException
(
'Document types are not allowed.'
)
;
}
}
public
static
function
createFromXmlFile
(
string
$xmlFile
)
: self
{
Feature::
triggerDeprecationOrThrow
(
'v6.6.0.0',
Feature::
deprecatedMethodMessage
(
self::
class
, __METHOD__, 'v6.6.0.0', '\Shopware\Core\Framework\App\Flow\Action\Action'
)
)
;
try
{
$doc
= XmlUtils::
loadFile
(
$xmlFile
, self::XSD_FILE
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFile
,
$e
->
getMessage
(
)
)
;
}
$actions
=
$doc
->
getElementsByTagName
(
'flow-actions'
)
->
item
(
0
)
;
$actions
=
$actions
=== null ? null : Actions::
fromXml
(
$actions
)
;
return
new
self
(
\
dirname
(
$xmlFile
)
,
$actions
)
;
}
public
function
getPath
(
)
: string
{
Feature::
triggerDeprecationOrThrow
(
$cookies
=
$doc
->
getElementsByTagName
(
'cookies'
)
->
item
(
0
)
;
$cookies
=
$cookies
=== null ? null : Cookies::
fromXml
(
$cookies
)
;
$payments
=
$doc
->
getElementsByTagName
(
'payments'
)
->
item
(
0
)
;
$payments
=
$payments
=== null ? null : Payments::
fromXml
(
$payments
)
;
$ruleConditions
=
$doc
->
getElementsByTagName
(
'rule-conditions'
)
->
item
(
0
)
;
$ruleConditions
=
$ruleConditions
=== null ? null : RuleConditions::
fromXml
(
$ruleConditions
)
;
$storefront
=
$doc
->
getElementsByTagName
(
'storefront'
)
->
item
(
0
)
;
$storefront
=
$storefront
=== null ? null : Storefront::
fromXml
(
$storefront
)
;
$tax
=
$doc
->
getElementsByTagName
(
'tax'
)
->
item
(
0
)
;
$tax
=
$tax
=== null ? null : Tax::
fromXml
(
$tax
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFile
,
$e
->
getMessage
(
)
)
;
}
return
new
self
(
\
dirname
(
$xmlFile
)
,
$metadata
,
$setup
,
$admin
,
$permissions
,
$allowedHosts
,
$customFields
,
$webhooks
,
public
function
__construct
(
private
string
$path
,
private
readonly ?Entities
$entities
)
{
}
public
static
function
createFromXmlFile
(
string
$xmlFile
)
: self
{
try
{
$doc
= XmlUtils::
loadFile
(
$xmlFile
, self::XSD_FILE
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFile
,
$e
->
getMessage
(
)
)
;
}
$entities
=
$doc
->
getElementsByTagName
(
'entities'
)
->
item
(
0
)
;
$entities
=
$entities
=== null ? null : Entities::
fromXml
(
$entities
)
;
return
new
self
(
\
dirname
(
$xmlFile
)
,
$entities
)
;
}
public
function
getPath
(
)
: string
{
return
$this
->path;
}
private
function
__construct
(
private
readonly AdminUi
$adminUi
)
{
}
public
static
function
createFromXmlFile
(
string
$xmlFilePath
)
: self
{
try
{
$doc
= XmlUtils::
loadFile
(
$xmlFilePath
, self::XSD_FILEPATH
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFilePath
,
$e
->
getMessage
(
)
)
;
}
/** @var \DOMElement $domItem */
$domItem
=
$doc
->
getElementsByTagName
(
'admin-ui'
)
->
item
(
0
)
;
return
new
self
(
AdminUi::
fromXml
(
$domItem
)
)
;
}
public
function
getAdminUi
(
)
: ?AdminUi
{
return
$this
->adminUi;
}
/** * load and validate xml file - parse to array * * @throws XmlParsingException */
public
function
read
(
string
$xmlFile
)
: array
{
try
{
$dom
= XmlUtils::
loadFile
(
$xmlFile
,
$this
->xsdFile
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFile
,
$e
->
getMessage
(
)
)
;
}
return
$this
->
parseFile
(
$dom
)
;
}
public
static
function
getAllChildren
(
\DOMNode
$node
)
: array
{
$children
=
[
]
;
foreach
(
$node
->childNodes
as
$child
)
{
if
(
$child
instanceof \DOMElement
)
{
$children
[
]
=
$child
;
}
private
function
__construct
(
private
string
$path
,
private
readonly ?Blocks
$blocks
)
{
}
public
static
function
createFromXmlFile
(
string
$xmlFile
)
: self
{
try
{
$doc
= XmlUtils::
loadFile
(
$xmlFile
, self::XSD_FILE
)
;
}
catch
(
\Exception
$e
)
{
throw
new
XmlParsingException
(
$xmlFile
,
$e
->
getMessage
(
)
)
;
}
$blocks
=
$doc
->
getElementsByTagName
(
'blocks'
)
->
item
(
0
)
;
$blocks
=
$blocks
=== null ? null : Blocks::
fromXml
(
$blocks
)
;
return
new
self
(
\
dirname
(
$xmlFile
)
,
$blocks
)
;
}
public
function
getPath
(
)
: string
{
return
$this
->path;
}