Security update for Core, with self-updated composer
[yaffs-website] / vendor / symfony / console / Helper / ProgressBar.php
index 89ca85d2f1071a4aed61029375bd84bb4d8f3a51..5ac8640689bc83b9e0a4cb5c9c375fc0401f6898 100644 (file)
@@ -14,6 +14,7 @@ namespace Symfony\Component\Console\Helper;
 use Symfony\Component\Console\Output\ConsoleOutputInterface;
 use Symfony\Component\Console\Output\OutputInterface;
 use Symfony\Component\Console\Exception\LogicException;
+use Symfony\Component\Console\Terminal;
 
 /**
  * The ProgressBar provides helpers to display progress output.
@@ -44,14 +45,13 @@ class ProgressBar
     private $formatLineCount;
     private $messages = array();
     private $overwrite = true;
+    private $terminal;
     private $firstRun = true;
 
     private static $formatters;
     private static $formats;
 
     /**
-     * Constructor.
-     *
      * @param OutputInterface $output An OutputInterface instance
      * @param int             $max    Maximum steps (0 if unknown)
      */
@@ -63,6 +63,7 @@ class ProgressBar
 
         $this->output = $output;
         $this->setMaxSteps($max);
+        $this->terminal = new Terminal();
 
         if (!$this->output->isDecorated()) {
             // disable overwrite when output does not support ANSI codes.
@@ -83,7 +84,7 @@ class ProgressBar
      * @param string   $name     The placeholder name (including the delimiter char like %)
      * @param callable $callable A PHP callable
      */
-    public static function setPlaceholderFormatterDefinition($name, $callable)
+    public static function setPlaceholderFormatterDefinition($name, callable $callable)
     {
         if (!self::$formatters) {
             self::$formatters = self::initPlaceholderFormatters();
@@ -181,20 +182,6 @@ class ProgressBar
         return $this->max;
     }
 
-    /**
-     * Gets the progress bar step.
-     *
-     * @deprecated since version 2.6, to be removed in 3.0. Use {@link getProgress()} instead.
-     *
-     * @return int The progress bar step
-     */
-    public function getStep()
-    {
-        @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0. Use the getProgress() method instead.', E_USER_DEPRECATED);
-
-        return $this->getProgress();
-    }
-
     /**
      * Gets the current step position.
      *
@@ -208,11 +195,9 @@ class ProgressBar
     /**
      * Gets the progress bar step width.
      *
-     * @internal This method is public for PHP 5.3 compatibility, it should not be used.
-     *
      * @return int The progress bar step width
      */
-    public function getStepWidth()
+    private function getStepWidth()
     {
         return $this->stepWidth;
     }
@@ -234,7 +219,7 @@ class ProgressBar
      */
     public function setBarWidth($size)
     {
-        $this->barWidth = (int) $size;
+        $this->barWidth = max(1, (int) $size);
     }
 
     /**
@@ -354,30 +339,12 @@ class ProgressBar
      * Advances the progress output X steps.
      *
      * @param int $step Number of steps to advance
-     *
-     * @throws LogicException
      */
     public function advance($step = 1)
     {
         $this->setProgress($this->step + $step);
     }
 
-    /**
-     * Sets the current progress.
-     *
-     * @deprecated since version 2.6, to be removed in 3.0. Use {@link setProgress()} instead.
-     *
-     * @param int $step The current progress
-     *
-     * @throws LogicException
-     */
-    public function setCurrent($step)
-    {
-        @trigger_error('The '.__METHOD__.' method is deprecated since version 2.6 and will be removed in 3.0. Use the setProgress() method instead.', E_USER_DEPRECATED);
-
-        $this->setProgress($step);
-    }
-
     /**
      * Sets whether to overwrite the progressbar, false for new line.
      *
@@ -392,18 +359,15 @@ class ProgressBar
      * Sets the current progress.
      *
      * @param int $step The current progress
-     *
-     * @throws LogicException
      */
     public function setProgress($step)
     {
         $step = (int) $step;
-        if ($step < $this->step) {
-            throw new LogicException('You can\'t regress the progress bar.');
-        }
 
         if ($this->max && $step > $this->max) {
             $this->max = $step;
+        } elseif ($step < 0) {
+            $step = 0;
         }
 
         $prevPeriod = (int) ($this->step / $this->redrawFreq);
@@ -445,25 +409,7 @@ class ProgressBar
             $this->setRealFormat($this->internalFormat ?: $this->determineBestFormat());
         }
 
-        // these 3 variables can be removed in favor of using $this in the closure when support for PHP 5.3 will be dropped.
-        $self = $this;
-        $output = $this->output;
-        $messages = $this->messages;
-        $this->overwrite(preg_replace_callback("{%([a-z\-_]+)(?:\:([^%]+))?%}i", function ($matches) use ($self, $output, $messages) {
-            if ($formatter = $self::getPlaceholderFormatterDefinition($matches[1])) {
-                $text = call_user_func($formatter, $self, $output);
-            } elseif (isset($messages[$matches[1]])) {
-                $text = $messages[$matches[1]];
-            } else {
-                return $matches[0];
-            }
-
-            if (isset($matches[2])) {
-                $text = sprintf('%'.$matches[2], $text);
-            }
-
-            return $text;
-        }, $this->format));
+        $this->overwrite($this->buildLine());
     }
 
     /**
@@ -633,4 +579,44 @@ class ProgressBar
             'debug_nomax' => ' %current% [%bar%] %elapsed:6s% %memory:6s%',
         );
     }
+
+    /**
+     * @return string
+     */
+    private function buildLine()
+    {
+        $regex = "{%([a-z\-_]+)(?:\:([^%]+))?%}i";
+        $callback = function ($matches) {
+            if ($formatter = $this::getPlaceholderFormatterDefinition($matches[1])) {
+                $text = call_user_func($formatter, $this, $this->output);
+            } elseif (isset($this->messages[$matches[1]])) {
+                $text = $this->messages[$matches[1]];
+            } else {
+                return $matches[0];
+            }
+
+            if (isset($matches[2])) {
+                $text = sprintf('%'.$matches[2], $text);
+            }
+
+            return $text;
+        };
+        $line = preg_replace_callback($regex, $callback, $this->format);
+
+        // gets string length for each sub line with multiline format
+        $linesLength = array_map(function ($subLine) {
+            return Helper::strlenWithoutDecoration($this->output->getFormatter(), rtrim($subLine, "\r"));
+        }, explode("\n", $line));
+
+        $linesWidth = max($linesLength);
+
+        $terminalWidth = $this->terminal->getWidth();
+        if ($linesWidth <= $terminalWidth) {
+            return $line;
+        }
+
+        $this->setBarWidth($this->barWidth - $linesWidth + $terminalWidth);
+
+        return preg_replace_callback($regex, $callback, $this->format);
+    }
 }