PHP 对象配置 XML
PHP Object Config XML
我正在使用单个 XML 文件来存储多个对象的配置设置。例如:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
我希望我的每个对象都能访问 xml 的不同方面;例如 "database" class 将访问数据库元素。
可以使用什么方法在不复制代码的情况下跨对象快速访问此配置?
你可以创建一个 Config
对象,它从 XML 文件中读取配置,然后在你想要的 类 上使用 Configurable
特征 "automatically" 可从 XML 配置(参见下面的示例)。
config.xml:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
PHP代码:
class Config
{
/**
* Full XML configuration
*
* @var SimpleXMLElement
*/
private static $xml;
/**
* Parse XML configuration file into XML object (once)
*/
public function __construct()
{
if (!self::$xml) {
$xmlSource = file_get_contents('config.xml');
self::$xml = new SimpleXMLElement($xmlSource);
}
}
/**
* Returns the first node matching the specified xpath
*
* @param $xpath
*
* @return SimpleXMLElement
*/
public function getFirstByXpath($xpath)
{
// Return the first matching configuration
return self::$xml->xpath($xpath)[0];
}
}
trait Configurable
{
/**
* Class configuration XML element
*
* @var SimpleXMLElement
*/
private $configuration;
/**
* Get the class configuration XML element
*
* @return SimpleXMLElement
*/
function getConfiguration()
{
if (!$this->configuration) {
$tag = strtolower(get_class($this));
$xpath = '/config/' . $tag;
$this->configuration = (new Config())->getFirstByXpath($xpath);
}
return $this->configuration;
}
}
class application
{
use configurable;
}
class database
{
use configurable;
}
$app = new application();
echo "App component 1: ", $app->getConfiguration()->component1, PHP_EOL;
$db = new database();
echo "DB user: ", $db->getConfiguration()->user, PHP_EOL;
输出:
App component 1: mycomponent
DB user: myuser
我正在使用单个 XML 文件来存储多个对象的配置设置。例如:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
我希望我的每个对象都能访问 xml 的不同方面;例如 "database" class 将访问数据库元素。
可以使用什么方法在不复制代码的情况下跨对象快速访问此配置?
你可以创建一个 Config
对象,它从 XML 文件中读取配置,然后在你想要的 类 上使用 Configurable
特征 "automatically" 可从 XML 配置(参见下面的示例)。
config.xml:
<config>
<database>
<user>myuser</user>
</database>
<application>
<component1>mycomponent</component1>
</application>
<infrastructure>
<hostname1>server1</hostname1>
</infrastructure>
</config>
PHP代码:
class Config
{
/**
* Full XML configuration
*
* @var SimpleXMLElement
*/
private static $xml;
/**
* Parse XML configuration file into XML object (once)
*/
public function __construct()
{
if (!self::$xml) {
$xmlSource = file_get_contents('config.xml');
self::$xml = new SimpleXMLElement($xmlSource);
}
}
/**
* Returns the first node matching the specified xpath
*
* @param $xpath
*
* @return SimpleXMLElement
*/
public function getFirstByXpath($xpath)
{
// Return the first matching configuration
return self::$xml->xpath($xpath)[0];
}
}
trait Configurable
{
/**
* Class configuration XML element
*
* @var SimpleXMLElement
*/
private $configuration;
/**
* Get the class configuration XML element
*
* @return SimpleXMLElement
*/
function getConfiguration()
{
if (!$this->configuration) {
$tag = strtolower(get_class($this));
$xpath = '/config/' . $tag;
$this->configuration = (new Config())->getFirstByXpath($xpath);
}
return $this->configuration;
}
}
class application
{
use configurable;
}
class database
{
use configurable;
}
$app = new application();
echo "App component 1: ", $app->getConfiguration()->component1, PHP_EOL;
$db = new database();
echo "DB user: ", $db->getConfiguration()->user, PHP_EOL;
输出:
App component 1: mycomponent
DB user: myuser