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 class to represent the nested <formatter> element for PDO SQL results.
22
 *
23
 * This class is inspired by the similarly-named class in the PHPUnit tasks.
24
 *
25
 * @author  Hans Lellelid <hans@xmpl.org>
26
 * @package phing.tasks.ext.pdo
27
 * @since   2.3.0
28
 */
29
class PDOSQLExecFormatterElement
30
{
31
    /**
32
     * @var PDOResultFormatter
33
     */
34
    private $formatter;
35

36
    /**
37
     * The type of the formatter (used for built-in formatter classes).
38
     *
39
     * @var string
40
     */
41
    private $type = "";
42

43
    /**
44
     * Whether to use file (or write output to phing log).
45
     *
46
     * @var boolean
47
     */
48
    private $useFile = true;
49

50
    /**
51
     * Output file for formatter.
52
     *
53
     * @var PhingFile
54
     */
55
    private $outfile;
56

57
    /**
58
     * Print header columns.
59
     *
60
     * @var boolean
61
     */
62
    private $showheaders = true;
63

64
    /**
65
     * Whether to format XML output.
66
     *
67
     * @var boolean
68
     */
69
    private $formatoutput = true;
70

71
    /**
72
     * Encoding for XML output.
73
     *
74
     * @var string
75
     */
76
    private $encoding;
77

78
    /**
79
     * Column delimiter.
80
     * Defaults to ','
81
     *
82
     * @var string
83
     */
84
    private $coldelimiter = ",";
85

86
    /**
87
     * Row delimiter.
88
     * Defaults to PHP_EOL.
89
     *
90
     * @var string
91
     */
92
    private $rowdelimiter = PHP_EOL;
93

94
    /**
95
     * Append to an existing file or overwrite it?
96
     *
97
     * @var boolean
98
     */
99
    private $append = false;
100

101
    /**
102
     * Parameters for a custom formatter.
103
     *
104
     * @var array Parameter[]
105
     */
106
    private $formatterParams = [];
107

108
    /**
109
     * @var PDOSQLExecTask
110
     */
111
    private $parentTask;
112

113
    /**
114
     * @var Parameter[]
115
     */
116
    private $parameters;
117

118
    /**
119
     * @var bool
120
     */
121
    private $formatOutput;
122

123
    /**
124
     * Construct a new PDOSQLExecFormatterElement with parent task.
125
     *
126
     * @param PDOSQLExecTask $parentTask
127
     */
128 0
    public function __construct(PDOSQLExecTask $parentTask)
129
    {
130 0
        $this->parentTask = $parentTask;
131
    }
132

133
    /**
134
     * Supports nested <param> element (for custom formatter classes).
135
     *
136
     * @return Parameter
137
     */
138 0
    public function createParam()
139
    {
140 0
        $num = array_push($this->parameters, new Parameter());
141

142 0
        return $this->parameters[$num - 1];
143
    }
144

145
    /**
146
     * Gets a configured output writer.
147
     *
148
     * @return Writer
149
     */
150 0
    private function getOutputWriter()
151
    {
152 0
        if ($this->useFile) {
153 0
            $of = $this->getOutfile();
154 0
            if (!$of) {
155 0
                $of = new PhingFile($this->formatter->getPreferredOutfile());
156
            }
157

158 0
            return new FileWriter($of, $this->append);
159
        }
160

161 0
        return $this->getDefaultOutput();
162
    }
163

164
    /**
165
     * Configures wrapped formatter class with any attributes on this element.
166
     *
167
     * @throws BuildException
168
     */
169 0
    public function prepare(Location $location)
170
    {
171 0
        if (!$this->formatter) {
172 0
            throw new BuildException("No formatter specified (use type or classname attribute)", $location);
173
        }
174

175 0
        $out = $this->getOutputWriter();
176

177 0
        $this->parentTask->log("Setting output writer to: " . get_class($out), Project::MSG_VERBOSE);
178 0
        $this->formatter->setOutput($out);
179

180 0
        if ($this->formatter instanceof PlainPDOResultFormatter) {
181
            // set any options that apply to the plain formatter
182 0
            $this->formatter->setShowheaders($this->showheaders);
183 0
            $this->formatter->setRowdelim($this->rowdelimiter);
184 0
            $this->formatter->setColdelim($this->coldelimiter);
185 0
        } elseif ($this->formatter instanceof XMLPDOResultFormatter) {
186
            // set any options that apply to the xml formatter
187 0
            $this->formatter->setEncoding($this->encoding);
188 0
            $this->formatter->setFormatOutput($this->formatoutput);
189
        }
190

191 0
        foreach ($this->formatterParams as $param) {
192 0
            $param = new Parameter();
193 0
            $method = 'set' . $param->getName();
194 0
            if (!method_exists($this->formatter, $param->getName())) {
195 0
                throw new BuildException(
196 0
                    "Formatter " . get_class(
197 0
                        $this->formatter
198 0
                    ) . " does not have a $method method.",
199 0
                    $location
200
                );
201
            }
202 0
            call_user_func([$this->formatter, $method], $param->getValue());
203
        }
204
    }
205

206
    /**
207
     * Sets the formatter type.
208
     *
209
     * @param  string $type
210
     * @throws BuildException
211
     */
212 0
    public function setType($type)
213
    {
214 0
        $this->type = $type;
215 0
        if ($this->type == "xml") {
216 0
            $this->formatter = new XMLPDOResultFormatter();
217 0
        } elseif ($this->type == "plain") {
218 0
            $this->formatter = new PlainPDOResultFormatter();
219 0
        } else {
220 0
            throw new BuildException("Formatter '" . $this->type . "' not implemented");
221
        }
222
    }
223

224
    /**
225
     * Set classname for a custom formatter (must extend PDOResultFormatter).
226
     *
227
     * @param string $className
228
     */
229 0
    public function setClassName($className)
230
    {
231 0
        $classNameNoDot = Phing::import($className);
232 0
        $this->formatter = new $classNameNoDot();
233
    }
234

235
    /**
236
     * Set whether to write formatter results to file.
237
     *
238
     * @param boolean $useFile
239
     */
240 0
    public function setUseFile($useFile)
241
    {
242 0
        $this->useFile = (bool) $useFile;
243
    }
244

245
    /**
246
     * Return whether to write formatter results to file.
247
     *
248
     * @return boolean
249
     */
250 0
    public function getUseFile()
251
    {
252 0
        return $this->useFile;
253
    }
254

255
    /**
256
     * Sets the output file for the formatter results.
257
     *
258
     * @param    PhingFile $outfile
259
     * @internal param PhingFile $outFile
260
     */
261 0
    public function setOutfile(PhingFile $outfile)
262
    {
263 0
        $this->outfile = $outfile;
264
    }
265

266
    /**
267
     * Get the output file.
268
     *
269
     * @return PhingFile
270
     */
271 0
    public function getOutfile()
272
    {
273 0
        return $this->outfile;
274
    }
275

276
    /**
277
     * whether output should be appended to or overwrite
278
     * an existing file.  Defaults to false.
279
     *
280
     * @param boolean $append
281
     */
282 0
    public function setAppend($append)
283
    {
284 0
        $this->append = (bool) $append;
285
    }
286

287
    /**
288
     * Whether output should be appended to file.
289
     *
290
     * @return boolean
291
     */
292 0
    public function getAppend()
293
    {
294 0
        return $this->append;
295
    }
296

297
    /**
298
     * Print headers for result sets from the
299
     * statements; optional, default true.
300
     *
301
     * @param boolean $showheaders
302
     */
303 0
    public function setShowheaders($showheaders)
304
    {
305 0
        $this->showheaders = (bool) $showheaders;
306
    }
307

308
    /**
309
     * Sets the column delimiter.
310
     *
311
     * @param string $v
312
     */
313 0
    public function setColdelim($v)
314
    {
315 0
        $this->coldelimiter = $v;
316
    }
317

318
    /**
319
     * Sets the row delimiter.
320
     *
321
     * @param string $v
322
     */
323 0
    public function setRowdelim($v)
324
    {
325 0
        $this->rowdelimiter = $v;
326
    }
327

328
    /**
329
     * Set the DOM document encoding.
330
     *
331
     * @param string $v
332
     */
333 0
    public function setEncoding($v)
334
    {
335 0
        $this->encoding = $v;
336
    }
337

338
    /**
339
     * @param boolean $v
340
     */
341 0
    public function setFormatOutput($v)
342
    {
343 0
        $this->formatOutput = (bool) $v;
344
    }
345

346
    /**
347
     * Gets a default output writer for this task.
348
     *
349
     * @return Writer
350
     */
351 0
    private function getDefaultOutput()
352
    {
353 0
        return new LogWriter($this->parentTask);
354
    }
355

356
    /**
357
     * Gets the formatter that has been configured based on this element.
358
     *
359
     * @return PDOResultFormatter
360
     */
361 0
    public function getFormatter()
362
    {
363 0
        return $this->formatter;
364
    }
365
}

Read our documentation on viewing source code .

Loading