Skip to content

ProgressBar placeholder formats don't have access to information available at initialization #31193

@ostrolucky

Description

@ostrolucky

There is a design issue with progress bar placeholder formats. They don't have access to information I wish to pass to ProgressBar when it's being constructed. For example

ProgressBar::setPlaceholderFormatterDefinition('speed', function (ProgressBar $bar) {
    return Helper::formatMemory($bar->getProgress() / max(1, time() - $bar->startTime)).'/s';
});

This doesn't work, because there is no $bar->startTime property and I have no way to pass it to ProgressBar so placeholder can retrieve it.

Similarly, I wanted to show IP of client that is being connected to server

ProgressBar::setPlaceholderFormatterDefinition('host', function (ProgressBar $bar) {
    return $bar->host;
});

Of course, it has same problem.

I guess I could workaround it by passing the variable via use, but this would require to:

  • have setPlaceholderFormatterDefinition call always in same scope as ProgressBar is being initialized
  • unique placeholder identifier for each progress bar

That's too much of expensive workarounds. These placeholders are designed to be defined once at application bootstrap and reused.

I don't know what's best course of action to solve this, I have couple ideas, but I'm not fan of either

  1. allow to pass arbitrary data to ProgressBar
  2. allow to pass arbitrary data to OutputInterface (closure is receiving it as second parameter)
  3. pass to closure $this + func_get_args (similar to 1. I guess)
  4. make ProgressBar open to inheritance again

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions