Esempio n. 1
0
 /**
  * Execute a command by executing all registered handlers
  *
  * If a command handler returns the 'break condition' the executing is halted. If no break condition is specified the
  * the command chain will execute all command handlers, regardless of the handler result returned.
  *
  * @param  string|CommandInterface  $command    The command name or a CommandInterface object
  * @param  array|\Traversable         $attributes An associative array or a Traversable object
  * @param  ObjectInterface          $subject    The command subject
  * @return mixed|null If a handler breaks, returns the break condition. NULL otherwise.
  */
 public function execute($command, $attributes = null, $subject = null)
 {
     $result = null;
     if ($this->isEnabled()) {
         $this->__stack->push(clone $this->__queue);
         //Make sure we have an command object
         if (!$command instanceof CommandInterface) {
             if ($attributes instanceof CommandInterface) {
                 $name = $command;
                 $command = $attributes;
                 $command->setName($name);
             } else {
                 $command = new Command($command, $attributes, $subject);
             }
         }
         //Set the command subject
         if ($subject) {
             $command->setSubject($subject);
         }
         foreach ($this->__stack->peek() as $handler) {
             $result = $handler->execute($command, $this);
             if ($result === $this->getBreakCondition()) {
                 break;
             }
         }
         $this->__stack->pop();
     }
     return $result;
 }