PHP DomDocument,XSLTProcessor的重用,是否稳定/安全? [英] PHP DomDocument, reuse of XSLTProcessor, it is stable/secure?

查看:76
本文介绍了PHP DomDocument,XSLTProcessor的重用,是否稳定/安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用下面的函数,但不确定它是否始终安全...是吗? 那里没有DOM内存或残余XSLT"吗?

I am using the function below, but not sure about it is always secure... Is it? No DOM-memory or "residual XSLT" at there?

   function XSLproc_reuse($domXsl) {
      static $XSLproc=NULL;
      if (!$XSLproc)
           $XSLproc = new XSLTProcessor();
      return $XSLproc->importStylesheet($domXsl); // STABLE?
   }

上面没有未来的意外副作用"吗?

PS:我的XSLT处理过程中存在一些奇怪的错误……因此,在此处发布一个(或多个)其他假设,以检查是否可以或必须避免.这个在XPath中更加明显,请参见其他相关问题.

PS: I have some strange bugs with my XSLT processing... So, posting one (of many other) hypothesis here, to check if ok or must be avoid. This is more evident with XPath, see this other related question.

重用更多处理表(在我的库中使用过)的另一种方法是也重用导入的XSLT:

Another way, to REUSE MORE processing sheet (that I was using on my library), is to reuse also the imported XSLT:

   function XSLproc_reuse2($nameOrDomXsl='', $domXsl=NULL) {
      static $XSLproc=NULL;
      static $name='';

      if (!$XSLproc)
                $XSLproc = new XSLTProcessor();
      // else reune of the already initialized $XSLproc.

      if (is_object($nameOrDomXsl))
                return $XSLproc->importStylesheet($nameOrDomXsl); // STABLE?
      elseif ($nameOrDomXsl==$name);
                return $XSLproc;  // imported in the last call, STABLE?
      else { // recording for future reuse:
                $name = $nameOrDomXsl;
                return $XSLproc->importStylesheet($domXsl);
      }
   }

推荐答案

使用static定义一个全局状态,即定义为不稳定".可以在程序中的任何位置进行更改.使用对象,您可以获取局部状态(在对象实例内部).我也建议使用数组.因此它可以为不同的文件存储多个处理器.

Using static defines a global state, that is by definition "unstable". It can be changed from anywhere in the program. Using an object you get a local state (inside the object instance). I suggest using a array, too. So it can store several processors for different files.

class XsltFactory {

  private $_processors = array();

  public function get($file) {
    if (!isset($this->_processors[$file])) {
      $xslDom = new DOMDocument();
      $xslDom->load($file);
      $xslProc = new XSLTProcessor();
      $xslProc->importStylesheet($xslDom);
      return $this->_processors[$file] = $xslProc;
    }
    return $this->_processors[$file];
  }
}

$xsltFactory = new XsltFactory();
var_dump(
  htmlspecialchars(
    $xsltFactory->get($template)->transformToDoc($xmlDom)->saveXml()
  )
);

xslcache 是提高性能的更好解决方案.它将$xslt->importStyleSheet($filename)的结果缓存在进程内部.如果该过程被重用,则编译后的xsl也将被重用.

A better solution to boost the performance would be xslcache. It caches the result of $xslt->importStyleSheet($filename) inside the process. If the process is reused, so is the compiled xsl.

这篇关于PHP DomDocument,XSLTProcessor的重用,是否稳定/安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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