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
 * Generates a file in the current directory with
22
 * an JSON description of what happened during a build.
23
 * The default filename is "log.json", but this can be overridden
24
 * with the property <code>JsonLogger.file</code>.
25
 *
26
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
27
 * @package phing.listener
28
 */
29
class JsonLogger extends XmlLogger
30
{
31
    /**
32
     * Fired when the build finishes, this adds the time taken and any
33
     * error stacktrace to the build element and writes the document to disk.
34
     *
35
     * @param  BuildEvent $event An event with any relevant extra information.
36
     *                          Will not be <code>null</code>.
37
     * @throws BuildException
38
     */
39 0
    public function buildFinished(BuildEvent $event)
40
    {
41 0
        $elapsedTime = Phing::currentTimeMillis() - $this->getBuildTimerStart();
42

43 0
        $this->getBuildElement()->setAttribute(XmlLogger::TIME_ATTR, DefaultLogger::formatTime($elapsedTime));
44

45 0
        if ($event->getException() != null) {
46 0
            $this->getBuildElement()->setAttribute(XmlLogger::ERROR_ATTR, $event->getException()->getMessage());
47 0
            $errText = $this->getDoc()->createCDATASection($event->getException()->getTraceAsString());
48 0
            $stacktrace = $this->getDoc()->createElement(XmlLogger::STACKTRACE_TAG);
49 0
            $stacktrace->appendChild($errText);
50 0
            $this->getBuildElement()->appendChild($stacktrace);
51
        }
52

53 0
        $this->getDoc()->appendChild($this->getBuildElement());
54

55 0
        $outFilename = $event->getProject()->getProperty("JsonLogger.file");
56 0
        if ($outFilename == null) {
57 0
            $outFilename = "log.json";
58
        }
59

60 0
        try {
61 0
            $stream = $this->getOut();
62 0
            if ($stream === null) {
63 0
                $stream = new FileOutputStream($outFilename);
64
            }
65

66 0
            $writer = new OutputStreamWriter($stream);
67 0
            $writer->write($this->xml2js(simplexml_import_dom($this->getDoc())));
68 0
            $writer->close();
69 0
        } catch (IOException $exc) {
70 0
            try {
71 0
                $stream->close(); // in case there is a stream open still ...
72 0
            } catch (Exception $x) {
73
            }
74 0
            throw new BuildException("Unable to write log file.", $exc);
75
        }
76

77
        // cleanup:remove the buildElement
78 0
        $this->setBuildElement(null);
79

80 0
        array_pop($this->getElementStack());
81 0
        array_pop($this->getTimesStack());
82
    }
83

84 0
    private function xml2js(SimpleXMLElement $xmlnode, $isRoot = true)
85
    {
86 0
        $jsnode = [];
87

88 0
        if (!$isRoot) {
89 0
            if (count($xmlnode->attributes()) > 0) {
90 0
                $jsnode["@attribute"] = [];
91 0
                foreach ($xmlnode->attributes() as $key => $value) {
92 0
                    $jsnode["@attribute"][$key] = (string) $value;
93
                }
94
            }
95

96 0
            $textcontent = trim((string) $xmlnode);
97 0
            if (count($textcontent) > 0) {
98 0
                $jsnode['_'] = $textcontent;
99
            }
100

101 0
            foreach ($xmlnode->children() as $childxmlnode) {
102 0
                $childname = $childxmlnode->getName();
103 0
                if (!array_key_exists($childname, $jsnode)) {
104 0
                    $jsnode[$childname] = [];
105
                }
106 0
                $jsnode[$childname][] = $this->xml2js($childxmlnode, false);
107
            }
108 0
            return $jsnode;
109
        }
110

111 0
        $nodename = $xmlnode->getName();
112 0
        $jsnode[$nodename] = [];
113 0
        $jsnode[$nodename][] = $this->xml2js($xmlnode, false);
114 0
        return json_encode($jsnode, JSON_PRETTY_PRINT);
115
    }
116
}

Read our documentation on viewing source code .

Loading