Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
Issue #88: Refactor event handling
Browse files Browse the repository at this point in the history
- Move CommandHandlerEvent implementation into BehaviorEventable
- Make events immutable by default
  • Loading branch information
johanjanssens committed May 16, 2016
1 parent 6648d0f commit 36be900
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 26 deletions.
59 changes: 38 additions & 21 deletions code/command/handler/event.php → code/behavior/eventable.php
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
<?php
/**
* Kodekit - http:https://timble.net/kodekit
*
* @copyright Copyright (C) 2007 - 2016 Johan Janssens and Timble CVBA. (http:https://www.timble.net)
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
* @link https://github.com/timble/kodekit for the canonical source repository
*/
* Kodekit - http:https://timble.net/kodekit
*
* @copyright Copyright (C) 2007 - 2016 Johan Janssens and Timble CVBA. (http:https://www.timble.net)
* @license MPL v2.0 <https://www.mozilla.org/en-US/MPL/2.0>
* @link https://github.com/timble/kodekit for the canonical source repository
*/

namespace Kodekit\Library;

/**
* Event Command Handler
* Eventable Behavior
*
* The event handler will translate the command name to a onCommandName format and let the event publisher publish
* This behavior will translate the command name to a onCommandName format and let the event publisher publish
* to any registered event listeners.
*
* The 'immutable' config option defines if the context is cloned before being passed to the event publisher or
* or passed by reference instead. By default the context is passed by reference.
* The 'event_immutable' config option defines if the command context is cloned before being passed to the event
* publisher or or passed by reference instead. By default the context is cloned.
*
* @author Johan Janssens <http:https://github.com/johanjanssens>
* @package Kodekit\Library\Command\Handler
* @package Kodekit\Library\Behavior
*/
final class CommandHandlerEvent extends CommandHandlerAbstract implements ObjectSingleton
class BehaviorEventable extends BehaviorAbstract
{
/**
* The command priority
Expand All @@ -35,7 +35,7 @@ final class CommandHandlerEvent extends CommandHandlerAbstract implements Object
*
* @var boolean
*/
protected $_immutable;
protected $_event_immutable;

/**
* Object constructor
Expand All @@ -55,7 +55,7 @@ public function __construct(ObjectConfig $config)
$this->__event_publisher = $config->event_publisher;

//Set the immutable state of the handler
$this->_immutable = $config->immutable;
$this->_event_immutable = $config->event_immutable;
}

/**
Expand All @@ -71,7 +71,7 @@ protected function _initialize(ObjectConfig $config)
$config->append(array(
'priority' => self::PRIORITY_LOWEST,
'event_publisher' => 'event.publisher',
'immutable' => false,
'event_immutable' => true,
));

parent::_initialize($config);
Expand Down Expand Up @@ -140,21 +140,25 @@ public function execute(CommandInterface $command, CommandChainInterface $chain)

$parts = explode('.', $command->getName());
$when = array_shift($parts); // Before or After
$name = StringInflector::implode($parts); // Read Dispatch Select etc.
$name = StringInflector::implode($parts); // Action

// Create Specific and Generic event names
$event_specific = 'on'.ucfirst($when).ucfirst($package).ucfirst($subject).ucfirst($type).$name;
$event_generic = 'on'.ucfirst($when).ucfirst($type).$name;

// Clone the context
if($this->_immutable) {
if($this->isEventImmutable()) {
$event = clone($command);
} else {
$event = $command;
}

// Create event object to check for propagation
$event = $this->getEventPublisher()->publishEvent($event_specific, $event->getAttributes(), $event->getSubject());
$event = $this->getEventPublisher()->publishEvent(
$event_specific,
$event->getAttributes(),
$event->getSubject()
);

// Ensure event can be propagated and event name is different
if ($event->canPropagate() && $event_specific != $event_generic)
Expand All @@ -165,12 +169,25 @@ public function execute(CommandInterface $command, CommandChainInterface $chain)
}

/*
* Is the command context immutable
* Is the event is immutable
*
* @return bool
*/
public function isImmutable()
public function isEventImmutable()
{
return $this->_event_immutable;
}

/**
* Get an object handle
*
* Force the object to be enqueue in the command chain.
*
* @return string A string that is unique, or NULL
* @see execute()
*/
public function getHandle()
{
return $this->_immutable;
return ObjectMixinAbstract::getHandle();
}
}
3 changes: 1 addition & 2 deletions code/controller/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,11 @@ public function __construct( ObjectConfig $config)
protected function _initialize(ObjectConfig $config)
{
$config->append(array(
'command_handlers' => array('lib:command.handler.event'),
'dispatched' => false,
'request' => array(),
'response' => array(),
'user' => array(),
'behaviors' => array('permissible'),
'behaviors' => array('permissible', 'lib:behavior.eventable'),
));

parent::_initialize($config);
Expand Down
2 changes: 1 addition & 1 deletion code/model/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function _initialize(ObjectConfig $config)
'identity_key' => null,
'state' => 'lib:model.state',
'state_defaults' => array(),
'command_handlers' => array('lib:command.handler.event'),
'behaviors' => array('lib:behavior.eventable')
));

parent::_initialize($config);
Expand Down
3 changes: 1 addition & 2 deletions code/view/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,12 @@ protected function _initialize(ObjectConfig $config)
$config->append(array(
'data' => array(),
'parameters' => array(),
'command_handlers' => array('lib:command.handler.event'),
'model' => 'lib:model.empty',
'content' => '',
'mimetype' => 'application/octet-stream ',
'url' => $this->getObject('lib:http.url'),
'title' => ucfirst($this->getName()),
'behaviors' => array()
'behaviors' => array('lib:behavior.eventable')
));

parent::_initialize($config);
Expand Down

0 comments on commit 36be900

Please sign in to comment.