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 is a Php comment and string stripper reader that filters |
22
|
|
|
* those lexical tokens out for purposes of simple Php parsing. |
23
|
|
|
* (if you have more complex Php parsing needs, use a real lexer). |
24
|
|
|
* Since this class heavily relies on the single char read function, |
25
|
|
|
* you are recommended to make it work on top of a buffered reader. |
26
|
|
|
* |
27
|
|
|
* @author <a href="mailto:[email protected]">Yannick Lecaillez</a> |
28
|
|
|
* @author hans lellelid, [email protected] |
29
|
|
|
* @see FilterReader |
30
|
|
|
* @package phing.filters |
31
|
|
|
*/ |
32
|
|
|
class StripPhpComments extends BaseFilterReader implements ChainableReader |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* Returns the stream without Php comments. |
36
|
|
|
* |
37
|
|
|
* @param int|null $len |
38
|
|
|
* @return string the resulting stream, or -1 |
39
|
|
|
* if the end of the resulting stream has been reached |
40
|
|
|
* @throws IOException |
41
|
|
|
*/ |
42
|
|
|
public function read($len = null) |
43
|
|
|
{ |
44
|
|
|
$buffer = $this->in->read($len); |
45
|
|
|
if ($buffer === -1) { |
46
|
|
|
return -1; |
47
|
|
|
} |
48
|
|
|
$newStr = ''; |
49
|
|
|
$tokens = token_get_all($buffer); |
50
|
|
|
|
51
|
|
|
foreach ($tokens as $token) { |
52
|
|
|
if (is_array($token)) { |
53
|
|
|
[$id, $text] = $token; |
54
|
|
|
|
55
|
|
|
switch ($id) { |
56
|
|
|
case T_COMMENT: |
57
|
|
|
case T_DOC_COMMENT: |
58
|
|
|
// no action on comments |
59
|
|
|
continue 2; |
60
|
|
|
|
61
|
|
|
default: |
62
|
|
|
$newStr .= $text; |
63
|
|
|
continue 2; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$newStr .= $token; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return $newStr; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Creates a new StripPhpComments using the passed in |
74
|
|
|
* Reader for instantiation. |
75
|
|
|
* |
76
|
|
|
* @param A|Reader $reader |
77
|
|
|
* @return $this a new filter based on this configuration, but filtering |
78
|
|
|
* the specified reader |
79
|
|
|
* @internal param A $reader Reader object providing the underlying stream. |
80
|
|
|
* Must not be <code>null</code>. |
81
|
|
|
*/ |
82
|
|
|
public function chain(Reader $reader): Reader |
83
|
|
|
{ |
84
|
|
|
$newFilter = new StripPhpComments($reader); |
85
|
|
|
$newFilter->setProject($this->getProject()); |
86
|
|
|
|
87
|
|
|
return $newFilter; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|