<commit_before><?php


namespace Gossamer\Horus\EventListeners;

use Gossamer\Horus\EventListeners\EventHandler;
use Monolog\Logger;
use Gossamer\Horus\Core\Request;


class EventDispatcher{
    
    private $listeners = array();
 
    private $logger = null;
    
    private $request = null;
    
    private $container = null;
    
    public function __construct($config = array(), Logger $logger, Request &$request) {
       
        $this->logger = $logger;
        $this->request = $request;
      
        if(count($config) > 0) {
            $this->configListeners($config);
        }
    }
    
    public function setContainer(ContainerInterface $container) {
        $this->container = $container;
    }
    
    public function configListeners(array $listeners) {

        foreach($listeners as $uri => $listener) {
            
            if(array_key_exists('listeners', $listener)  && count($listener['listeners']) > 0) {
           
                try{
                    $this->addEventHandler( $uri, $listener['listeners']);   
                }catch(\Exception $e) {
                    //assume the developer has an empty element such as:
                    //listeners:
                    //with no sub elements, which is allowable                                     
                }                
            }
        }
       
    }
    
    private function addEventHandler($uri, array $listeners) {
        foreach($listeners as $listener) {
             
            $handler = new EventHandler($this->logger, $this->request); 
          
            $handler->addListener($listener); 
       
            $this->listen($uri, $handler);
        } 
           
    }
    
    public function listen($uri, EventHandler $handler) {
       
        $this->listeners[$uri][] = $handler;
    }
 
    public function dispatch($uri, $state, Event &$event) {
     
        if(!array_key_exists($uri, $this->listeners)) {
                
            return;
        }        
     
      
        foreach ($this->listeners[$uri] as $listener)
        {
            $listener->setState($state, $event);
        }
    }
    
    public function getListenerURIs() {
        return array_keys($this->listeners);
    }
}<commit_msg>added key to handlers to prevent duplicate loads<commit_after><?php


namespace Gossamer\Horus\EventListeners;

use Gossamer\Horus\EventListeners\EventHandler;
use Monolog\Logger;
use Gossamer\Horus\Core\Request;


class EventDispatcher{
    
    private $listeners = array();
 
    private $logger = null;
    
    private $request = null;
    
    private $container = null;
    
    public function __construct($config = array(), Logger $logger, Request &$request) {
       
        $this->logger = $logger;
        $this->request = $request;
      
        if(count($config) > 0) {
            $this->configListeners($config);
        }
    }
    
    public function setContainer(ContainerInterface $container) {
        $this->container = $container;
    }
    
    public function configListeners(array $listeners) {

        foreach($listeners as $uri => $listener) {
            
            if(array_key_exists('listeners', $listener)  && count($listener['listeners']) > 0) {
           
                try{
                    $this->addEventHandler( $uri, $listener['listeners']);   
                }catch(\Exception $e) {
                    //assume the developer has an empty element such as:
                    //listeners:
                    //with no sub elements, which is allowable                                     
                }                
            }
        }
       
    }
    
    private function addEventHandler($uri, array $listeners) {
        foreach($listeners as $listener) {
             
            $handler = new EventHandler($this->logger, $this->request); 
          
            $handler->addListener($listener); 
       
            $this->listen($uri, $handler);
        } 
           
    }
    
    public function listen($uri, EventHandler $handler) {
       
        $this->listeners[$uri][get_class($handler)] = $handler;
    }
 
    public function dispatch($uri, $state, Event &$event) {
     
        if(!array_key_exists($uri, $this->listeners)) {
                
            return;
        }        
     
      
        foreach ($this->listeners[$uri] as $listener)
        {
            $listener->setState($state, $event);
        }
    }
    
    public function getListenerURIs() {
        return array_keys($this->listeners);
    }
}
{'fraction_non_alphanumeric': 0.11498028909329829, 'fraction_numerical': 0.0008760402978537013, 'mean_word_length': 1.5919409761634506, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5320892', 'n_tokens_mistral': 1222, 'n_tokens_neox': 1191, 'n_words': 249}