Passed
Push — master ( e1f86a...4e1a3a )
by Siad
05:23
created

Diagnostics::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 0
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
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
namespace Phing\Util;
21
22
use Phing\Io\FileUtils;
23
use Phing\Io\FileWriter;
24
use Phing\Phing;
25
use Phing\Io\File;
26
use Phing\Io\PrintStream;
27
use Phing\Project;
28
29
/**
30
 * A little diagnostic helper that output some information that may help
31
 * in support. It should quickly give correct information about the
32
 * phing system.
33
 */
34
class Diagnostics
35
{
36
    /**
37
     * utility class
38
     */
39
    private function __construct()
40
    {
41
        // hidden constructor
42
    }
43
44
    /**
45
     * return the list of files existing in PHING_HOME/vendor
46
     *
47
     * @param string $type
48
     *
49
     * @return array the list of jar files existing in ant.home/lib or
50
     *               <tt>null</tt> if an error occurs.
51
     */
52
    public static function listLibraries($type)
53
    {
54
        $home = Phing::getProperty(Phing::PHING_HOME);
55
        if ($home == null) {
56
            return [];
57
        }
58
        $currentWorkingDir = getcwd();
59
        chdir($home);
60
        exec('composer show --' . $type, $packages, $code);
61
        chdir($currentWorkingDir);
62
63
        return $packages;
64
    }
65
66
    /**
67
     * Print a report to the given stream.
68
     *
69
     * @param PrintStream $out the stream to print the report to.
70
     */
71
    public static function doReport(PrintStream $out)
72
    {
73
        $out->println(str_pad('Phing diagnostics report', 79, "-", STR_PAD_BOTH));
74
        self::header($out, "Version");
75
        $out->println(Phing::getPhingVersion());
76
77
        self::header($out, "Project properties");
78
        self::doReportProjectProperties($out);
79
80
        self::header($out, "System properties");
81
        self::doReportSystemProperties($out);
82
83
        self::header($out, "PHING_HOME/vendor package listing");
84
        self::doReportPhingVendorLibraries($out);
85
86
        self::header($out, "COMPOSER_HOME/vendor package listing");
87
        self::doReportComposerSystemLibraries($out);
88
89
        self::header($out, "Tasks availability");
90
        self::doReportTasksAvailability($out);
91
92
        self::header($out, "Temp dir");
93
        self::doReportTempDir($out);
94
    }
95
96
    private static function header(PrintStream $out, $section)
97
    {
98
        $out->println(str_repeat('-', 79));
99
        $out->prints(" ");
100
        $out->println($section);
101
        $out->println(str_repeat('-', 79));
102
    }
103
104
    /**
105
     * Report a listing of system properties existing in the current phing.
106
     *
107
     * @param PrintStream $out the stream to print the properties to.
108
     */
109
    private static function doReportSystemProperties(PrintStream $out)
110
    {
111
        array_walk(
112
            Phing::getProperties(),
113
            static function ($v, $k) use ($out) {
114
                $out->println($k . ' : ' . $v);
115
            }
116
        );
117
    }
118
119
    /**
120
     * Report a listing of project properties.
121
     *
122
     * @param PrintStream $out the stream to print the properties to.
123
     */
124
    private static function doReportProjectProperties(PrintStream $out)
125
    {
126
        $project = new Project();
127
        $project->init();
128
129
        $sysprops = $project->getProperties();
130
131
        foreach ($sysprops as $key => $value) {
132
            $out->println($key . " : " . $value);
133
        }
134
    }
135
136
    /**
137
     * Report the content of PHING_HOME/vendor directory
138
     *
139
     * @param PrintStream $out the stream to print the content to
140
     */
141
    private static function doReportPhingVendorLibraries(PrintStream $out)
142
    {
143
        $libs = self::listLibraries('');
144
        self::printLibraries($libs, $out);
145
    }
146
147
    /**
148
     * Report the content of the global composer library directory
149
     *
150
     * @param PrintStream $out the stream to print the content to
151
     */
152
    private static function doReportComposerSystemLibraries(PrintStream $out)
153
    {
154
        $libs = self::listLibraries('platform');
155
        self::printLibraries($libs, $out);
156
    }
157
158
    /**
159
     * list the libraries
160
     *
161
     * @param array       $libs array of libraries (can be null)
162
     * @param PrintStream $out  output stream
163
     */
164
    private static function printLibraries($libs, PrintStream $out)
165
    {
166
        if ($libs == null) {
167
            $out->println("No such directory.");
168
            return;
169
        }
170
171
        foreach ($libs as $lib) {
172
            $out->println($lib);
173
        }
174
    }
175
176
    /**
177
     * Create a report about all available task in phing.
178
     *
179
     * @param PrintStream $out the stream to print the tasks report to
180
     *                         <tt>null</tt> for a missing stream (ie mapping).
181
     */
182
    private static function doReportTasksAvailability(PrintStream $out)
183
    {
184
        $project = new Project();
185
        $project->init();
186
        $tasks = $project->getTaskDefinitions();
187
        ksort($tasks);
188
        foreach ($tasks as $shortName => $task) {
189
            $out->println($shortName);
190
        }
191
    }
192
193
    /**
194
     * try and create a temp file in our temp dir; this
195
     * checks that it has space and access.
196
     * We also do some clock reporting.
197
     *
198
     * @param PrintStream $out
199
     */
200
    private static function doReportTempDir(PrintStream $out)
201
    {
202
        $tempdir = FileUtils::getTempDir();
203
        if ($tempdir == null) {
204
            $out->println("Warning: php.tmpdir is undefined");
205
            return;
206
        }
207
        $out->println("Temp dir is " . $tempdir);
208
        $tempDirectory = new File($tempdir);
209
210
        if (!$tempDirectory->exists()) {
211
            $out->println("Warning, php.tmpdir directory does not exist: " . $tempdir);
212
            return;
213
        }
214
215
        $now = time();
216
        $tempFile = (new FileUtils())->createTempFile('diag', 'txt', $tempDirectory, true, true);
217
        $fileWriter = new FileWriter($tempFile);
218
        $fileWriter->write('some test text');
219
        $fileWriter->close();
220
221
        $filetime = $tempFile->lastModified();
222
223
        $out->println("Temp dir is writeable");
224
        $drift = $filetime - $now;
225
        $out->println("Temp dir alignment with system clock is " . $drift . " s");
226
        if (abs($drift) > 10) {
227
            $out->println("Warning: big clock drift -maybe a network filesystem");
228
        }
229
    }
230
}
231