|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony MakerBundle package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bundle\MakerBundle\Console; |
| 13 | + |
| 14 | +use Symfony\Component\Console\Formatter\OutputFormatterInterface; |
| 15 | +use Symfony\Component\Console\Output\OutputInterface; |
| 16 | + |
| 17 | +class MigrationDiffFilteredOutput implements OutputInterface |
| 18 | +{ |
| 19 | + private $output; |
| 20 | + private $buffer = ''; |
| 21 | + private $previousLineWasRemoved = false; |
| 22 | + |
| 23 | + public function __construct(OutputInterface $output) |
| 24 | + { |
| 25 | + $this->output = $output; |
| 26 | + } |
| 27 | + |
| 28 | + public function write($messages, $newline = false, $options = 0) |
| 29 | + { |
| 30 | + $messages = $this->filterMessages($messages, $newline); |
| 31 | + |
| 32 | + $this->output->write($messages, $newline, $options); |
| 33 | + } |
| 34 | + |
| 35 | + public function writeln($messages, $options = 0) |
| 36 | + { |
| 37 | + $messages = $this->filterMessages($messages, true); |
| 38 | + |
| 39 | + $this->output->writeln($messages, $options); |
| 40 | + } |
| 41 | + |
| 42 | + public function setVerbosity($level) |
| 43 | + { |
| 44 | + $this->output->setVerbosity($level); |
| 45 | + } |
| 46 | + |
| 47 | + public function getVerbosity() |
| 48 | + { |
| 49 | + return $this->output->getVerbosity(); |
| 50 | + } |
| 51 | + |
| 52 | + public function isQuiet() |
| 53 | + { |
| 54 | + return $this->output->isQuiet(); |
| 55 | + } |
| 56 | + |
| 57 | + public function isVerbose() |
| 58 | + { |
| 59 | + return $this->output->isVerbose(); |
| 60 | + } |
| 61 | + |
| 62 | + public function isVeryVerbose() |
| 63 | + { |
| 64 | + return $this->output->isVeryVerbose(); |
| 65 | + } |
| 66 | + |
| 67 | + public function isDebug() |
| 68 | + { |
| 69 | + return $this->output->isDebug(); |
| 70 | + } |
| 71 | + |
| 72 | + public function setDecorated($decorated) |
| 73 | + { |
| 74 | + $this->output->setDecorated($decorated); |
| 75 | + } |
| 76 | + |
| 77 | + public function isDecorated() |
| 78 | + { |
| 79 | + return $this->output->isDecorated(); |
| 80 | + } |
| 81 | + |
| 82 | + public function setFormatter(OutputFormatterInterface $formatter) |
| 83 | + { |
| 84 | + $this->output->setFormatter($formatter); |
| 85 | + } |
| 86 | + |
| 87 | + public function getFormatter() |
| 88 | + { |
| 89 | + return $this->output->getFormatter(); |
| 90 | + } |
| 91 | + |
| 92 | + public function fetch(): string |
| 93 | + { |
| 94 | + return $this->buffer; |
| 95 | + } |
| 96 | + |
| 97 | + private function filterMessages($messages, bool $newLine) |
| 98 | + { |
| 99 | + if (!is_iterable($messages)) { |
| 100 | + $messages = [$messages]; |
| 101 | + } |
| 102 | + |
| 103 | + $hiddenPhrases = [ |
| 104 | + 'Generated new migration class', |
| 105 | + 'To run just this migration', |
| 106 | + 'To revert the migration you', |
| 107 | + ]; |
| 108 | + |
| 109 | + foreach ($messages as $key => $message) { |
| 110 | + $this->buffer .= $message; |
| 111 | + |
| 112 | + if ($newLine) { |
| 113 | + $this->buffer .= PHP_EOL; |
| 114 | + } |
| 115 | + |
| 116 | + if ($this->previousLineWasRemoved && !trim($message)) { |
| 117 | + // hide a blank line after a filtered line |
| 118 | + unset($messages[$key]); |
| 119 | + $this->previousLineWasRemoved = false; |
| 120 | + |
| 121 | + continue; |
| 122 | + } |
| 123 | + |
| 124 | + $this->previousLineWasRemoved = false; |
| 125 | + foreach ($hiddenPhrases as $hiddenPhrase) { |
| 126 | + if (false !== strpos($message, $hiddenPhrase)) { |
| 127 | + $this->previousLineWasRemoved = true; |
| 128 | + unset($messages[$key]); |
| 129 | + |
| 130 | + break; |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + return array_values($messages); |
| 136 | + } |
| 137 | +} |
0 commit comments