Koowa_Mixin
[ class tree: Koowa_Mixin ] [ index: Koowa_Mixin ] [ all elements ]

Source for file command.php

Documentation is available at command.php

  1. <?php
  2. /**
  3.  * @version     $Id: command.php 4628 2012-05-06 19:56:43Z johanjanssens $
  4.  * @package     Koowa_Mixin
  5.  * @copyright   Copyright (C) 2007 - 2010 Johan Janssens. All rights reserved.
  6.  * @license     GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
  7.  * @link        http://www.nooku.org
  8.  */
  9.  
  10. /**
  11.  * Command Mixin
  12.  * 
  13.  * Class can be used as a mixin in classes that want to implement a chain
  14.  * of responsability or chain of command pattern.
  15.  *  
  16.  * @author      Johan Janssens <johan@nooku.org>
  17.  * @package     Koowa_Mixin
  18.  * @uses        KCommandChain
  19.  * @uses        KCommandInterface
  20.  * @uses        KCommandEvent
  21.  */
  22. class KMixinCommand extends KMixinAbstract
  23. {   
  24.     /**
  25.      * Chain of command object
  26.      *
  27.      * @var KCommandChain 
  28.      */
  29.     protected $_command_chain;
  30.     
  31.     /**
  32.      * Object constructor
  33.      *
  34.      * @param   object  An optional KConfig object with configuration options
  35.      */
  36.     public function __construct(KConfig $config)
  37.     {
  38.         parent::__construct($config);
  39.         
  40.         if(is_null($config->command_chain)) {
  41.             throw new KMixinException('command_chain [KCommandChain] option is required');
  42.         }
  43.             
  44.         //Create a command chain object 
  45.         $this->_command_chain = $config->command_chain;
  46.         
  47.         //Set the mixer in the config
  48.         $config->mixer $this->_mixer;
  49.         
  50.         //Mixin the callback mixer if callbacks have been enabled
  51.         if($config->enable_callbacks{
  52.             $this->_mixer->mixin(new KMixinCallback($config));
  53.         }
  54.         
  55.         //Enqueue the event command with a lowest priority to make sure it runs last
  56.         if($config->dispatch_events
  57.         
  58.             $this->_mixer->mixin(new KMixinEvent($config));
  59.             
  60.             //@TODO : Add KCommandChain::getCommand()     
  61.             $event $this->_command_chain->getService('koowa:command.event'array(
  62.                 'event_dispatcher' => $config->event_dispatcher
  63.             ));
  64.             
  65.             $this->_command_chain->enqueue($event$config->event_priority);
  66.         }
  67.     }
  68.     
  69.     /**
  70.      * Initializes the options for the object
  71.      * 
  72.      * Called from {@link __construct()} as a first step of object instantiation.
  73.      *
  74.      * @param   object  An optional KConfig object with configuration options
  75.      * @return  void 
  76.      */
  77.     protected function _initialize(KConfig $config)
  78.     {
  79.         $config->append(array(
  80.             'command_chain'     => null,
  81.             'event_dispatcher'  => null,
  82.             'dispatch_events'   => true,
  83.             'event_priority'    => KCommand::PRIORITY_LOWEST,
  84.             'enable_callbacks'  => false,
  85.             'callback_priority' => KCommand::PRIORITY_HIGH,
  86.         ));
  87.         
  88.         parent::_initialize($config);
  89.     }
  90.     
  91.     /**
  92.      * Get the command chain context
  93.      * 
  94.      * This functions inserts a 'caller' variable in the context which contains
  95.      * the mixer object.
  96.      *
  97.      * @return  KCommandContext 
  98.      */
  99.     public function getCommandContext()
  100.     {
  101.         $context $this->_command_chain->getContext();
  102.         $context->caller $this->_mixer;
  103.         
  104.         return $context;
  105.     }
  106.     
  107.     /**
  108.      * Get the chain of command object
  109.      *
  110.      * @return  KCommandChain 
  111.      */
  112.     public function getCommandChain()
  113.     {
  114.         return $this->_command_chain;
  115.     }
  116.     
  117.     /**
  118.      * Set the chain of command object
  119.      *
  120.      * @param   object     command chain object
  121.      * @return  KObject The mixer object
  122.      */
  123.     public function setCommandChain(KCommandChain $chain)
  124.     {
  125.         $this->_command_chain = $chain;
  126.         return $this->_mixer;
  127.     }
  128.     
  129.     /**
  130.      * Preform a deep clone of the object.
  131.      *
  132.      * @retun void
  133.      */
  134.     public function __clone()
  135.     {
  136.         $this->_command_chain = clone $this->_command_chain;    
  137.     }
  138. }

Documentation generated on Fri, 24 May 2013 03:01:09 +0200 by phpDocumentor 1.4.3