<commit_before><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace WildPHP\EventManager;

use \WildPHP\EventManager\ListenerPriority as Priority,
	WildPHP\Event\IEvent,
	\RuntimeException,
	\InvalidArgumentException;


/**
 * Represents a registered event within the event manager.
 */
class RegisteredEvent
{

	/**
	 * Class name of the event.
	 * The event is guaranteed to be using this class name or its descendant when it is triggered.
	 */
	protected $className;

	/**
	 * An array of all listeners that are attached to this event.
	 */
	protected $listeners = array();

	/**
	 * Boolean telling if the listener array needs sorting.
	 */
	protected $isSorted = true;

	/**
	 * Creates a registered event with the specified class name.
	 * @param string $className the event's class name.
	 */
	public function __construct($className)
	{
		$this->className = (string) $className;
	}

	/**
	 * Registers a listener for this event at given priority.
	 * The listener function must be able to accept the event as its first parameter.
	 * Throws an exception when the listener is attempted to be registered for a second time.
	 * @param callable $listener The listener (as a callable function or method) that will be called when the event is triggered
	 * @param Priority $priority The priority this event will be ran with. Defaults to normal.
	 * @throws ListenerAlreadyRegisteredException
	 */
	public function registerListener(callable $listener, Priority $priority = null)
	{
		if($priority === null)
			$priority = new Priority(self::NORMAL);

		if($this->isListenerRegistered($listener))
			throw new ListenerAlreadyRegisteredException('Attempt to register event listener failed: listener already attached.');

		$this->isSorted = false;
		$this->listeners[$priority->getValue()][]= $listener;
	}

	/**
	 * Checks whether a listener is already attached to this event.<commit_msg>Adds event handler registration. Implemented in terms of listener with a priority that makes it run last.<commit_after><?php

/*
	WildPHP - a modular and easily extendable IRC bot written in PHP
	Copyright (C) 2015 WildPHP

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 3 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
namespace WildPHP\EventManager;

use \WildPHP\EventManager\ListenerPriority as Priority,
	WildPHP\Event\IEvent,
	\RuntimeException,
	\InvalidArgumentException;


/**
 * Represents a registered event within the event manager.
 */
class RegisteredEvent
{

	/**
	 * Class name of the event.
	 * The event is guaranteed to be using this class name or its descendant when it is triggered.
	 */
	protected $className;

	/**
	 * An array of all listeners that are attached to this event.
	 */
	protected $listeners = array();

	/**
	 * Boolean telling if the listener array needs sorting.
	 */
	protected $isSorted = true;

	/**
	 * Creates a registered event with the specified class name.
	 * @param string $className the event's class name.
	 */
	public function __construct($className)
	{
		$this->className = (string) $className;
	}

	/**
	 * Registers a listener for this event at given priority.
	 * The listener function must be able to accept the event as its first parameter.
	 * Throws an exception when the listener is attempted to be registered for a second time.
	 * @param callable $listener The listener (as a callable function or method) that will be called when the event is triggered
	 * @param Priority $priority The priority this event will be ran with. Defaults to normal.
	 * @throws ListenerAlreadyRegisteredException
	 */
	public function registerListener(callable $listener, Priority $priority = null)
	{
		if($priority === null)
			$priority = new Priority(self::NORMAL);

		if($this->isListenerRegistered($listener))
			throw new ListenerAlreadyRegisteredException('Attempt to register event listener failed: listener already attached.');

		$this->isSorted = false;
		$this->listeners[$priority->getValue()][]= $listener;
	}

	/**
	 * Registers the final event handler.
	 * This is basically just a listener that is guaranteed to run last.
	 * @see registerListener($listener, Priority::HANDLER())
	 */
	public function registerEventHandler(callable $listener)
	{
		return $this->registerListener($listener, new Priority(self::HANDLER));
	}

	/**
	 * Checks whether a listener is already attached to this event.
{'fraction_non_alphanumeric': 0.078113750899928, 'fraction_numerical': 0.0017998560115190785, 'mean_word_length': 4.042649727767695, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '664703', 'n_tokens_mistral': 1519, 'n_tokens_neox': 1395, 'n_words': 677}