是否可以在不编辑核心的情况下更改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);
有更好的方法吗?