Giter VIP home page Giter VIP logo

Comments (6)

Chi-teck avatar Chi-teck commented on July 26, 2024 1

Ideally, the Input handler should not have state at all. That could be achived if we pass askedQuestions as a parameter to the collectVars() method. Unfortunetly that would make the caller code a way more verbose. Initialization the handler on each command will work just for your case. That might not be desirable for other cases. For example someone may want to run second command that shares answers option with the first one. So that I think the inputHandler should be instantiated consciously when needed.

from drupal-code-generator.

Chi-teck avatar Chi-teck commented on July 26, 2024

That needs more details or better a reference to code that implements two commands.

from drupal-code-generator.

richardgaunt avatar richardgaunt commented on July 26, 2024

Thank you for the speedy reply.

Here is an example implementation - note how it's a command calling other command's within the structure.

The problem is the HelperSet is initialised in Application (see) and InputHandler stores the questions already asked questions on a property questions because it is only initialised once, commands called within commands and then when we call the same command several times like iterating through a csv, the questions have already been asked according to the InputHandler (see) and so the $vars are the same as the first time we called that command.

<?php


use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\Console\Output\OutputInterface;

class CustomGenerator extends \DrupalCodeGenerator\Command\BaseGenerator {

  /**
   * {@inheritdoc}
   */
  protected function interact(InputInterface $input, OutputInterface $output) {
    parent::initialize($input, $output); // TODO: Change the autogenerated stub
    $rows = $this->getRowFromCSV();
    foreach ($rows as $row) {
      $cmd = $this->getCommandNameFromRow($row);
      /** @var \Symfony\Component\Console\Command\Command $command */
      $command = $this->getApplication()->find($cmd);
      $sub_input = new ArrayInput([
        '--answers' => json_encode($this->getCommandArguments($cmd, $row))
      ]);
      $sub_output = new ConsoleOutput();
      $command->initialize($sub_input, $sub_output);
      // I can call the same command many  times as it is iterating through a csv but the options passed do not override the first time we run it.
      $return_code = $command->run($sub_input, $sub_output);
    }
  }

}

from drupal-code-generator.

Chi-teck avatar Chi-teck commented on July 26, 2024

Helper is set on Application level same way as in Symfony Console. I could think of two options.

  1. Replace the helper set with new instance in the loop. Something like this $this->getHelperSet()->set(new Helper()).
  2. Initialize a new DCG application for the second command. That's how drush generate embeds DCG.
    https://github.com/drush-ops/drush/blob/10.6.0/src/Commands/generate/GenerateCommands.php#L58

from drupal-code-generator.

richardgaunt avatar richardgaunt commented on July 26, 2024

I've done your first suggestion to overcome in my application and agree that's the way Symfony sets helpers, so I guess what I am saying is:

It just seems to be a problem that InputHandler holds state between commands and it's not an obvious problem until you have done what I've done.

Maybe InputHandler should initialise on each command as you suggest in point 1? I'm happy to create a patch if you agree?

from drupal-code-generator.

richardgaunt avatar richardgaunt commented on July 26, 2024

That's a fair design decision.
Thanks for your work with the library, it's an amazing collection.

from drupal-code-generator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.