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
 * Strips whitespace from [php] files using PHP stripwhitespace() method.
22
 *
23
 * @author  Hans Lellelid, hans@velum.net
24
 * @see     FilterReader
25
 * @package phing.filters
26
 */
27
class StripWhitespace extends BaseFilterReader implements ChainableReader
28
{
29
    private $processed = false;
30

31
    /**
32
     * Returns the  stream without Php comments and whitespace.
33
     *
34
     * @param int $len
35
     * @return string the resulting stream, or -1
36
     *             if the end of the resulting stream has been reached
37
     * @throws IOException
38
     * @throws NullPointerException
39
     */
40 1
    public function read($len = null)
41
    {
42 1
        if ($this->processed === true) {
43 1
            return -1; // EOF
44
        }
45

46
        // Read XML
47 1
        $php = null;
48 1
        while (($buffer = $this->in->read($len)) !== -1) {
49 1
            $php .= $buffer;
50
        }
51

52 1
        if ($php === null) { // EOF?
53 0
            return -1;
54
        }
55

56 1
        if (empty($php)) {
57 0
            $this->log("PHP file is empty!", Project::MSG_WARN);
58

59 0
            return ''; // return empty string, don't attempt to strip whitespace
60
        }
61

62
        // write buffer to a temporary file, since php_strip_whitespace() needs a filename
63 1
        $file = new SplFileObject(tempnam(FileUtils::getTempDir(), mt_rand()), 'w+');
64 1
        $file->fwrite($php);
65 1
        $name = $file->getRealPath();
66 1
        $output = php_strip_whitespace($name);
67 1
        $file = null;
68 1
        unlink($name);
69

70 1
        $this->processed = true;
71

72 1
        return $output;
73
    }
74

75
    /**
76
     * Creates a new StripWhitespace using the passed in
77
     * Reader for instantiation.
78
     *
79
     * @param A|Reader $reader
80
     * @return StripWhitespace a new filter based on this configuration, but filtering
81
     *           the specified reader
82
     * @internal param A $reader Reader object providing the underlying stream.
83
     *               Must not be <code>null</code>.
84
     */
85 1
    public function chain(Reader $reader): Reader
86
    {
87 1
        $newFilter = new StripWhitespace($reader);
88 1
        $newFilter->setProject($this->getProject());
89

90 1
        return $newFilter;
91
    }
92
}

Read our documentation on viewing source code .

Loading