处理多个 HTMLPurifier 配置的有效方法

发布于 2024-08-27 14:29:54 字数 796 浏览 8 评论 0原文

我在当前项目中使用 HTMLPurifier,我不确定处理多个配置的最有效方法。在大多数情况下,唯一主要的变化是允许的标签。

目前,我在每个使用 HTMLPurifier 的类中都有一个私有方法,当需要配置时会调用该方法,并从默认值创建一个。我对此并不满意,就好像两个类使用相同的配置,这是重复的代码,如果一个类需要 2 个配置怎么办?就是感觉很乱。

它的一个好处是它很懒,因为它在需要时才创建任何东西。可以使用各种类,而无需纯化任何东西。所以我不想创建一堆甚至可能不会被使用的配置对象。

我最近发现您可以创建一个新的 HTMLPurifier 实例并直接设置配置选项,如下所示:

$purifier = new HTMLPurifier();
$purifier->config->set('HTML.Allowed', '');

我不确定这是否是不好的形式,如果不是,我真的不确定使用它的好方法。

我最新的想法是创建一个配置处理程序类,它只返回一个 HTMLPurifier 配置以供后续的 purify 调用使用。在某些时候,它可能会扩展以允许设置配置,但从一开始我就认为它们只是被硬编码并通过开关运行方法参数来获取请求的配置。也许我可以将存储的净化器实例作为参数发送,并让该方法直接在其上设置配置,如上所示?

对我来说,这似乎是我想到的几种方法中最好的,尽管我不确定这样的任务是否值得我创建一个类来处理它,如果是的话,我是否以最好的方式处理它。

我不太熟悉 HTMLPurifier 的内部工作原理,所以我不确定是否有更好的机制来处理多个配置。

感谢任何人都可以提供的任何见解。

I'm using HTMLPurifier in a current project and I'm not sure about the most efficient way to go about handling multiple configs. For the most part the only major thing changing is the allowed tags.

Currently I have a private method, in each class using HTMLPurifier, that gets called when a config is needed and it creates one from the default. I'm not really happy with this as if 2 classes are using the same config, that's duplicating code, and what if a class needs 2 configs? It just feels messy.

The one upside to it, is it's lazy in the sense that it's not creating anything until it's needed. The various classes could be used and never have to purify anything. So I don't want to be creating a bunch of config objects that might not even be used.

I just recently found out that you can create a new instance of HTMLPurifier and directly set config options like so:

$purifier = new HTMLPurifier();
$purifier->config->set('HTML.Allowed', '');

I'm not sure if that's bad form at all or not, and if it isn't I'm not really sure of a good way to put it to use.

My latest idea was to create a config handler class that would just return an HTMLPurifier config for use in subsequent purify calls. At some point it could probably be expanded to allow the setting of configs, but just from the start I figured they'd just be hardcoded and run a method parameter through a switch to grab the requested config. Perhaps I could just send the stored purifier instance as an argument and have the method directly set the config on it like shown above?

This to me seems the best of the few ways I thought of, though I'm not sure if such a task warrants me creating a class to handle it, and if so, if I'm handling it in the best way.

I'm not too familiar with the inner workings of HTMLPurifier so I'm not sure if there are any better mechanisms in place for handling multiple configs.

Thanks for any insight anyone can offer.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

清音悠歌 2024-09-03 14:29:54

配置对象有一个重要的不变量:在使用它们执行净化之后,它们就不能被编辑。您可能对该类具有的一些便捷方法感兴趣,特别是继承,并且您可以使用loadArray加载值数组。

Configuration objects have one important invariant: after they've been used to perform a purification, they cannot be edited. You might be interested in some convenience methods that the class has, in particular, inherit, and you can load an array of values using loadArray.

花桑 2024-09-03 14:29:54

我刚刚编写了一个快速、简单的配置处理程序类。

您可以查看/使用它@ http://gist.github.com/358187/

初始化时的配置数组。

$configs = array(
  'HTML.Doctype' => 'HTML 4.01 Strict',

  'posts' => array(
    'HTML.Allowed' => 'p,a[href],img[src]'
  ),
  'comments' => array(
    'HTML.Allowed' => 'p'
  ),

  'default' => array(
    'HTML.Allowed' => ''
  )
);

直接放入数组中的指令是全局的,并将应用于所有配置。如果在命名配置中设置相同的指令,则将使用命名配置中的值。

然后,您可以像这样获取配置:

$purifierConfig = new purifierConfig($configs);
$commentsConfig = $purifierConfig->getConfig('comments');

或者简写:

$commentsConfig = $purifierConfig['comments'];

如果请求的配置不存在或在调用中未指定配置,则将返回默认配置。

我可能会在某个时候充实它,但现在这是可行的。

我一直在寻找人们如何在他们的项目中处理此类事情,或者是否有任何内置机制可以使此类事情更加精简,但我认为我的问题范围有点太窄了。

I just wrote up a quick, simple config handler class.

You can view / use it @ http://gist.github.com/358187/

It takes an array of configs at initialization.

$configs = array(
  'HTML.Doctype' => 'HTML 4.01 Strict',

  'posts' => array(
    'HTML.Allowed' => 'p,a[href],img[src]'
  ),
  'comments' => array(
    'HTML.Allowed' => 'p'
  ),

  'default' => array(
    'HTML.Allowed' => ''
  )
);

Directives put directly into the array are global and will be applied to all configs. If the same directive is set inside a named config, the value in the named config will be used.

Then you can grab a config like so:

$purifierConfig = new purifierConfig($configs);
$commentsConfig = $purifierConfig->getConfig('comments');

or, shorthand:

$commentsConfig = $purifierConfig['comments'];

If the requested config does not exist or no config is specified in the call, the default config will be returned.

I'll probably flesh it out at some point, but for now this works.

I was looking for how people handled such things in their projects or if there were any built in mechanisms that made something like this more steamlined, but I suppose my question was a bit too narrow in scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文