LimePrinter.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /*
  3. * This file is part of the Lime framework.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. * (c) Bernhard Schussek <bernhard.schussek@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  11. class LimePrinter
  12. {
  13. const
  14. OK = 0,
  15. NOT_OK = 1,
  16. COMMENT = 2,
  17. SKIP = 3,
  18. WARNING = 4,
  19. ERROR = 5,
  20. HAPPY = 6,
  21. STRING = 7,
  22. NUMBER = 8,
  23. BOOLEAN = 9,
  24. INFO = 10,
  25. TRACE = 11,
  26. TODO = 12;
  27. protected
  28. $colorizer = null;
  29. public function __construct(LimeColorizer $colorizer = null)
  30. {
  31. if (!is_null($colorizer))
  32. {
  33. $colorizer->setStyle(self::OK, array('fg' => 'green', 'bold' => true));
  34. $colorizer->setStyle(self::NOT_OK, array('bg' => 'red', 'fg' => 'white', 'bold' => true));
  35. $colorizer->setStyle(self::COMMENT, array('fg' => 'yellow'));
  36. $colorizer->setStyle(self::SKIP, array('fg' => 'yellow', 'bold' => true));
  37. $colorizer->setStyle(self::TODO, array('fg' => 'yellow', 'bold' => true));
  38. $colorizer->setStyle(self::WARNING, array('fg' => 'white', 'bg' => 'yellow', 'bold' => true));
  39. $colorizer->setStyle(self::ERROR, array('bg' => 'red', 'fg' => 'white', 'bold' => true));
  40. $colorizer->setStyle(self::HAPPY, array('fg' => 'white', 'bg' => 'green', 'bold' => true));
  41. $colorizer->setStyle(self::STRING, array('fg' => 'cyan'));
  42. $colorizer->setStyle(self::NUMBER, array());
  43. $colorizer->setStyle(self::BOOLEAN, array('fg' => 'cyan'));
  44. $colorizer->setStyle(self::INFO, array('fg' => 'cyan', 'bold' => true));
  45. $colorizer->setStyle(self::TRACE, array('fg' => 'green', 'bold' => true));
  46. }
  47. $this->colorizer = $colorizer;
  48. }
  49. public function printText($text, $style = null)
  50. {
  51. print $this->colorize($text, $style);
  52. }
  53. public function printLine($text, $style = null)
  54. {
  55. print $this->colorize($text, $style)."\n";
  56. }
  57. public function printBox($text, $style = null)
  58. {
  59. print $this->colorize(str_pad($text, 80, ' '), $style)."\n";
  60. }
  61. public function printLargeBox($text, $style = null)
  62. {
  63. $space = $this->colorize(str_repeat(' ', 80), $style)."\n";
  64. $text = trim($text);
  65. $text = wordwrap($text, 75, "\n");
  66. print "\n".$space;
  67. foreach (explode("\n", $text) as $line)
  68. {
  69. print $this->colorize(str_pad(' '.$line, 80, ' '), $style)."\n";
  70. }
  71. print $space."\n";
  72. }
  73. protected function colorize($text, $style)
  74. {
  75. if (is_null($this->colorizer))
  76. {
  77. return $text;
  78. }
  79. else
  80. {
  81. if (is_null($style))
  82. {
  83. return preg_replace_callback('/("[^"]*"|(?<!\w)\d+(\.\d+)?(?!\w)|true|false'/*|(->|::)?\w+\([^\)]*\)*/.')/', array($this, 'autoColorize'), $text);
  84. }
  85. else
  86. {
  87. return $this->colorizer->colorize($text, $style);
  88. }
  89. }
  90. }
  91. public function autoColorize($text)
  92. {
  93. $text = $text[0];
  94. if (is_null($this->colorizer))
  95. {
  96. return $text;
  97. }
  98. else
  99. {
  100. if ($text{0} == '"')
  101. {
  102. return $this->colorizer->colorize($text, self::STRING);
  103. }
  104. else if (in_array(strtolower($text), array('true', 'false')))
  105. {
  106. return $this->colorizer->colorize($text, self::BOOLEAN);
  107. }
  108. else
  109. {
  110. return $this->colorizer->colorize($text, self::NUMBER);
  111. }
  112. }
  113. }
  114. }