Skip to content

[Console] Helper instances should be new for each Command #21417

@wjaspers

Description

@wjaspers

Previously, #14505 highlighted the same issue I am experiencing. However, I believe the issue closed prematurely because it attempted to change the behavior of Command.

Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.x.x, 3.x.x
Affected Helpers Progress, Table

Because the Application is a shared instance amongst Console Commands, and the HelperSet is generated from it, all Console Commands share the same default helpers.

This has the unintended side-effect of triggering errors or exceptions when one command may call another, and both use the same Helpers.

For example:

<?php

class CommandA extends \Symfony\Component\Console\Command\Command {
...
protected $name = 'command-a';

public function execute(InputInterface $input, OutputInterface $output) {
  $progress = $this->getHelperSet()->get('progress');
  $progress->start($output, 10);
  $progress->advance();
  $progress->finish();
  return 0;
}
...
}

class CommandB extends \Symfony\Component\Console\Command\Command {
...
protected $name = 'command-b';
public function execute(InputInterface $input, OutputInterface $output) {
   $commandA =  $this->application->get('command-a');
   $commandA->execute($input, $output);

  $progress = $this->getHelperSet()->get('progress');
  $progress->start($output, 10);
  $progress->advance();
  $progress->finish();
  return 0;
}
...
}

Unless, I specifically call clone $this->getHelperSet()->get('progress'), the same instance is used, which triggers a LogicException.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy