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
 * LoadFileTask
22
 *
23
 * Loads a (text) file and stores the contents in a property.
24
 * Supports filterchains.
25
 *
26
 * @author  Michiel Rook <mrook@php.net>
27
 * @package phing.tasks.ext
28
 */
29
class LoadFileTask extends Task
30
{
31
    use FilterChainAware;
32

33
    /**
34
     * File to read
35
     *
36
     * @var PhingFile file
37
     */
38
    private $file;
39

40
    /**
41
     * Property to be set
42
     *
43
     * @var string $property
44
     */
45
    private $property;
46

47
    private $failOnError = true;
48

49
    private $quiet = false;
50

51
    /**
52
     * @param bool $fail
53
     */
54 0
    public function setFailOnError($fail)
55
    {
56 0
        $this->failOnError = $fail;
57
    }
58

59
    /**
60
     * @param bool $quiet
61
     */
62 0
    public function setQuiet($quiet)
63
    {
64 0
        $this->quiet = $quiet;
65 0
        if ($quiet) {
66 0
            $this->failOnError = false;
67
        }
68
    }
69

70
    /**
71
     * Set file to read
72
     *
73
     * @param PhingFile $file
74
     */
75 1
    public function setFile($file)
76
    {
77 1
        $this->file = $file;
78
    }
79

80
    /**
81
     * Convenience setter to maintain Ant compatibility (@see setFile())
82
     *
83
     * @param    $srcFile
84
     * @internal param PhingFile $file
85
     */
86 0
    public function setSrcFile($srcFile)
87
    {
88 0
        $this->file = $srcFile;
89
    }
90

91
    /**
92
     * Set name of property to be set
93
     *
94
     * @param  $property
95
     * @return void
96
     */
97 1
    public function setProperty($property)
98
    {
99 1
        $this->property = $property;
100
    }
101

102
    /**
103
     * Main method
104
     *
105
     * @return void
106
     * @throws BuildException
107
     */
108 1
    public function main()
109
    {
110 1
        if (empty($this->file)) {
111 0
            throw new BuildException("Attribute 'file' required", $this->getLocation());
112
        }
113

114 1
        if (empty($this->property)) {
115 0
            throw new BuildException("Attribute 'property' required", $this->getLocation());
116
        }
117 1
        if ($this->quiet && $this->failOnError) {
118 0
            throw new BuildException("quiet and failonerror cannot both be set to true");
119
        }
120

121
        try {
122 1
            if (is_string($this->file)) {
123 1
                $this->file = new PhingFile($this->file);
124
            }
125 1
            if (!$this->file->exists()) {
126 0
                $message = (string) $this->file . ' doesn\'t exist';
127 0
                if ($this->failOnError) {
128 0
                    throw new BuildException($message);
129
                }
130

131 0
                $this->log($message, $this->quiet ? Project::MSG_WARN : Project::MSG_ERR);
132 0
                return;
133
            }
134

135 1
            $this->log("loading " . (string) $this->file . " into property " . $this->property, Project::MSG_VERBOSE);
136
            // read file (through filterchains)
137 1
            $contents = "";
138

139 1
            if ($this->file->length() > 0) {
140 1
                $reader = FileUtils::getChainedReader(new FileReader($this->file), $this->filterChains, $this->project);
141 1
                while (-1 !== ($buffer = $reader->read())) {
142 1
                    $contents .= $buffer;
143
                }
144 1
                $reader->close();
145
            } else {
146 0
                $this->log(
147 0
                    "Do not set property " . $this->property . " as its length is 0.",
148 0
                    $this->quiet ? Project::MSG_VERBOSE : Project::MSG_INFO
149
                );
150
            }
151

152
            // publish as property
153 1
            if ($contents !== '') {
154 1
                $this->project->setNewProperty($this->property, $contents);
155 1
                $this->log("loaded " . strlen($contents) . " characters", Project::MSG_VERBOSE);
156 1
                $this->log($this->property . " := " . $contents, Project::MSG_DEBUG);
157
            }
158 0
        } catch (IOException $ioe) {
159 0
            $message = "Unable to load resource: " . $ioe->getMessage();
160 0
            if ($this->failOnError) {
161 0
                throw new BuildException($message, $ioe, $this->getLocation());
162
            }
163

164 0
            $this->log($message, $this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR);
165 0
        } catch (BuildException $be) {
166 0
            if ($this->failOnError) {
167 0
                throw $be;
168
            }
169

170 0
            $this->log($be->getMessage(), $this->quiet ? Project::MSG_VERBOSE : Project::MSG_ERR);
171
        }
172
    }
173
}

Read our documentation on viewing source code .

Loading