Jarves\Configuration\Bundle::exportFileBased PHP Method

exportFileBased() public method

public exportFileBased ( string $property ) : string
$property string
return string
    public function exportFileBased($property)
    {
        $xmlFile = $this->getPropertyFilePath($property);
        $doc = new \DOMDocument();
        $doc->formatOutput = true;
        $doc->preserveWhiteSpace = false;
        if (file_exists($path = $this->getJarves()->getRootDir() . '/../' . $xmlFile)) {
            $doc->load($path);
        } else {
            $configElement = $doc->createElement('config');
            $doc->appendChild($configElement);
            $bundleElement = $doc->createElement('bundle');
            $configElement->appendChild($bundleElement);
        }
        $xpath = new \DOMXPath($doc);
        $element = null;
        $elements = $xpath->query('/config/bundle/' . $property);
        if (0 < $elements->length) {
            $element = $elements->item(0);
        }
        if ($element) {
            $newNode = $this->appendXmlProperty($property, $element->parentNode, false);
            if ($newNode instanceof \DOMElement) {
                $element->parentNode->replaceChild($newNode, $element);
            }
        } else {
            $elements = $xpath->query('/config/bundle');
            if (0 < $elements->length) {
                $element = $elements->item(0);
            }
            $this->appendXmlProperty($property, $element, false);
        }
        $xml = $doc->saveXML();
        $xml = substr($xml, strlen('<?xml version="1.0"?>') + 1);
        $xml = trim($xml);
        return $xml;
    }

Usage Example

Exemplo n.º 1
0
    /**
     * @param Bundle $bundle
     * @param $property
     * @return bool
     */
    public function saveFileBased(Bundle $bundle, $property)
    {
        $xml = $bundle->exportFileBased($property);
        $xmlFile = $bundle->getPropertyFilePath($property);
        $emptyXml = '<config>
  <bundle/>
</config>';
        if ($xml == $emptyXml) {
            if ($this->localFilesystem->has($xmlFile)) {
                return $this->localFilesystem->delete($xmlFile);
            } else {
                return true;
            }
        } else {
            return $this->localFilesystem->write($xmlFile, $xml);
        }
    }