1<?php
2
3class HTML_Template_Flexy_Compiler_Regex {
4
5    /**
6    * The main flexy engine
7    *
8    * @var object HTML_Template_Flexy
9    * @access public
10    */
11
12    var $flexy;
13    /**
14    *   classicParse - the older regex based code generator.
15    *   here all the replacing, filtering and writing of the compiled file is done
16    *   well this is not much work, but still its in here :-)
17    *
18    *   @access     private
19    *   @version    01/12/03
20    *   @author     Wolfram Kriesing <wolfram@kriesing.de>
21    *   @author     Alan Knowles <alan@akbkhome.com>
22    *   @return   boolean (basically true all the time here)
23    */
24    function compile(&$flexy)
25    {
26        $this->flexy = &$flexy;
27        // read the entire file into one variable
28        $fileContent = file_get_contents($flexy->currentTemplate);
29
30        //  apply pre filter
31        $fileContent = $this->applyFilters( $fileContent , "/^pre_/i" );
32        $fileContent = $this->applyFilters( $fileContent , "/^(pre_|post_)/i",TRUE);
33        $fileContent = $this->applyFilters( $fileContent , "/^post_/i" );
34        // write the compiled template into the compiledTemplate-File
35        if( ($cfp = fopen( $flexy->compiledTemplate , 'w' )) ) {
36            fwrite($cfp,$fileContent);
37            fclose($cfp);
38            @chmod($flexy->compiledTemplate,0775);
39        }
40
41        return true;
42    }
43    /**
44    *   actually it will only be used to apply the pre and post filters
45    *
46    *   @access     public
47    *   @version    01/12/10
48    *   @author     Alan Knowles <alan@akbkhome.com>
49    *   @param      string  $input      the string to filter
50    *   @param      array   $prefix     the subset of methods to use.
51    *   @return     string  the filtered string
52    */
53    function applyFilters( $input , $prefix = "",$negate=FALSE)
54    {
55        $this->flexy->debug("APPLY FILTER $prefix<BR>");
56        $filters = $this->options['filters'];
57        $this->flexy->debug(serialize($filters)."<BR>");
58        foreach($filters as $filtername) {
59            $class = "HTML_Template_Flexy_Compiler_Regex_{$filtername}";
60            require_once("HTML/Template/Flexy/Compiler/Regex/{$filtername}.php");
61
62            if (!class_exists($class)) {
63                return HTML_Template_Flexy::staticRaiseError("Failed to load filter $filter",null,HTML_TEMPLATE_FLEXY_ERROR_DIE);
64            }
65
66            if (!@$this->filter_objects[$class])  {
67                $this->filter_objects[$class] = new $class;
68                $this->filter_objects[$class]->_set_engine($this);
69            }
70            $filter = &$this->filter_objects[$class];
71            $methods = get_class_methods($class);
72            $this->flexy->debug("METHODS:");
73            $this->flexy->debug(serialize($methods)."<BR>");
74            foreach($methods as $method) {
75                if ($method{0} == "_") {
76                    continue; // private
77                }
78                if ($method  == $class) {
79                    continue; // constructor
80                }
81                $this->flexy->debug("TEST: $negate $prefix : $method");
82                if ($negate &&  preg_match($prefix,$method)) {
83                    continue;
84                }
85                if (!$negate && !preg_match($prefix,$method)) {
86                    continue;
87                }
88
89                $this->flexy->debug("APPLYING $filtername $method<BR>");
90                $input = $filter->$method($input);
91            }
92        }
93
94        return $input;
95    }
96}
97
98
99