1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information please see |
17
|
|
|
* <http://phing.info>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* This filter uses the bundled-with-PHP Tidy extension to filter input. |
22
|
|
|
* |
23
|
|
|
* <p> |
24
|
|
|
* Example:<br/> |
25
|
|
|
* <pre> |
26
|
|
|
* <tidyfilter encoding="utf8"> |
27
|
|
|
* <config name="indent" value="true"/> |
28
|
|
|
* <config name="output-xhtml" value="true"/> |
29
|
|
|
* </tidyfilter> |
30
|
|
|
* </pre> |
31
|
|
|
* |
32
|
|
|
* @author Hans Lellelid <[email protected]> |
33
|
|
|
* @package phing.filters |
34
|
|
|
*/ |
35
|
|
|
class TidyFilter extends BaseParamFilterReader implements ChainableReader |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string Encoding of resulting document. |
40
|
|
|
*/ |
41
|
|
|
private $encoding = 'utf8'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var array Parameter[] |
45
|
|
|
*/ |
46
|
|
|
private $configParameters = []; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set the encoding for resulting (X)HTML document. |
50
|
|
|
* |
51
|
|
|
* @param string $v |
52
|
|
|
*/ |
53
|
|
|
public function setEncoding($v) |
54
|
|
|
{ |
55
|
|
|
$this->encoding = $v; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Sets the config params. |
60
|
|
|
* |
61
|
|
|
* @param array Parameter[] |
62
|
|
|
* @see chain() |
63
|
|
|
*/ |
64
|
|
|
public function setConfigParameters($params) |
65
|
|
|
{ |
66
|
|
|
$this->configParameters = $params; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Adds a <config> element (which is a Parameter). |
71
|
|
|
* |
72
|
|
|
* @return Parameter |
73
|
|
|
*/ |
74
|
|
|
public function createConfig() |
75
|
|
|
{ |
76
|
|
|
$num = array_push($this->configParameters, new Parameter()); |
77
|
|
|
|
78
|
|
|
return $this->configParameters[$num - 1]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Converts the Parameter objects being used to store configuration into a simle assoc array. |
83
|
|
|
* |
84
|
|
|
* @return array |
85
|
|
|
*/ |
86
|
|
|
private function getDistilledConfig() |
87
|
|
|
{ |
88
|
|
|
$config = []; |
89
|
|
|
foreach ($this->configParameters as $p) { |
90
|
|
|
$config[$p->getName()] = $p->getValue(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $config; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Reads input and returns Tidy-filtered output. |
98
|
|
|
* |
99
|
|
|
* @param int $len |
100
|
|
|
* @throws BuildException |
101
|
|
|
* @return string Characters read, or -1 if the end of the stream has been reached |
102
|
|
|
*/ |
103
|
|
|
public function read($len = null) |
104
|
|
|
{ |
105
|
|
|
if (!class_exists('Tidy')) { |
106
|
|
|
throw new BuildException("You must enable the 'tidy' extension in your PHP configuration in order to use the Tidy filter."); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
if (!$this->getInitialized()) { |
110
|
|
|
$this->initialize(); |
111
|
|
|
$this->setInitialized(true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$buffer = $this->in->read($len); |
115
|
|
|
if ($buffer === -1) { |
116
|
|
|
return -1; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$config = $this->getDistilledConfig(); |
120
|
|
|
|
121
|
|
|
$tidy = new tidy(); |
122
|
|
|
$tidy->parseString($buffer, $config, $this->encoding); |
123
|
|
|
$tidy->cleanRepair(); |
124
|
|
|
|
125
|
|
|
return tidy_get_output($tidy); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Creates a new TidyFilter using the passed in Reader for instantiation. |
130
|
|
|
* |
131
|
|
|
* @param Reader $reader Reader object providing the underlying stream. |
132
|
|
|
* Must not be <code>null</code>. |
133
|
|
|
* @return TidyFilter a new filter based on this configuration, but filtering the specified reader |
134
|
|
|
*/ |
135
|
|
|
public function chain(Reader $reader): Reader |
136
|
|
|
{ |
137
|
|
|
$newFilter = new self($reader); |
138
|
|
|
$newFilter->setConfigParameters($this->configParameters); |
139
|
|
|
$newFilter->setEncoding($this->encoding); |
140
|
|
|
$newFilter->setProject($this->getProject()); |
141
|
|
|
|
142
|
|
|
return $newFilter; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Initializes any parameters (e.g. config options). |
147
|
|
|
* This method is only called when this filter is used through a <filterreader> tag in build file. |
148
|
|
|
*/ |
149
|
|
|
private function initialize() |
150
|
|
|
{ |
151
|
|
|
$params = $this->getParameters(); |
152
|
|
|
if (!empty($params)) { |
153
|
|
|
foreach ($params as $param) { |
154
|
|
|
if ($param->getType() == "config") { |
155
|
|
|
$this->configParameters[] = $param; |
156
|
|
|
} else { |
157
|
|
|
if ($param->getName() == "encoding") { |
158
|
|
|
$this->setEncoding($param->getValue()); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|