PHP将配置映射文件移动到XML,但是如何创建动态XPATH [英] PHP Moving config mapping file to XML but how to I create a dynamic XPATH

查看:101
本文介绍了PHP将配置映射文件移动到XML,但是如何创建动态XPATH的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个PHP文件,我用来定义所有的常量,但现在想要使用XML的灵活性。

So I have a PHP file that I use to define all my constants but now want the flexibility of using a XML instead.

示例PHP配置文件

define("LOGIN_URL", "secure_login.php");
define("REDIRECT", "redirect.php");

define("MAPPED_VALUE_GREEN", "object_green");
define("MAPPED_VALUE_KEY", "object_key_12345");

我要做的是:

<scriptNameConfig>
  <urls>
     <url alias="LOGIN_URL" scriptname="secure_login.php" location="optional/path/to/file"/>
     <url alias="REDIRECTL" scriptname="redirect.php" location="optional/path/to/file"/>
  </urls>
  <mapping>
     <mapped name="MAPPED_VALUE_GREEN" data="object_green"/>
     <mapped name="MAPPED_VALUE_KEY" data="object_key_12345"/>
  </mapping>
</scriptNameConfig>

现在这没关系,但我想使用XPATH从这种类型的配置文件中提取值,

Now this is okay but I want to use XPATH to extract the values from this type of config file but would like it to be dynamic enough so I don't have to hard code the functions

这是我的基类

class ParseXMLConfig {
    protected $xml;

    public function __construct($xml) {
        if(is_file($xml)) {
            $this->xml = simplexml_load_file($xml);
        } else {
            $this->xml = simplexml_load_string($xml);
        }
    }
}

class ParseURLS extends ParseXMLConfig {

public function getUrlArr($url_alias) {
        $attr = false;
        $el = $this->xml->xpath("//url[@alias='$url_alias']");
        if($el && count($el) === 1) {
            $attr = (array) $el[0]->attributes();
            $attr = $attr['@attributes'];
        } 
        return $attr; // this will return the element array with all attributes
    }
}

但问题是如果我想在confie文件中引入一个新值,我必须为XPATH添加一些函数来解析它。想知道是否有人有如何去做这个通用/动态的想法,所以在XML配置文件中添加/更改或删除元素/属性将更容易,更少的硬编码。

But the problem is if I wanted to introduce a new value in the confie file I have to add some sort of function for XPATH to parse it. wanted to know if anyone has an idea on how to go about doing this generic/dynamic so adding/changing or removing elements/attributes in the XML config file would be easier and less hard coding.

感谢任何提示/示例/代码/想法,
-Phill

Thanks for any tips/examples/code/ideas, -Phill

推荐答案

我很好奇 - 你认为你有什么灵活性?你将经历大量的工作,把东西放在XML中,只是将它转换为PHP数据。

I'm curious - exactly what flexibility do you think you're getting from this? You're going through an awful lot of work to put stuff in XML just to convert it to PHP data.

如果你确定使用XML(即没有

If you're ok with ditching XML (i.e., nothing else is using this configuration), why not something like this?

class Config
{
  private static $urls = array(
      'LOGIN_URL' => array(
          'scriptname' => 'secure_login.php'
        , 'location'   => 'optional/path/to/file'
      )
    , 'REDIRECTL' => array(
          'scriptname' => 'redirect.php'
        , 'location'   => 'optional/path/to/file'
      )
  );

  public static function getUrlArr( $alias )
  {
    if ( isset( self::$urls[$alias] ) )
    {
      return self::$urls[$alias];
    }
    throw new Exception( "URL alias '$alias' not defined" );
  }
}

另外,关于你在一个类名中放一个动词(解析)的选择。如果你要走这条路线,尝试使用该动词的名词表示。

Also, as a side note, I have some reservations about your choice to put a verb (parse) in a class name. If you're gonna go that route, try using a noun-representation of that verb.

class XmlConfigParser {}



EDIT



在这种情况下, t有任何方法名称引用XML节点结构(除非你想进入元程序的世界)

EDIT

In that case, then you can't have any method names that refer to the XML node structure (unless you want to get into the world of meta programming)

真的,无论你做什么,你只是要在XPath表达式上创建语法糖类

Really no matter what you do, you're just going to be creating syntactic sugar over an XPath expression

这里是您可以做的方法的示例

Here are examples of methods you might make

// Use XPath from the client
$config->fetchAttributesByXpath( "/urls/url[@alias='LOGIN_URL']" );

// Abstract away the XPah
$config->fetchAttributes( 'urls.url', 'alias', 'LOGIN_URL' );

例如, symfony 用他们的配置文件,这是YAML做这样的事情。在像这样的配置文件中

For example, symfony does something like this with their config files, which are YAML. In a config file like such

// app.yml
urls:
  LOGIN_URL: {scriptname: 'secure_login.php', location='optional/path/to/file'}

值如下

$values = sfConfig::get( 'app_urls_LOGIN_URL' );

这篇关于PHP将配置映射文件移动到XML,但是如何创建动态XPATH的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆