Completed
Push — master ( 603188...dfca13 )
by Laurent
03:29
created

check.php ➔ get_error_message()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
require_once dirname(__FILE__).'/SymfonyRequirements.php';
4
5
$lineSize = 70;
6
$symfonyRequirements = new SymfonyRequirements();
7
$iniPath = $symfonyRequirements->getPhpIniConfigPath();
8
9
echo_title('Symfony Requirements Checker');
10
11
echo '> PHP is using the following php.ini file:'.PHP_EOL;
12
if ($iniPath) {
13
    echo_style('green', '  '.$iniPath);
14
} else {
15
    echo_style('yellow', '  WARNING: No configuration file (php.ini) used by PHP!');
16
}
17
18
echo PHP_EOL.PHP_EOL;
19
20
echo '> Checking Symfony requirements:'.PHP_EOL.'  ';
21
22
$messages = array();
23
foreach ($symfonyRequirements->getRequirements() as $req) {
24
    if ($helpText = get_error_message($req, $lineSize)) {
25
        echo_style('red', 'E');
26
        $messages['error'][] = $helpText;
27
    } else {
28
        echo_style('green', '.');
29
    }
30
}
31
32
$checkPassed = empty($messages['error']);
33
34
foreach ($symfonyRequirements->getRecommendations() as $req) {
35
    if ($helpText = get_error_message($req, $lineSize)) {
36
        echo_style('yellow', 'W');
37
        $messages['warning'][] = $helpText;
38
    } else {
39
        echo_style('green', '.');
40
    }
41
}
42
43
if ($checkPassed) {
44
    echo_block('success', 'OK', 'Your system is ready to run Symfony projects');
45
} else {
46
    echo_block('error', 'ERROR', 'Your system is not ready to run Symfony projects');
47
48
    echo_title('Fix the following mandatory requirements', 'red');
49
50
    foreach ($messages['error'] as $helpText) {
51
        echo ' * '.$helpText.PHP_EOL;
52
    }
53
}
54
55
if (!empty($messages['warning'])) {
56
    echo_title('Optional recommendations to improve your setup', 'yellow');
57
58
    foreach ($messages['warning'] as $helpText) {
59
        echo ' * '.$helpText.PHP_EOL;
60
    }
61
}
62
63
echo PHP_EOL;
64
echo_style('title', 'Note');
65
echo '  The command console could use a different php.ini file'.PHP_EOL;
66
echo_style('title', '~~~~');
67
echo '  than the one used with your web server. To be on the'.PHP_EOL;
68
echo '      safe side, please check the requirements from your web'.PHP_EOL;
69
echo '      server using the ';
70
echo_style('yellow', 'web/config.php');
71
echo ' script.'.PHP_EOL;
72
echo PHP_EOL;
73
74
exit($checkPassed ? 0 : 1);
75
76
function get_error_message(Requirement $requirement, $lineSize)
77
{
78
    if ($requirement->isFulfilled()) {
79
        return;
80
    }
81
82
    $errorMessage = wordwrap($requirement->getTestMessage(), $lineSize - 3, PHP_EOL.'   ').PHP_EOL;
83
    $errorMessage .= '   > '.wordwrap($requirement->getHelpText(), $lineSize - 5, PHP_EOL.'   > ').PHP_EOL;
84
85
    return $errorMessage;
86
}
87
88
function echo_title($title, $style = null)
89
{
90
    $style = $style ?: 'title';
91
92
    echo PHP_EOL;
93
    echo_style($style, $title.PHP_EOL);
94
    echo_style($style, str_repeat('~', strlen($title)).PHP_EOL);
95
    echo PHP_EOL;
96
}
97
98
function echo_style($style, $message)
99
{
100
    // ANSI color codes
101
    $styles = array(
102
        'reset' => "\033[0m",
103
        'red' => "\033[31m",
104
        'green' => "\033[32m",
105
        'yellow' => "\033[33m",
106
        'error' => "\033[37;41m",
107
        'success' => "\033[37;42m",
108
        'title' => "\033[34m",
109
    );
110
    $supports = has_color_support();
111
112
    echo($supports ? $styles[$style] : '').$message.($supports ? $styles['reset'] : '');
113
}
114
115
function echo_block($style, $title, $message)
116
{
117
    $message = ' '.trim($message).' ';
118
    $width = strlen($message);
119
120
    echo PHP_EOL.PHP_EOL;
121
122
    echo_style($style, str_repeat(' ', $width));
123
    echo PHP_EOL;
124
    echo_style($style, str_pad(' ['.$title.']', $width, ' ', STR_PAD_RIGHT));
125
    echo PHP_EOL;
126
    echo_style($style, $message);
127
    echo PHP_EOL;
128
    echo_style($style, str_repeat(' ', $width));
129
    echo PHP_EOL;
130
}
131
132
function has_color_support()
133
{
134
    static $support;
135
136
    if (null === $support) {
137
        if (DIRECTORY_SEPARATOR == '\\') {
138
            $support = false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
139
        } else {
140
            $support = function_exists('posix_isatty') && @posix_isatty(STDOUT);
141
        }
142
    }
143
144
    return $support;
145
}
146