Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 1
<?php
2
/*
3
 *  $Id: ReflexiveTask.php 144 2007-02-05 15:19:00Z hans $
4
 *
5
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
6
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
7
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
8
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
9
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
10
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
11
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
12
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
13
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
14
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
15
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
16
 *
17
 * This software consists of voluntary contributions made by many individuals
18
 * and is licensed under the LGPL. For more information please see
19
 * <http://phing.info>.
20
 */
21
 
22
require_once 'phing/Task.php';
23
 
24
/**
25
 * This task is for using filter chains to make changes to files and overwrite the original files.
26
 *
27
 * This task was created to serve the need for "cleanup" tasks -- e.g. a ReplaceRegexp task or strip task
28
 * being used to modify files and then overwrite the modified files.  In many (most?) cases you probably
29
 * should just use a copy task  to preserve the original source files, but this task supports situations
30
 * where there is no src vs. build directory, and modifying source files is actually desired.
31
 *
32
 * <code>
33
 *    <reflexive>
34
 *        <fileset dir=".">
35
 *            <include pattern="*.html">
36
 *        </fileset>
37
 *        <filterchain>
38
 *            <replaceregexp>
39
 *                <regexp pattern="\n\r" replace="\n"/>
40
 *            </replaceregexp>
41
 *        </filterchain>
42
 *    </reflexive>
43
 * </code>
44
 *
45
 * @author    Hans Lellelid <hans@xmpl.org>
46
 * @version   $Revision: 1.11 $
47
 * @package   phing.tasks.system
48
 */
49
class ReflexiveTask extends Task {
50
 
51
    /** Single file to process. */
52
    private $file;
53
 
54
    /** Any filesets that should be processed. */
55
    private $filesets = array();
56
 
57
    /** Any filters to be applied before append happens. */
58
    private $filterChains = array();
59
 
60
    /** Alias for setFrom() */
61
    function setFile(PhingFile $f) {
62
        $this->file = $f;
63
    }
64
 
65
    /** Nested creator, adds a set of files (nested fileset attribute). */
66
    function createFileSet() {
67
        $num = array_push($this->filesets, new FileSet());
68
        return $this->filesets[$num-1];
69
    }
70
 
71
    /**
72
     * Creates a filterchain
73
     *
74
     * @return  object  The created filterchain object
75
     */
76
    function createFilterChain() {
77
        $num = array_push($this->filterChains, new FilterChain($this->project));
78
        return $this->filterChains[$num-1];
79
    }
80
 
81
    /** Append the file(s). */
82
    function main() {
83
 
84
        if ($this->file === null && empty($this->filesets)) {
85
            throw new BuildException("You must specify a file or fileset(s) for the <reflexive> task.");
86
        }
87
 
88
        // compile a list of all files to modify, both file attrib and fileset elements
89
        // can be used.
90
 
91
        $files = array();
92
 
93
        if ($this->file !== null) {
94
            $files[] = $this->file;
95
        }
96
 
97
        if (!empty($this->filesets)) {
98
            $filenames = array();
99
            foreach($this->filesets as $fs) {
100
                try {
101
                    $ds = $fs->getDirectoryScanner($this->project);
102
                    $filenames = $ds->getIncludedFiles(); // get included filenames
103
                    $dir = $fs->getDir($this->project);
104
                    foreach ($filenames as $fname) {
105
                        $files[] = new PhingFile($dir, $fname);
106
                    }
107
                } catch (BuildException $be) {
108
                    $this->log($be->getMessage(), Project::MSG_WARN);
109
                }
110
            }
111
        }
112
 
113
        $this->log("Applying reflexive processing to " . count($files) . " files.");
114
 
115
		// These "slots" allow filters to retrieve information about the currently-being-process files
116
		$slot = $this->getRegisterSlot("currentFile");
117
		$basenameSlot = $this->getRegisterSlot("currentFile.basename");
118
 
119
 
120
        foreach($files as $file) {
121
			// set the register slots
122
 
123
			$slot->setValue($file->getPath());
124
			$basenameSlot->setValue($file->getName());
125
 
126
            // 1) read contents of file, pulling through any filters
127
            $in = null;
128
            try {
129
                $contents = "";
130
                $in = FileUtils::getChainedReader(new FileReader($file), $this->filterChains, $this->project);
131
                while(-1 !== ($buffer = $in->read())) {
132
                    $contents .= $buffer;
133
                }
134
                $in->close();
135
            } catch (Exception $e) {
136
                if ($in) $in->close();
137
                $this->log("Erorr reading file: " . $e->getMessage(), Project::MSG_WARN);
138
            }
139
 
140
            try {
141
                // now create a FileWriter w/ the same file, and write to the file
142
                $out = new FileWriter($file);
143
                $out->write($contents);
144
                $out->close();
145
                $this->log("Applying reflexive processing to " . $file->getPath(), Project::MSG_VERBOSE);
146
            } catch (Exception $e) {
147
                if ($out) $out->close();
148
                $this->log("Error writing file back: " . $e->getMessage(), Project::MSG_WARN);
149
            }
150
 
151
        }
152
 
153
    }
154
 
155
}