laracasts/Commander

Make mapInputToCommand optional in CommanderTrait

Opened this issue · 7 comments

Hi Jeffrey,

For CommanderTrait I thought it might be handy to make mapInputToCommand in the execute method optional because sometimes you may wish to build up a command object that is not directly tied to a form input. For example, say you want to pass the currently authenticated user's id into the command. This value would not come from a form input, but directly from the controller. What do you think? Am I missing something totally obvious? As always, thanks, you do great work!

It's already optional!
If you pass a variable as second argument of execute method of "CommanderTrait", "mapInputToCommand" won't execute.

Hi miladir

When I checked the source I see execute always calls mapInputToCommand and reflects into your command class no matter what you give as second argument. I'm saying that for times that you don't need to have your class reflected into we could have a simple optional parameter on execute that would optionally abort calling mapInputToCommand. Otherwise, you are always stuck with input::all, or a default value on your command class parameter, or having to pass your own array. Perhaps passing your own array is what Jeff intended?

"mapInputToCommand" could not be optional because trait is responsible for creating a command object and than pass it to CommandBus.
If you pass null as second argument, "execute" will make a Command object from Input::all(), otherwise it will make Command object from input array.

Yeah I see that, I was just thinking say you had a command object already made, and then just pass it to execute with a flag to not reflect. In that case you wouldn't have to construct that input array. But no big deal if you don't like my idea.

You wouldn't even have to have a flag, mapInputToCommand could run depending on if you passed a class or an existing object to execute

Your idea works too but the idea behind CommandBus design pattern is making code in Controller as close as possible to real world Business language. In real world business a manager will say to his/her employers to do something with the input data. Manager is not responsible to shape data. Employer should shape data before use it and report to Manager that the job is done or fail.

You could do this in the execute method. It would allow an existing command object to pass through and not be reflected.

if( ! is_object($command)) {
    $command = $this->mapInputToCommand($command, $input);
 }