common.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. define('LINE_WIDTH', 75);
  11. define('LINE', str_repeat('-', LINE_WIDTH)."\n");
  12. function bailout($message)
  13. {
  14. echo wordwrap($message, LINE_WIDTH)." Aborting.\n";
  15. exit(1);
  16. }
  17. function strip_minor_versions($version)
  18. {
  19. preg_match('/^(?P<version>[0-9]\.[0-9]|[0-9]{2,})/', $version, $matches);
  20. return $matches['version'];
  21. }
  22. function centered($text)
  23. {
  24. $padding = (int) ((LINE_WIDTH - strlen($text)) / 2);
  25. return str_repeat(' ', $padding).$text;
  26. }
  27. function cd($dir)
  28. {
  29. if (false === chdir($dir)) {
  30. bailout("Could not switch to directory $dir.");
  31. }
  32. }
  33. function run($command)
  34. {
  35. exec($command, $output, $status);
  36. if (0 !== $status) {
  37. $output = implode("\n", $output);
  38. echo "Error while running:\n ".getcwd().'$ '.$command."\nOutput:\n".LINE."$output\n".LINE;
  39. bailout("\"$command\" failed.");
  40. }
  41. }
  42. function get_icu_version_from_genrb($genrb)
  43. {
  44. exec($genrb.' --version - 2>&1', $output, $status);
  45. if (0 !== $status) {
  46. bailout($genrb.' failed.');
  47. }
  48. if (!preg_match('/ICU version ([\d\.]+)/', implode('', $output), $matches)) {
  49. return null;
  50. }
  51. return $matches[1];
  52. }
  53. error_reporting(\E_ALL);
  54. set_error_handler(function ($type, $msg, $file, $line) {
  55. throw new \ErrorException($msg, 0, $type, $file, $line);
  56. });
  57. set_exception_handler(function (Throwable $exception) {
  58. echo "\n";
  59. $cause = $exception;
  60. $root = true;
  61. while (null !== $cause) {
  62. if (!$root) {
  63. echo "Caused by\n";
  64. }
  65. echo get_class($cause).': '.$cause->getMessage()."\n";
  66. echo "\n";
  67. echo $cause->getFile().':'.$cause->getLine()."\n";
  68. echo $cause->getTraceAsString()."\n";
  69. $cause = $cause->getPrevious();
  70. $root = false;
  71. }
  72. });