1<?php
2/**
3 * Smarty Internal Plugin Filter Handler
4 * Smarty filter handler class
5 *
6 * @package    Smarty
7 * @subpackage PluginsInternal
8 * @author     Uwe Tews
9 */
10
11/**
12 * Class for filter processing
13 *
14 * @package    Smarty
15 * @subpackage PluginsInternal
16 */
17class Smarty_Internal_Runtime_FilterHandler
18{
19    /**
20     * Run filters over content
21     * The filters will be lazy loaded if required
22     * class name format: Smarty_FilterType_FilterName
23     * plugin filename format: filtertype.filtername.php
24     * Smarty2 filter plugins could be used
25     *
26     * @param  string                   $type     the type of filter ('pre','post','output') which shall run
27     * @param  string                   $content  the content which shall be processed by the filters
28     * @param  Smarty_Internal_Template $template template object
29     *
30     * @throws SmartyException
31     * @return string                   the filtered content
32     */
33    public function runFilter($type, $content, Smarty_Internal_Template $template)
34    {
35        // loop over autoload filters of specified type
36        if (!empty($template->smarty->autoload_filters[ $type ])) {
37            foreach ((array) $template->smarty->autoload_filters[ $type ] as $name) {
38                $plugin_name = "Smarty_{$type}filter_{$name}";
39                if (function_exists($plugin_name)) {
40                    $callback = $plugin_name;
41                } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) {
42                    $callback = array($plugin_name, 'execute');
43                } elseif ($template->smarty->loadPlugin($plugin_name, false)) {
44                    if (function_exists($plugin_name)) {
45                        // use loaded Smarty2 style plugin
46                        $callback = $plugin_name;
47                    } elseif (class_exists($plugin_name, false) && is_callable(array($plugin_name, 'execute'))) {
48                        // loaded class of filter plugin
49                        $callback = array($plugin_name, 'execute');
50                    } else {
51                        throw new SmartyException("Auto load {$type}-filter plugin method \"{$plugin_name}::execute\" not callable");
52                    }
53                } else {
54                    // nothing found, throw exception
55                    throw new SmartyException("Unable to auto load {$type}-filter plugin \"{$plugin_name}\"");
56                }
57                $content = call_user_func($callback, $content, $template);
58            }
59        }
60        // loop over registered filters of specified type
61        if (!empty($template->smarty->registered_filters[ $type ])) {
62            foreach ($template->smarty->registered_filters[ $type ] as $key => $name) {
63                $content = call_user_func($template->smarty->registered_filters[ $type ][ $key ], $content, $template);
64            }
65        }
66        // return filtered output
67        return $content;
68    }
69}
70