check.php 3.8 KB

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