Comments (6)
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.
That needs more details or better a reference to code that implements two commands.
from drupal-code-generator.
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.
Helper is set on Application level same way as in Symfony Console. I could think of two options.
- Replace the helper set with new instance in the loop. Something like this
$this->getHelperSet()->set(new Helper())
. - 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.
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.
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)
- Allow to name provided answers instead of ordering them HOT 1
- Parent QuestionHelper class is instantiated in some cases and causes a fatal error HOT 4
- Update plugin generators to use PHP attributes HOT 5
- Allow a custom module to be specified as the target location for a new sdc HOT 1
- Drush code gen fails to generate Drush Alias File HOT 1
- Drupal Entity Generator fails to generate functional entity HOT 1
- Drupal Code Generator generates Constraint Validator with wrong constructor definition HOT 2
- Single Directory Component generator refers to metadata.json instead of .component.yml file
- Provide a replacement for Drupal Console generate:ajax:command HOT 2
- Apply Drupal code style for declare(strict_types=1);
- Class naming convention HOT 6
- Add a command to generate batch service
- Config entity form template does not use entity class for machine name exists check
- Add a command to generate plugin derivative
- Release for Symfony 7 compatibility HOT 4
- Revise generator list for 4.x HOT 5
- Use PHP attributes for plugins HOT 2
- Migrate to PhpStan
- Commit fails after update to 3.6.0 HOT 3
- Create an access policy generator
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from drupal-code-generator.