diff options
Diffstat (limited to 'phpBB/phpbb/event')
| -rw-r--r-- | phpBB/phpbb/event/data.php | 62 | ||||
| -rw-r--r-- | phpBB/phpbb/event/dispatcher.php | 36 | ||||
| -rw-r--r-- | phpBB/phpbb/event/extension_subscriber_loader.php | 35 | ||||
| -rw-r--r-- | phpBB/phpbb/event/kernel_exception_subscriber.php | 78 | ||||
| -rw-r--r-- | phpBB/phpbb/event/kernel_request_subscriber.php | 77 | ||||
| -rw-r--r-- | phpBB/phpbb/event/kernel_terminate_subscriber.php | 37 | 
6 files changed, 325 insertions, 0 deletions
diff --git a/phpBB/phpbb/event/data.php b/phpBB/phpbb/event/data.php new file mode 100644 index 0000000000..fbb16574ed --- /dev/null +++ b/phpBB/phpbb/event/data.php @@ -0,0 +1,62 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\Event; + +class data extends Event implements \ArrayAccess +{ +	private $data; + +	public function __construct(array $data = array()) +	{ +		$this->set_data($data); +	} + +	public function set_data(array $data = array()) +	{ +		$this->data = $data; +	} + +	public function get_data() +	{ +		return $this->data; +	} + +	/** +	 * Returns data filtered to only include specified keys. +	 * +	 * This effectively discards any keys added to data by hooks. +	 */ +	public function get_data_filtered($keys) +	{ +		return array_intersect_key($this->data, array_flip($keys)); +	} + +	public function offsetExists($offset) +	{ +		return isset($this->data[$offset]); +	} + +	public function offsetGet($offset) +	{ +		return isset($this->data[$offset]) ? $this->data[$offset] : null; +	} + +	public function offsetSet($offset, $value) +	{ +		$this->data[$offset] = $value; +	} + +	public function offsetUnset($offset) +	{ +		unset($this->data[$offset]); +	} +} diff --git a/phpBB/phpbb/event/dispatcher.php b/phpBB/phpbb/event/dispatcher.php new file mode 100644 index 0000000000..74b35eb78d --- /dev/null +++ b/phpBB/phpbb/event/dispatcher.php @@ -0,0 +1,36 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher; + +/** +* Extension of the Symfony2 EventDispatcher +* +* It provides an additional `trigger_event` method, which +* gives some syntactic sugar for dispatching events. Instead +* of creating the event object, the method will do that for +* you. +* +* Example: +* +*     $vars = array('page_title'); +*     extract($phpbb_dispatcher->trigger_event('core.index', compact($vars))); +* +*/ +class dispatcher extends ContainerAwareEventDispatcher +{ +	public function trigger_event($eventName, $data = array()) +	{ +		$event = new \phpbb\event\data($data); +		$this->dispatch($eventName, $event); +		return $event->get_data_filtered(array_keys($data)); +	} +} diff --git a/phpBB/phpbb/event/extension_subscriber_loader.php b/phpBB/phpbb/event/extension_subscriber_loader.php new file mode 100644 index 0000000000..6408f93e2a --- /dev/null +++ b/phpBB/phpbb/event/extension_subscriber_loader.php @@ -0,0 +1,35 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\EventDispatcherInterface; + +class extension_subscriber_loader +{ +	private $dispatcher; +	private $listener_collection; + +	public function __construct(EventDispatcherInterface $dispatcher, \phpbb\di\service_collection $listener_collection) +	{ +		$this->dispatcher = $dispatcher; +		$this->listener_collection = $listener_collection; +	} + +	public function load() +	{ +		if (!empty($this->listener_collection)) +		{ +			foreach ($this->listener_collection as $listener) +			{ +				$this->dispatcher->addSubscriber($listener); +			} +		} +	} +} diff --git a/phpBB/phpbb/event/kernel_exception_subscriber.php b/phpBB/phpbb/event/kernel_exception_subscriber.php new file mode 100644 index 0000000000..8a4de1fbad --- /dev/null +++ b/phpBB/phpbb/event/kernel_exception_subscriber.php @@ -0,0 +1,78 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; +use Symfony\Component\HttpKernel\Exception\HttpException; +use Symfony\Component\HttpFoundation\Response; + +class kernel_exception_subscriber implements EventSubscriberInterface +{ +	/** +	* Template object +	* @var \phpbb\template\template +	*/ +	protected $template; + +	/** +	* User object +	* @var \phpbb\user +	*/ +	protected $user; + +	/** +	* Construct method +	* +	* @param \phpbb\template\template $template Template object +	* @param \phpbb\user $user User object +	*/ +	public function __construct(\phpbb\template\template $template, \phpbb\user $user) +	{ +		$this->template = $template; +		$this->user = $user; +	} + +	/** +	* This listener is run when the KernelEvents::EXCEPTION event is triggered +	* +	* @param GetResponseForExceptionEvent $event +	* @return null +	*/ +	public function on_kernel_exception(GetResponseForExceptionEvent $event) +	{ +		page_header($this->user->lang('INFORMATION')); + +		$exception = $event->getException(); + +		$this->template->assign_vars(array( +			'MESSAGE_TITLE'		=> $this->user->lang('INFORMATION'), +			'MESSAGE_TEXT'		=> $exception->getMessage(), +		)); + +		$this->template->set_filenames(array( +			'body'	=> 'message_body.html', +		)); + +		page_footer(true, false, false); + +		$status_code = $exception instanceof HttpException ? $exception->getStatusCode() : 500; +		$response = new Response($this->template->assign_display('body'), $status_code); +		$event->setResponse($response); +	} + +	public static function getSubscribedEvents() +	{ +		return array( +			KernelEvents::EXCEPTION		=> 'on_kernel_exception', +		); +	} +} diff --git a/phpBB/phpbb/event/kernel_request_subscriber.php b/phpBB/phpbb/event/kernel_request_subscriber.php new file mode 100644 index 0000000000..7d5418498b --- /dev/null +++ b/phpBB/phpbb/event/kernel_request_subscriber.php @@ -0,0 +1,77 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Event\GetResponseEvent; +use Symfony\Component\HttpKernel\EventListener\RouterListener; +use Symfony\Component\Routing\RequestContext; + +class kernel_request_subscriber implements EventSubscriberInterface +{ +	/** +	* Extension finder object +	* @var \phpbb\extension\finder +	*/ +	protected $finder; + +	/** +	* PHP extension +	* @var string +	*/ +	protected $php_ext; + +	/** +	* Root path +	* @var string +	*/ +	protected $root_path; + +	/** +	* Construct method +	* +	* @param \phpbb\extension\finder $finder Extension finder object +	* @param string $root_path Root path +	* @param string $php_ext PHP extension +	*/ +	public function __construct(\phpbb\extension\finder $finder, $root_path, $php_ext) +	{ +		$this->finder = $finder; +		$this->root_path = $root_path; +		$this->php_ext = $php_ext; +	} + +	/** +	* This listener is run when the KernelEvents::REQUEST event is triggered +	* +	* This is responsible for setting up the routing information +	* +	* @param GetResponseEvent $event +	* @return null +	*/ +	public function on_kernel_request(GetResponseEvent $event) +	{ +		$request = $event->getRequest(); +		$context = new RequestContext(); +		$context->fromRequest($request); + +		$matcher = phpbb_get_url_matcher($this->finder, $context, $this->root_path, $this->php_ext); +		$router_listener = new RouterListener($matcher, $context); +		$router_listener->onKernelRequest($event); +	} + +	public static function getSubscribedEvents() +	{ +		return array( +			KernelEvents::REQUEST		=> 'on_kernel_request', +		); +	} +} diff --git a/phpBB/phpbb/event/kernel_terminate_subscriber.php b/phpBB/phpbb/event/kernel_terminate_subscriber.php new file mode 100644 index 0000000000..32dba322d1 --- /dev/null +++ b/phpBB/phpbb/event/kernel_terminate_subscriber.php @@ -0,0 +1,37 @@ +<?php +/** +* +* @package phpBB3 +* @copyright (c) 2012 phpBB Group +* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2 +* +*/ + +namespace phpbb\event; + +use Symfony\Component\EventDispatcher\EventSubscriberInterface; +use Symfony\Component\HttpKernel\KernelEvents; +use Symfony\Component\HttpKernel\Event\PostResponseEvent; + +class kernel_terminate_subscriber implements EventSubscriberInterface +{ +	/** +	* This listener is run when the KernelEvents::TERMINATE event is triggered +	* This comes after a Response has been sent to the server; this is +	* primarily cleanup stuff. +	* +	* @param PostResponseEvent $event +	* @return null +	*/ +	public function on_kernel_terminate(PostResponseEvent $event) +	{ +		exit_handler(); +	} + +	public static function getSubscribedEvents() +	{ +		return array( +			KernelEvents::TERMINATE		=> 'on_kernel_terminate', +		); +	} +}  | 
