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
 * A parameter is composed of a name, type and value.
22
 *
23
 * Example of usage:
24
 *
25
 * <replacetokens>
26
 *   <tokensource classname="phing.filters.util.IniFileTokenReader">
27
 *     <!-- all params for the TokenReader here -->
28
 *     <param name="file" value="tokens.ini" />
29
 *   </tokensource>
30
 * </replacetokens>
31
 *
32
 * or:
33
 *
34
 * <filterreader classname="phing.filters.ReplaceTokens">
35
 *   <param type="tokensource>
36
 *     <param name="classname" value="phing.filters.util.IniFileTokenReader" />
37
 *     <param name="file" value="tokens.ini" />
38
 *   </param>
39
 * </filterreader>
40
 *
41
 * @author  <a href="mailto:yl@seasonfive.com">Yannick Lecaillez</a>
42
 * @package phing.types
43
 */
44
class TokenSource extends DataType
45
{
46

47
    /**
48
     * String to hold the path to the TokenReader
49
     *
50
     * @var string
51
     */
52
    protected $classname = null;
53

54
    /**
55
     * Array holding parameters for the wrapped TokenReader.
56
     *
57
     * @var array
58
     */
59
    protected $parameters = [];
60

61
    /**
62
     * Reference to the TokenReader used by this TokenSource
63
     *
64
     * @var TokenReader
65
     */
66
    protected $reader;
67

68
    /**
69
     * Array with key/value pairs of tokens
70
     */
71
    protected $tokens = [];
72

73
    /**
74
     * This method is called to load the sources from the reader
75
     * into the buffer of the source.
76
     */
77 1
    public function load()
78
    {
79
        // Create new Reader
80 1
        if ($this->classname === null) {
81 0
            throw new BuildException("No Classname given to TokenSource.");
82
        }
83

84 1
        $classname = Phing::import($this->classname);
85 1
        $this->reader = new $classname($this->project);
86

87
        // Configure Reader
88 1
        $this->configureTokenReader($this->reader);
89

90
        // Load Tokens
91
        try {
92 1
            while ($token = $this->reader->readToken()) {
93 1
                $this->tokens[] = $token;
94
            }
95 0
        } catch (BuildException $e) {
96 0
            $this->log("Error reading TokenSource: " . $e->getMessage(), Project::MSG_WARN);
97 0
        } catch (IOException $e) {
98 0
            $this->log("Error reading TokenSource: " . $e->getMessage(), Project::MSG_WARN);
99
        }
100
    }
101

102
    /**
103
     * This function uses the wrapper to read the tokens and then
104
     * returns them.
105
     */
106 1
    public function getTokens()
107
    {
108 1
        if (count($this->tokens) == 0) {
109 1
            $this->load();
110
        }
111

112 1
        return $this->tokens;
113
    }
114

115
    /**
116
     * Configures a TokenReader with the parameters passed to the
117
     * TokenSource.
118
     *
119
     * @param TokenReader $reader
120
     */
121 1
    private function configureTokenReader(TokenReader $reader)
122
    {
123 1
        $count = count($this->parameters);
124 1
        for ($i = 0; $i < $count; $i++) {
125 1
            $method_name = "Set" . $this->parameters[$i]->getName();
126 1
            $value = $this->parameters[$i]->getValue();
127 1
            $reader->$method_name($value);
128
        }
129
    }
130

131
    /**
132
     * Set the classname (dot-path) to use for handling token replacement.
133
     *
134
     * @param string $c
135
     */
136 1
    public function setClassname($c)
137
    {
138 1
        $this->classname = $c;
139
    }
140

141
    /**
142
     * Returns the qualified classname (dot-path) to use for handling token replacement.
143
     *
144
     * @return string
145
     */
146 0
    public function getClassname()
147
    {
148 0
        return $this->classname;
149
    }
150

151
    /**
152
     * Create nested <param> tag.
153
     * Uses standard name/value Parameter class.
154
     *
155
     * @return Parameter
156
     */
157 1
    public function createParam()
158
    {
159 1
        $num = array_push($this->parameters, new Parameter());
160

161 1
        return $this->parameters[$num - 1];
162
    }
163
}

Read our documentation on viewing source code .

Loading