Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 9f705be713
Fetching contributors…

Cannot retrieve contributors at this time

file 111 lines (102 sloc) 2.999 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
<?php
/**
* Tracks CQRS Framework
*
* PHP Version 5.3
*
* @category Tracks
* @package EventHandler
* @author Sean Crystal <sean.crystal@gmail.com>
* @copyright 2011 Sean Crystal
* @license http://www.opensource.org/licenses/BSD-3-Clause BSD 3-Clause
* @link https://github.com/spiralout/Tracks
*/

namespace Tracks\EventHandler;

/**
* Routes to an event handler by referencing a lookup object
*
* Configuration should be INI-style in the format:
*
* [Event1Classname]
* handlers[] = EventHandler1Classname
* handlers[] = EventHandler2Classname
*
* [Event2Classname]
* handlers[] = EventHandler3Classname
* ...
*
* @category Tracks
* @package EventHandler
* @author Sean Crystal <sean.crystal@gmail.com>
* @author Doug Hurst <doug.hurst@gmail.com>
* @copyright 2011 Sean Crystal
* @license http://www.opensource.org/licenses/BSD-3-Clause BSD 3-Clause
* @link https://github.com/spiralout/Tracks
*/
class ConfigBasedRouter implements IEventRouter
{
    /**
* @var array An associative array of events to an array of handlers
*/
    private $_config;

    /**
* @var array<IEventHandler> Associative array of Event Handlers
*/
    private $_handlers = array();

    /**
* Route an event
*
* @param Tracks\Event\Base $event An Event
*
* @return null
*/
    public function route(\Tracks\Event\Base $event)
    {
        $eventClass = get_class($event);
        if (isset($this->_handlers[$eventClass])) {
            foreach ($this->_handlers[$eventClass] as $handler) {
                $handler->execute($event);
            }
        } else if (isset($this->_config[$eventClass])) {
            foreach ($this->_config[$eventClass]['handlers'] as $handlerClass) {
                $this->addHandler($eventClass, new $handlerClass());
            }
            $this->route($event);
        }
    }

    /**
* Add an event handler to the routing table
*
* @param string $eventClass The Event classname
* @param IEventHandler $handler An EventHandler
*
* @return null
*/
    public function addHandler($eventClass, $handler)
    {
        assert('is_string($eventClass)');
        assert('class_exists($eventClass)');

        if (!($handler instanceof IEventHandler)) {
            throw new \LogicException('Event handlers must implement IEventHandler');
        }

        if (!isset($this->_handlers[$eventClass])) {
            $this->_handlers[$eventClass] = array();
        }

        $this->_handlers[$eventClass][] = $handler;
    }

    /**
* Routing Configuration Mutator
*
* @param string $filename The configuration filename
*
* @return null
*/
    public function __construct($filename)
    {
        if (file_exists($filename)) {
            $this->_config = parse_ini_file($filename, true);
        } else {
            throw new \InvalidArgumentException();
        }
    }
}
Something went wrong with that request. Please try again.