<?php
/**
 * Bitrix Framework
 * @package bitrix
 * @subpackage main
 * @copyright 2001-2012 Bitrix
 */
namespace Bitrix\Main;

/**
 * Context of current request.
 */
final class Context
{
	/** @var Application */
	private $application;

	/** @var Response */
	private $response;

	/** @var Request */
	private $request;

	/** @var \Bitrix\Main\Localization\Culture */
	private $culture;

	/** @var \Bitrix\Main\Security\CurrentUser */
	private $user;

	/**
	 * Creates new instance of context.
	 *
	 * @param Application $application
	 */
	public function __construct(Application $application)
	{
		$this->application = $application;
	}

	/**
	 * Initializes context by request and response objects.
	 *
	 * @param Request $request
	 * @param Response $response
	 */
	public function initialize(Request $request, Response $response)
	{
		$this->request = $request;
		$this->response = $response;
		$this->culture = new Localization\Culture();
	}

	/**
	 * Sets current user for the context.
	 *
	 * @param Security\CurrentUser $user
	 */
	public function setUser(\Bitrix\Main\Security\CurrentUser $user)
	{
		$this->user = $user;
	}

	/**
	 * Returns current user.
	 *
	 * @return \Bitrix\Main\Security\CurrentUser
	 */
	public function getUser()
	{
		return $this->user;
	}

	/**
	 * Returns response object of the context.
	 *
	 * @return Response
	 */
	public function getResponse()
	{
		return $this->response;
	}

	/**
	 * Returns request object of the context.
	 *
	 * @return Request
	 */
	public function getRequest()
	{
		return $this->request;
	}

	/**
	 * Returns backreference to Application.
	 *
	 * @return Application
	 */
	public function getApplication()
	{
		return $this->application;
	}

	/**
	 * Returns backreference to Server.
	 *
	 * @return Server
	 */
	public function getServer()
	{
		return $this->application->GetServer();
	}

	/**
	 * Returns culture of the context.
	 *
	 * @return \Bitrix\Main\Localization\Culture
	 */
	public function getCulture()
	{
		return $this->culture;
	}

	/**
	 * Sets culture of the context.
	 *
	 * @param \Bitrix\Main\Localization\Culture $culture
	 */
	public function setCulture(Localization\Culture $culture)
	{
		$this->culture = $culture;
	}

	/**
	 * Static method returns current instance of context.
	 *
	 * @static
	 * @return Context
	 */
	public static function getCurrent()
	{
		$server = Server::getInstance();
		$app = $server->getApplication();
		return $app->getContext();
	}
}
