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 little diagnostic helper that output some information that may help
22
 * in support. It should quickly give correct information about the
23
 * phing system.
24
 */
25
class Diagnostics
26
{
27
    /**
28
     * utility class
29
     */
30 0
    private function __construct()
31
    {
32
        // hidden constructor
33
    }
34

35
    /**
36
     * return the list of files existing in PHING_HOME/vendor
37
     *
38
     * @param string $type
39
     *
40
     * @return array the list of jar files existing in ant.home/lib or
41
     *               <tt>null</tt> if an error occurs.
42
     */
43 0
    public static function listLibraries($type)
44
    {
45 0
        $home = Phing::getProperty(Phing::PHING_HOME);
46 0
        if ($home == null) {
47 0
            return [];
48
        }
49 0
        $currentWorkingDir = getcwd();
50 0
        chdir($home);
51 0
        exec('composer show --' . $type, $packages, $code);
52 0
        chdir($currentWorkingDir);
53

54 0
        return $packages;
55
    }
56

57
    /**
58
     * Print a report to the given stream.
59
     *
60
     * @param PrintStream $out the stream to print the report to.
61
     */
62 0
    public static function doReport(PrintStream $out)
63
    {
64 0
        $out->println(str_pad('Phing diagnostics report', 79, "-", STR_PAD_BOTH));
65 0
        self::header($out, "Version");
66 0
        $out->println(Phing::getPhingVersion());
67

68 0
        self::header($out, "Project properties");
69 0
        self::doReportProjectProperties($out);
70

71 0
        self::header($out, "System properties");
72 0
        self::doReportSystemProperties($out);
73

74 0
        self::header($out, "PHING_HOME/vendor package listing");
75 0
        self::doReportPhingVendorLibraries($out);
76

77 0
        self::header($out, "COMPOSER_HOME/vendor package listing");
78 0
        self::doReportComposerSystemLibraries($out);
79

80 0
        self::header($out, "Tasks availability");
81 0
        self::doReportTasksAvailability($out);
82

83 0
        self::header($out, "Temp dir");
84 0
        self::doReportTempDir($out);
85
    }
86

87 0
    private static function header(PrintStream $out, $section)
88
    {
89 0
        $out->println(str_repeat('-', 79));
90 0
        $out->prints(" ");
91 0
        $out->println($section);
92 0
        $out->println(str_repeat('-', 79));
93
    }
94

95
    /**
96
     * Report a listing of system properties existing in the current phing.
97
     *
98
     * @param PrintStream $out the stream to print the properties to.
99
     */
100 0
    private static function doReportSystemProperties(PrintStream $out)
101
    {
102 0
        array_walk(
103 0
            Phing::getProperties(),
104
            static function ($v, $k) use ($out) {
105 0
                $out->println($k . ' : ' . $v);
106
            }
107
        );
108
    }
109

110
    /**
111
     * Report a listing of project properties.
112
     *
113
     * @param PrintStream $out the stream to print the properties to.
114
     */
115 0
    private static function doReportProjectProperties(PrintStream $out)
116
    {
117 0
        $project = new Project();
118 0
        $project->init();
119

120 0
        $sysprops = $project->getProperties();
121

122 0
        foreach ($sysprops as $key => $value) {
123 0
            $out->println($key . " : " . $value);
124
        }
125
    }
126

127
    /**
128
     * Report the content of PHING_HOME/vendor directory
129
     *
130
     * @param PrintStream $out the stream to print the content to
131
     */
132 0
    private static function doReportPhingVendorLibraries(PrintStream $out)
133
    {
134 0
        $libs = self::listLibraries('');
135 0
        self::printLibraries($libs, $out);
136
    }
137

138
    /**
139
     * Report the content of the global composer library directory
140
     *
141
     * @param PrintStream $out the stream to print the content to
142
     */
143 0
    private static function doReportComposerSystemLibraries(PrintStream $out)
144
    {
145 0
        $libs = self::listLibraries('platform');
146 0
        self::printLibraries($libs, $out);
147
    }
148

149
    /**
150
     * list the libraries
151
     *
152
     * @param array $libs array of libraries (can be null)
153
     * @param PrintStream $out output stream
154
     */
155 0
    private static function printLibraries($libs, PrintStream $out)
156
    {
157 0
        if ($libs == null) {
158 0
            $out->println("No such directory.");
159 0
            return;
160
        }
161

162 0
        foreach ($libs as $lib) {
163 0
            $out->println($lib);
164
        }
165
    }
166

167
    /**
168
     * Create a report about all available task in phing.
169
     *
170
     * @param PrintStream $out the stream to print the tasks report to
171
     *                         <tt>null</tt> for a missing stream (ie mapping).
172
     */
173 0
    private static function doReportTasksAvailability(PrintStream $out)
174
    {
175 0
        $project = new Project();
176 0
        $project->init();
177 0
        $tasks = $project->getTaskDefinitions();
178 0
        ksort($tasks);
179 0
        foreach ($tasks as $shortName => $task) {
180 0
            $out->println($shortName);
181
        }
182
    }
183

184
    /**
185
     * try and create a temp file in our temp dir; this
186
     * checks that it has space and access.
187
     * We also do some clock reporting.
188
     *
189
     * @param PrintStream $out
190
     */
191 0
    private static function doReportTempDir(PrintStream $out)
192
    {
193 0
        $tempdir = FileUtils::getTempDir();
194 0
        if ($tempdir == null) {
195 0
            $out->println("Warning: php.tmpdir is undefined");
196 0
            return;
197
        }
198 0
        $out->println("Temp dir is " . $tempdir);
199 0
        $tempDirectory = new PhingFile($tempdir);
200

201 0
        if (!$tempDirectory->exists()) {
202 0
            $out->println("Warning, php.tmpdir directory does not exist: " . $tempdir);
203 0
            return;
204
        }
205

206 0
        $now = time();
207 0
        $tempFile = (new FileUtils())->createTempFile('diag', 'txt', $tempDirectory, true, true);
208 0
        $fileWriter = new FileWriter($tempFile);
209 0
        $fileWriter->write('some test text');
210 0
        $fileWriter->close();
211

212 0
        $filetime = $tempFile->lastModified();
213

214 0
        $out->println("Temp dir is writeable");
215 0
        $drift = $filetime - $now;
216 0
        $out->println("Temp dir alignment with system clock is " . $drift . " s");
217 0
        if (abs($drift) > 10) {
218 0
            $out->println("Warning: big clock drift -maybe a network filesystem");
219
        }
220
    }
221
}

Read our documentation on viewing source code .

Loading