Understanding Chains

Brico::Command supports the Chain of Responsability pattern, a chain represents a list of Commands that will be executed sequentially by the RequestHandler. Each chain must have an associated ChainHandler that takes care of all events generated when the chain is executed, which are:

  • onChainBegin - published beforee processing the Chain.
  • onChainEnd - published after successfuly processing the Chain.
  • onCommandBegin - published before the execution of a chained Command.
  • onCommandEnd - published after the successful execution of a chained Command.
  • onChainBroken - published when a Command sets an error property on the Response.
  • onExceptionThrown - published when a Command throws an exception.
Brico::Command has a trick up its sleeve when executing a Chain and some of its commands share a form property name. Suppouse for a moment that a Chain has 2 commands, and that each command has a Form with a name attribute; the first Command appends "1" to the 'name' property of its Form and puts the result on the Response under the 'name' attribute; the second Command appends "2" to the 'name' property of its Form and puts the result on the Response under a 'result' attribute. With this setup in place, lets say we put 'Brico' as a value of an attribute 'name' under the Request and call the Chain, the output will be 'Brico12', shocked? were you guessing it would be 'Brico2'? well the answer lies in how the default RequestHandler processes the Form properties. It first copies all matching attributes from the Request and then copies all matching attributes from the Response, overwritting any shared attributes between the Request and the Response, this mode of operation assures that a Command will get the most recent value of a shared attribute without it explicitely knowing that it forms part of a chain, as chained commands may be executed outside the Chain.