Goetas\Twital\EventSubscriber\CustomNamespaceRawSubscriber::addCustomNamespace PHP Method

addCustomNamespace() public method

public addCustomNamespace ( SourceEvent $event )
$event Goetas\Twital\EventDispatcher\SourceEvent
    public function addCustomNamespace(SourceEvent $event)
    {
        $xml = $event->getTemplate();
        $mch = null;
        if (preg_match('~<(([a-z0-9\\-_]+):)?([a-z0-9\\-_]+)~i', $xml, $mch, PREG_OFFSET_CAPTURE)) {
            $addPos = $mch[0][1] + strlen($mch[0][0]);
            foreach ($this->customNamespaces as $prefix => $ns) {
                if (!preg_match('/\\sxmlns:([a-z0-9\\-]+)="' . preg_quote($ns, '/') . '"/', $xml) && !preg_match('/\\sxmlns:([a-z0-9\\-]+)=".*?"/', $xml)) {
                    $xml = substr_replace($xml, ' xmlns:' . $prefix . '="' . $ns . '"', $addPos, 0);
                }
            }
            $event->setTemplate($xml);
        }
    }