\ Zend \ Config \ Writer \ Xml - 启动元素

时间:2016-12-25 13:58:05

标签: php zend-framework2

是否可以在不编辑核心的情况下更改XMLWriter中使用的\Zend\Config\Writer\Xml::processConfig的startElement?

修改 我是这样做的:

namespace Test\Config\Writer;

use XMLWriter;
use Zend\Config\Exception;

class Xml extends \Zend\Config\Writer\Xml
{
    private $_startElement = "zend-config";

    /**
     * processConfig(): defined by AbstractWriter.
     *
     * @param  array $config
     * @return string
     */
    public function processConfig(array $config)
    {
        $writer = new XMLWriter();
        $writer->openMemory();
        $writer->setIndent(true);
        $writer->setIndentString(str_repeat(' ', 4));

        $writer->startDocument('1.0', 'UTF-8');
        $writer->startElement($this->_startElement);

        foreach ($config as $sectionName => $data) {
            if (!is_array($data)) {
                $writer->writeElement($sectionName, (string) $data);
            } else {
                $this->addBranch($sectionName, $data, $writer);
            }
        }

        $writer->endElement();
        $writer->endDocument();

        return $writer->outputMemory();
    }

    public function setStartElement($value) {
        $this->_startElement = $value;
    }
}

然后我称之为:

 $config = new \Zend\Config\Config(array(), true);
 $config->command = $commands;
 $writer = new \Test\Config\Writer\Xml();
 $writer->setStartElement("test");
 $writer->toFile($filePath, $config);

有更好的方法吗?

0 个答案:

没有答案