/Component/Console/Style/ImagineStyle.php

https://github.com/liip/LiipImagineBundle · PHP · 158 lines · 109 code · 32 blank · 17 comment · 7 complexity · 227c3ee10660bcdd924c3dcab2e49192 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the `liip/LiipImagineBundle` project.
  4. *
  5. * (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
  6. *
  7. * For the full copyright and license information, please view the LICENSE.md
  8. * file that was distributed with this source code.
  9. */
  10. namespace Liip\ImagineBundle\Component\Console\Style;
  11. use Liip\ImagineBundle\Exception\InvalidArgumentException;
  12. use Symfony\Component\Console\Input\InputInterface;
  13. use Symfony\Component\Console\Output\OutputInterface;
  14. use Symfony\Component\Console\Style\SymfonyStyle;
  15. use ValueError;
  16. /**
  17. * @internal
  18. */
  19. final class ImagineStyle
  20. {
  21. /**
  22. * @var SymfonyStyle
  23. */
  24. private $io;
  25. /**
  26. * @var bool
  27. */
  28. private $decoration;
  29. public function __construct(InputInterface $input, OutputInterface $output, bool $decoration = true)
  30. {
  31. $this->io = new SymfonyStyle($input, $output);
  32. $this->decoration = $decoration;
  33. }
  34. public function text(string $string, array $replacements = []): self
  35. {
  36. $this->io->write($this->compileString($string, $replacements));
  37. return $this;
  38. }
  39. public function line(string $string, array $replacements = []): self
  40. {
  41. $this->io->writeln($this->compileString($string, $replacements));
  42. return $this;
  43. }
  44. public function newline(int $count = 1): self
  45. {
  46. $this->io->newLine($count);
  47. return $this;
  48. }
  49. public function status(string $status, string $fg = null): self
  50. {
  51. return $this->text(
  52. sprintf('<fg=%2$s>(</><fg=%2$s;options=bold>%1$s</><fg=%2$s>)</>', $status, $fg ?: 'default')
  53. );
  54. }
  55. public function group(string $item, string $group, string $fg = null): self
  56. {
  57. $this->text(
  58. sprintf('<fg=%3$s;options=bold>%1$s[</><fg=%3$s>%2$s</><fg=%3$s;options=bold>]</>', $item, $group, $fg ?: 'default')
  59. );
  60. return $this->space();
  61. }
  62. public function title(string $title, string $type = null): self
  63. {
  64. if (!$this->decoration) {
  65. return $this->plainTitle($title, $type);
  66. }
  67. return $this->block($title, $type, 'white', 'cyan');
  68. }
  69. public function okayBlock(string $string, array $replacements = []): self
  70. {
  71. return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'OKAY', 'black', 'green', '-');
  72. }
  73. public function critBlock(string $string, array $replacements = []): self
  74. {
  75. return $this->largeBlock($this->compileString(strip_tags($string), $replacements), 'ERROR', 'white', 'red', '#');
  76. }
  77. private function largeBlock(string $string, string $type, string $fg = null, string $bg = null, string $prefix = null): self
  78. {
  79. return $this->block($string, $type, $fg, $bg, $prefix, true);
  80. }
  81. private function space(int $count = 1): self
  82. {
  83. return $this->text(str_repeat(' ', $count));
  84. }
  85. private function plainTitle(string $title, string $type = null): self
  86. {
  87. $this->newline();
  88. if ($type) {
  89. $this->line('# [%s] %s', [$type, $title]);
  90. } else {
  91. $this->line('# %s', [$title]);
  92. }
  93. return $this->newline();
  94. }
  95. private function block(string $string, string $type = null, string $fg = null, string $bg = null, string $prefix = null, bool $padding = true): self
  96. {
  97. if (!$this->decoration) {
  98. return $this->plainBlock($string, $type);
  99. }
  100. $this->io->block($string, $type, sprintf('fg=%s;bg=%s', $fg ?: 'default', $bg ?: 'default'), $prefix ? sprintf(' %s ', $prefix) : ' ', $padding);
  101. return $this;
  102. }
  103. private function plainBlock(string $string, string $type): self
  104. {
  105. return $this
  106. ->newline()
  107. ->line('[%s] %s', [$type, $string])
  108. ->newline();
  109. }
  110. private function compileString(string $format, array $replacements = []): string
  111. {
  112. if (!$this->decoration) {
  113. $format = strip_tags($format);
  114. }
  115. if (0 === \count($replacements)) {
  116. return $format;
  117. }
  118. try {
  119. if (false !== $compiled = @vsprintf($format, $replacements)) {
  120. return $compiled;
  121. }
  122. } catch (ValueError $error) {
  123. }
  124. throw new InvalidArgumentException(sprintf('Invalid string format "%s" or replacements "%s".', $format, implode(', ', array_map(function ($replacement) { return var_export($replacement, true); }, $replacements))));
  125. }
  126. }