ConsoleLogger.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Setup;
  7. use Symfony\Component\Console\Output\OutputInterface;
  8. use Symfony\Component\Console\Formatter\OutputFormatterStyle;
  9. /**
  10. * Console Logger
  11. *
  12. * @package Magento\Setup\Model
  13. */
  14. class ConsoleLogger implements LoggerInterface
  15. {
  16. /**
  17. * Indicator of whether inline output is started
  18. *
  19. * @var bool
  20. */
  21. private $isInline = false;
  22. /**
  23. * Console
  24. *
  25. * @var OutputInterface
  26. */
  27. protected $console;
  28. /**
  29. * Constructor
  30. *
  31. * @param OutputInterface $output
  32. */
  33. public function __construct(OutputInterface $output)
  34. {
  35. $this->console = $output;
  36. $outputFormatter = $this->console->getFormatter();
  37. $outputFormatter->setStyle('detail', new OutputFormatterStyle('blue'));
  38. $outputFormatter->setStyle('metadata', new OutputFormatterStyle('cyan'));
  39. }
  40. /**
  41. * {@inheritdoc}
  42. */
  43. public function logSuccess($message)
  44. {
  45. $this->terminateLine();
  46. $this->console->writeln("<info>[SUCCESS]" . ($message ? ": $message" : '') . '</info>');
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function logError(\Exception $e)
  52. {
  53. $this->terminateLine();
  54. $this->console->writeln("<error>[ERROR]: " . $e . '</error>');
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function log($message)
  60. {
  61. $this->terminateLine();
  62. $this->console->writeln('<detail>' . $message . '</detail>');
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function logInline($message)
  68. {
  69. $this->isInline = true;
  70. $this->console->write('<detail>' . $message . '</detail>');
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function logMeta($message)
  76. {
  77. $this->terminateLine();
  78. $this->console->writeln('<metadata>' . $message . '</metadata>');
  79. }
  80. /**
  81. * Terminates line if the inline logging is started
  82. *
  83. * @return void
  84. */
  85. private function terminateLine()
  86. {
  87. if ($this->isInline) {
  88. $this->isInline = false;
  89. $this->console->writeln('');
  90. }
  91. }
  92. }