<commit_before><?php
namespace System;

class Events extends \System\Singleton {
    private $listeners;
    
    function __construct() {
        parent::__construct();
        
        $this->loadModuleListeners();
    }
    
    private function loadModuleListeners() {
        // Define dir- and filenames
        $moduledir = 'modules';
        $listenerfile = 'listeners.json';
        
        // Open the directory
        $fh = opendir($moduledir);
    
        // Read listeners from all the files inside the module directories
        if ($fh == false) {
            return false;
        }
        else {
            while ($item = readdir($fh)) {
                $item = $moduledir.'/'.$item;
                if (!is_dir($item)) {
                    continue;
                }
                if (file_exists($item.'/'.$listenerfile)) {
                    $listeners = json_decode(file_get_contents($item.'/'.$listenerfile), true);
                    foreach ($listeners as $k=>$v) {
                        if (isset($v['event']) and isset($v['listener'])) {
                            $this->addListener($v['event'], $v['listener']);
                        }
                    }
                }
            }
        }
    }
    
    function fireEvent($name, &$data = null) {
        if (!empty($this->listeners[$name])) {
            foreach ($this->listeners[$name] as $k=>$v) {
                if ($data != null) {
                    call_user_func($v, $data);
                }
                else { 
                    call_user_func($v);
                }
            }
        }
        return $data;
    }
    
    function addListener($event, $function, $persistent = false) {
        // Make sure the event does not exist in the array of listeners
        $this->deleteListener($event, $function);
        
        // Then (re-)add the listener to the array
        $this->listeners[$event][] = $function;
        
        return true;
    }
    
    function deleteListener($event, $function) {
        if (!empty($this->listeners[$event])) {
            foreach ($this->listeners[$event] as $k=>$v) {
                if ($v == $function) {
                    unset($this->listeners[$event][$k]);
                    return true;
                }
            }
        }
        return false;
    }
}<commit_msg>Tiny bugfix in the event firing<commit_after><?php
namespace System;

class Events extends \System\Singleton {
    private $listeners;
    
    function __construct() {
        parent::__construct();
        
        $this->loadModuleListeners();
    }
    
    private function loadModuleListeners() {
        // Define dir- and filenames
        $moduledir = 'modules';
        $listenerfile = 'listeners.json';
        
        // Open the directory
        $fh = opendir($moduledir);
    
        // Read listeners from all the files inside the module directories
        if ($fh == false) {
            return false;
        }
        else {
            while ($item = readdir($fh)) {
                $item = $moduledir.'/'.$item;
                if (!is_dir($item)) {
                    continue;
                }
                if (file_exists($item.'/'.$listenerfile)) {
                    $listeners = json_decode(file_get_contents($item.'/'.$listenerfile), true);
                    foreach ($listeners as $k=>$v) {
                        if (isset($v['event']) and isset($v['listener'])) {
                            $this->addListener($v['event'], $v['listener']);
                        }
                    }
                }
            }
        }
    }
    
    function fireEvent($name, &$data = null) {
        if (!empty($this->listeners[$name])) {
            foreach ($this->listeners[$name] as $k=>$v) {
                if ($data !== null) {
                    call_user_func_array($v, array(&$data));
                }
                else { 
                    call_user_func($v);
                }
            }
        }
        return $data;
    }
    
    function addListener($event, $function, $persistent = false) {
        // Make sure the event does not exist in the array of listeners
        $this->deleteListener($event, $function);
        
        // Then (re-)add the listener to the array
        $this->listeners[$event][] = $function;
        
        return true;
    }
    
    function deleteListener($event, $function) {
        if (!empty($this->listeners[$event])) {
            foreach ($this->listeners[$event] as $k=>$v) {
                if ($v == $function) {
                    unset($this->listeners[$event][$k]);
                    return true;
                }
            }
        }
        return false;
    }
}
{'fraction_non_alphanumeric': 0.13414891373128032, 'fraction_numerical': 0.0, 'mean_word_length': 1.3290766208251474, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7104355', 'n_tokens_mistral': 1280, 'n_tokens_neox': 1232, 'n_words': 273}