I'm building a cli application with Symfony Console Component. I'm using several other libraries - Monolog among others. I use the logger to log information about the command being executed, exit statuses, etc.
I find that I cannot print or log input arguments that I pass to my command.
When I call $inputArgs = $event->getInput(); in my event listener, I get an object of Symfony\Component\Console\Input\ArgvInput class. This class provides a method to retrieve a value of an argument:
getArgument(string $name) Returns the argument value for a given
argument name.
This method will always return an empty array since 'arguments' are in fact 'tokens'. Here is a screenshot from a debug window:
This seems very misleading. I need to get the values stored in $tokens and I do not want to resort to the global $_SERVER array.
Why are 'arguments' introduced by addArgument() tokens ? And how could I access them without resorting to $_SERVER?
Related
I have project, with some custom ConstraintValidator. I wanted to build custom message. I did so and everyting works just fine. Then we met need to have 'api' module, which means, that you have to split bean validation annotation and ConstraintValidator, have to use #Constraint(validatedBy = {}) and setup pairing manually using ConstraintMapping. Everyting stil works, except for message interpolation.
Log now contains kinda cryptic message:
Expression variables have been defined for constraint interface whatever.SomeValidation while Expression Language is not enabled.
All of that while I'm literary using the same code as one mentioned here:
https://docs.jboss.org/hibernate/stable/validator/reference/en-US/html_single/#_custom_contexts
to disableDefaultConstraintViolation and register custom one.
Any ideas what can cause that?
Given a function 'plugin_update' that would take a parameter $version, how can I pass a value to this method using the following syntax with the add_action method (as the method is within a class):
add_action('plugins_loaded',array('test-app-class','plugin_update'));
I have tried several variations that didn't seem to work. Any advice appreciated!
Short answer: No.
Let me explain why.
WordPress Event Engine Explanation
The add_action construct allows you to register a callback to an event. In your case, the event is plugins_loaded. The event is then fired when the construct do_action() or do_action_ref_array.
What happens when the event fires? WordPress Core does the following:
Assembles the arguments, if any are passed
Sorts the event registry table
Calls each of the registered callbacks in order (as determine by the priority number and when it loaded into the registry), passing the arguments (if any)
The arguments that are passed to each callback is determined by the firing construct. Huh? The do_action() specifies if any arguments will be passed to the callbacks. Then each callback can declare if it wants them or not.
Why Can't You Pass the Version?
The plugins_loaded event does not pass arguments. Your callback cannot specify parameters that are not passed from the firing construct.
In other words, the code that does the `do_action’ determines the arguments that your function can choose to accept and use.
Why? Because the do_action is the main firing mechanism. It says "Hello plugins and themes, I'm running now. If you are registered to me, then I'll call you tomrun too. And I might send you some parameters too."
Options for You
How do you get the version into to your callback?
You have many choices:
storing it in the database if it's dynamic (will change)
store it as an environmental variable and load it
store it as a configuration parameter and load it
As your using OOP, I'd advise injecting it when instantiating the object. I typically inject the dynamic configuration when I create the objects.
Reference
Here is a link to the Core code where WordPress fires do_action( 'plugins_loaded' );: click here to view on GitHub. Notice, there are no arguments being declared, which means none will be passed.
I am having an issue described here :
Can't persist symfony collection with double embedded form
https://stackoverflow.com/questions/27188337/symfony-setter-not-called-on-form-collection-type-even-with-by-reference-fals
I can't get a setter collection to get called even with the property by_reference set to true.
Here my question is how can follow the chain of commands conducting symfony/doctrine to call this setUserIngredients of a userIngredients collection ?
Which functions from the vendors files are called to identify the by_reference => false and call the appropriate setter ?
Thanks a lot !
To follow the chain of calls you can hook up a debugger. For example http://phpdbg.com/ or http://xdebug.org/ alternatively if you already know a specific function that gets called then you can temporarily put var_dump(debug_backtrace()) in your vendor code to see how it gets to that point
I am creating a command to generate accounts from a file. In command I have passed some arguments.
$this
->setName('batch:create')
->setDescription('xyz')
->setHelp('xyz')
->addArgument('account-id', InputArgument::REQUIRED, "Set the account id.")
->addArgument('name', InputArgument::REQUIRED, "Set the account name.");
I was just thinking if there is any way I can check type of argument passed. For now I am checking it like this,
if (is_numeric($input->getArgument('account-id'))) {
// ....
}
Is there anyway I can create a validator that checks the type and I just have to call validate function.
if ($input->validate() === false) {
// show error message and return.
}
Unfortunately, currently there's no way to implement command argument validation in Symfony. The best way to implement these checks would be overriding Symfony\Component\Console\Command::initialize method in your command and then applying the validation rules there, throwing exceptions if passed arguments are invalid.
Update: Matthias Noback has implemented symfony-console-form (https://github.com/matthiasnoback/symfony-console-form), and looks like implementing Matthias\SymfonyConsoleForm\Console\Command\FormBasedCommand interface would give you basic validation abilities through the form component (have to test it with validation, though).
In my Symfony2 project I am retrieving an ordered set of entity IDs from an Elasticsearch index. I'm then passing this list to Doctrine2 to retrieve the actual entities, by way of a WHERE IN() call.
This doesn't return them in the correct order, so I think I need to use the MySQL-specific FIELD() function. I've created a custom DQL function to allow the functionality.
So now I'm using the following code to build a Doctrine query object, but the parameters aren't being parsed into the select() method:
$itemIds = array(4,8,2,1);
$this->getRepository()
->createQueryBuilder('i')
->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')
->where('i.id IN (:ids)')
->setParameters(array(
'ids_string' => implode(',', $itemIds),
'ids' => $itemIds))
->orderBy('fixed_order', 'ASC')
->getQuery()
;
This fails with the error "Invalid parameter number: number of bound variables does not match number of tokens", so apparently it's not "seeing" the :ids_string in the select() method.
I initially tried putting the FIELD() function in the orderBy() call, but it doesn't look like this is getting parsed for custom DQL function calls, and I imagine I'd run into the same problem as above.
EDIT 1 I'm aware I could put the base data directly into the select() call.
EDIT 2 I've given up and put the bare data into the select() call (which I wanted to avoid). This worked, but then it became necessary to implement Koc's suggestion of using the HIDDEN keyword to prevent Doctrine returning array(Object i, array(fixed_order)) instead of just Object i
From Doctrine 2.2 you can use HIDDEN keyword for avability field in order by without hydration them.
Try:
->select('i, FIELD(i.id, :ids_string) AS HIDDEN fixed_order')
You're going to kick yourself when you notice the problem...
Try re-reading your sentence: "so apparently it's not "seeing" the :ids_string in the select() method".
And then take a close look at your code: 'id_string' => implode(',', $itemIds)