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 task is for using filter chains to make changes to files and overwrite the original files.
22
 *
23
 * This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task
24
 * being used to modify files and then overwrite the modified files.  In many (most?) cases you probably
25
 * should just use a copy task  to preserve the original source files, but this task supports situations
26
 * where there is no src vs. build directory, and modifying source files is actually desired.
27
 *
28
 * <code>
29
 *    <reflexive>
30
 *        <fileset dir=".">
31
 *            <include pattern="*.html">
32
 *        </fileset>
33
 *        <filterchain>
34
 *            <replaceregexp>
35
 *                <regexp pattern="\n\r" replace="\n"/>
36
 *            </replaceregexp>
37
 *        </filterchain>
38
 *    </reflexive>
39
 * </code>
40
 *
41
 * @author Hans Lellelid <hans@xmpl.org>
42
 *
43
 * @package phing.tasks.system
44
 */
45
class ReflexiveTask extends Task
46
{
47
    use FileSetAware;
48
    use FilterChainAware;
49

50
    /**
51
     * Single file to process.
52
     */
53
    private $file;
54

55
    /**
56
     * Alias for setFrom()
57
     *
58
     * @param PhingFile $f
59
     */
60 0
    public function setFile(PhingFile $f)
61
    {
62 0
        $this->file = $f;
63
    }
64

65
    /**
66
     * Append the file(s).
67
     */
68 0
    public function main()
69
    {
70 0
        if ($this->file === null && empty($this->filesets)) {
71 0
            throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task.");
72
        }
73

74
        // compile a list of all files to modify, both file attrib and fileset elements
75
        // can be used.
76

77 0
        $files = [];
78

79 0
        if ($this->file !== null) {
80 0
            $files[] = $this->file;
81
        }
82

83 0
        if (!empty($this->filesets)) {
84 0
            foreach ($this->filesets as $fs) {
85
                try {
86 0
                    $ds = $fs->getDirectoryScanner($this->project);
87 0
                    $filenames = $ds->getIncludedFiles(); // get included filenames
88 0
                    $dir = $fs->getDir($this->project);
89 0
                    foreach ($filenames as $fname) {
90 0
                        $files[] = new PhingFile($dir, $fname);
91
                    }
92 0
                } catch (BuildException $be) {
93 0
                    $this->log($be->getMessage(), Project::MSG_WARN);
94
                }
95
            }
96
        }
97

98 0
        $this->log("Applying reflexive processing to " . count($files) . " files.");
99

100
        // These "slots" allow filters to retrieve information about the currently-being-process files
101 0
        $slot = $this->getRegisterSlot("currentFile");
102 0
        $basenameSlot = $this->getRegisterSlot("currentFile.basename");
103

104 0
        foreach ($files as $file) {
105
            // set the register slots
106

107 0
            $slot->setValue($file->getPath());
108 0
            $basenameSlot->setValue($file->getName());
109

110
            // 1) read contents of file, pulling through any filters
111 0
            $in = null;
112
            try {
113 0
                $contents = "";
114 0
                $in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project);
115 0
                while (-1 !== ($buffer = $in->read())) {
116 0
                    $contents .= $buffer;
117
                }
118 0
                $in->close();
119 0
            } catch (Exception $e) {
120 0
                if ($in) {
121 0
                    $in->close();
122
                }
123 0
                $this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN);
124
            }
125

126
            try {
127
                // now create a FileWriter w/ the same file, and write to the file
128 0
                $out = new FileWriter($file);
129 0
                $out->write($contents);
130 0
                $out->close();
131 0
                $this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE);
132 0
            } catch (Exception $e) {
133 0
                if ($out) {
134 0
                    $out->close();
135
                }
136 0
                $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
137
            }
138
        }
139
    }
140
}

Read our documentation on viewing source code .

Loading