CommunityID/libs/Zend/Config/Writer/Xml.php

200 lines
6.1 KiB
PHP
Raw Normal View History

2019-07-17 20:08:50 +00:00
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
2019-07-17 20:16:19 +00:00
* @version $Id: Xml.php 14267 2009-03-10 15:49:03Z dasprid $
2019-07-17 20:08:50 +00:00
*/
/**
* @see Zend_Config_Writer
*/
require_once 'Zend/Config/Writer.php';
/**
* @category Zend
* @package Zend_Config
* @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Config_Writer_Xml extends Zend_Config_Writer
{
/**
* Filename to write to
*
* @var string
*/
protected $_filename = null;
2019-07-17 20:16:19 +00:00
/**
* Wether to exclusively lock the file or not
*
* @var boolean
*/
protected $_exclusiveLock = false;
2019-07-17 20:08:50 +00:00
/**
* Set the target filename
*
* @param string $filename
* @return Zend_Config_Writer_Xml
*/
public function setFilename($filename)
{
$this->_filename = $filename;
return $this;
}
2019-07-17 20:16:19 +00:00
/**
* Set wether to exclusively lock the file or not
*
* @param boolean $exclusiveLock
* @return Zend_Config_Writer_Array
*/
public function setExclusiveLock($exclusiveLock)
{
$this->_exclusiveLock = $exclusiveLock;
return $this;
}
2019-07-17 20:08:50 +00:00
/**
* Defined by Zend_Config_Writer
*
* @param string $filename
* @param Zend_Config $config
2019-07-17 20:16:19 +00:00
* @param boolean $exclusiveLock
2019-07-17 20:08:50 +00:00
* @throws Zend_Config_Exception When filename was not set
* @throws Zend_Config_Exception When filename is not writable
* @return void
*/
2019-07-17 20:16:19 +00:00
public function write($filename = null, Zend_Config $config = null, $exclusiveLock = null)
2019-07-17 20:08:50 +00:00
{
if ($filename !== null) {
$this->setFilename($filename);
}
if ($config !== null) {
$this->setConfig($config);
}
2019-07-17 20:16:19 +00:00
if ($exclusiveLock !== null) {
$this->setExclusiveLock($exclusiveLock);
}
2019-07-17 20:08:50 +00:00
if ($this->_filename === null) {
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('No filename was set');
}
if ($this->_config === null) {
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('No config was set');
}
$xml = new SimpleXMLElement('<zend-config/>');
$extends = $this->_config->getExtends();
$sectionName = $this->_config->getSectionName();
if (is_string($sectionName)) {
$child = $xml->addChild($sectionName);
2019-07-17 20:16:19 +00:00
$this->_addBranch($this->_config, $child, $xml);
2019-07-17 20:08:50 +00:00
} else {
foreach ($this->_config as $sectionName => $data) {
if (!($data instanceof Zend_Config)) {
2019-07-17 20:16:19 +00:00
$xml->addChild($sectionName, (string) $data);
} else {
$child = $xml->addChild($sectionName);
if (isset($extends[$sectionName])) {
$child->addAttribute('extends', $extends[$sectionName]);
}
$this->_addBranch($data, $child, $xml);
2019-07-17 20:08:50 +00:00
}
}
}
$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->formatOutput = true;
$xmlString = $dom->saveXML();
2019-07-17 20:16:19 +00:00
$flags = 0;
if ($this->_exclusiveLock) {
$flags |= LOCK_EX;
}
$result = @file_put_contents($this->_filename, $xmlString, $flags);
2019-07-17 20:08:50 +00:00
if ($result === false) {
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Could not write to file "' . $this->_filename . '"');
}
}
/**
* Add a branch to an XML object recursively
*
* @param Zend_Config $config
* @param SimpleXMLElement $xml
2019-07-17 20:16:19 +00:00
* @param SimpleXMLElement $parent
2019-07-17 20:08:50 +00:00
* @return void
*/
2019-07-17 20:16:19 +00:00
protected function _addBranch(Zend_Config $config, SimpleXMLElement $xml, SimpleXMLElement $parent)
2019-07-17 20:08:50 +00:00
{
2019-07-17 20:16:19 +00:00
$branchType = null;
2019-07-17 20:08:50 +00:00
foreach ($config as $key => $value) {
2019-07-17 20:16:19 +00:00
if ($branchType === null) {
if (is_numeric($key)) {
$branchType = 'numeric';
$branchName = $xml->getName();
$xml = $parent;
unset($parent->{$branchName});
} else {
$branchType = 'string';
}
} else if ($branchType !== (is_numeric($key) ? 'numeric' : 'string')) {
require_once 'Zend/Config/Exception.php';
throw new Zend_Config_Exception('Mixing of string and numeric keys is not allowed');
}
if ($branchType === 'numeric') {
if ($value instanceof Zend_Config) {
$child = $parent->addChild($branchName, (string) $value);
$this->_addBranch($value, $child, $parent);
} else {
$parent->addChild($branchName, (string) $value);
}
} else {
if ($value instanceof Zend_Config) {
$child = $xml->addChild($key);
$this->_addBranch($value, $child, $xml);
} else {
$xml->addChild($key, (string) $value);
}
2019-07-17 20:08:50 +00:00
}
}
}
}