<commit_before><?php

namespace Alloy;

/**
 * Router
 *
 * Maps URL to named parameters for use in application
 *
 * @package Alloy
 * @link    http://alloyframework.com/
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 */
class Router
{
	/**
	 * @var array  Stored routes
	 */
	private $routes = array();

	/**
	 * @var \Alloy\Route  The matched route.
	 */
	private $matchedRoute;

	/**
	 * Connect route
	 *
	 * @param  string $name      Name of the route
	 * @param  string $route     Route to match
	 * @param  array  $defaults  Array of key => value parameters to supply as defaults
	 * @return \Alloy\Route      The newly created route
	 */
	public function route($name, $route, array $defaults = null)
	{
		$route = new Route($name, $route, $defaults);
		$this->routes[$name] = $route;
		return $route;
	}

	/**
	 * Get set routes
	 *
	 * @return array
	 */
	public function routes()
	{
		return $this->routes;
	}

	/**
	 * Match given URL string
	 *
	 * @throws \OutOfBoundsException  Exception thrown when no routes are defined.
	 *
	 * @param  string $method  HTTP Method to match for
	 * @param  string $url     Request URL to match for
	 * @return array  $params  Parameters with values that were matched
	 */
	public function match($method, $url)
	{
		if (empty($this->routes))
		{
			throw new \OutOfBoundsException("There must be at least one route defined to match for.");
		}

		$params = array();
		foreach ($this->routes as $name => $route)
		{
			$matches = $this->getMatches($route, $url);
			if ( ! empty($matches))
			{
				$params = $this->getRouteParams($route, $matches, $method);

				$callback = $route->condition();
				if ($callback !== null)
				{
					$passed = call_user_func($callback, $params, $method, $url);

					if ($passed === false)
					{
						$params = array();
						continue;
					}
				}

				$this->matchedRoute = $route;
				break;
			}
		}


		if ( ! empty($params) AND $route->afterMatch() !== null)
		{
			$params = call_user_func($route->afterMatch(), $params, $method, $url);
		}

		return $params;
	}

	/**
	 * Does the route match the current url?
	 *
	 * @param  Route  $route    The route to check
	 * @param  string $url      The url to test against
	 * @return mixed            Either an array of matches or boolean
	 */
	private function getMatches(Route $route, $url)
	{
		$matches = array();
		preg_match($route->regexp(), $url, $matches);
		return $matches;
	}

	/**
	 * Gets the parameters from the matches route.
	 *
	 * @param  Route  $route    The matched route
	 * @param  array  $matches  The matched param list
	 * @param  string $method   HTTP request method
	 * @return array
	 */
	private function getRouteParams(Route $route, array $matches, $method)
	{
		$params = array();

		foreach ($matches as $key => $value)
		{
			if (is_string($key))
			{
				$params[$key] = $value;
			}
		}

		return array_merge($route->defaults(), $route->methodDefaults($method), $params);
	}

	/**
	 * Return the last matched route
	 *
	 * @throws \LogicException  If no route is matched yet.
	 *
	 * @return \Alloy\Route
	 */
	public function getMatchedRoute()
	{
		if ($this->matchedRoute)
		{<commit_msg>Moving the fetching of route params from the router to the route.<commit_after><?php

namespace Alloy;

/**
 * Router
 *
 * Maps URL to named parameters for use in application
 *
 * @package Alloy
 * @link    http://alloyframework.com/
 * @license http://www.opensource.org/licenses/bsd-license.php BSD
 */
class Router
{
	/**
	 * @var array  Stored routes
	 */
	private $routes = array();

	/**
	 * @var \Alloy\Route  The matched route.
	 */
	private $matchedRoute;

	/**
	 * Connect route
	 *
	 * @param  string $name      Name of the route
	 * @param  string $route     Route to match
	 * @param  array  $defaults  Array of key => value parameters to supply as defaults
	 * @return \Alloy\Route      The newly created route
	 */
	public function route($name, $route, array $defaults = null)
	{
		$route = new Route($name, $route, $defaults);
		$this->routes[$name] = $route;
		return $route;
	}

	/**
	 * Get set routes
	 *
	 * @return array
	 */
	public function routes()
	{
		return $this->routes;
	}

	/**
	 * Match given URL string
	 *
	 * @throws \OutOfBoundsException  Exception thrown when no routes are defined.
	 *
	 * @param  string $method  HTTP Method to match for
	 * @param  string $url     Request URL to match for
	 * @return array  $params  Parameters with values that were matched
	 */
	public function match($method, $url)
	{
		if (empty($this->routes))
		{
			throw new \OutOfBoundsException("There must be at least one route defined to match for.");
		}

		$params = array();
		foreach ($this->routes as $name => $route)
		{
			$matches = array();
			preg_match($route->regexp(), $url, $matches);

			if ( ! empty($matches))
			{
				$matches = $this->normalizeMatches($matches);
				$params = $route->getParams($matches, $method);

				$callback = $route->condition();
				if ($callback !== null)
				{
					$passed = call_user_func($callback, $params, $method, $url);

					if ($passed === false)
					{
						$params = array();
						continue;
					}
				}

				$this->matchedRoute = $route;
				break;
			}
		}


		if ( ! empty($params) AND $route->afterMatch() !== null)
		{
			$params = call_user_func($route->afterMatch(), $params, $method, $url);
		}

		return $params;
	}

	/**
	 * Normalizes the matches array returned from a preg_match call.
	 *
	 * This will pull out just the named sections and remove the number indexed matches.
	 *
	 * @param  array  $matches  The matched param list
	 * @return array
	 */
	private function normalizeMatches(array $matches)
	{
		$params = array();

		foreach ($matches as $key => $value)
		{
			if (is_string($key))
			{
				$params[$key] = $value;
			}
		}

		return $params;
	}

	/**
	 * Return the last matched route
	 *
	 * @throws \LogicException  If no route is matched yet.
	 *
	 * @return \Alloy\Route
	 */
	public function getMatchedRoute()
	{
		if ($this->matchedRoute)
		{
{'fraction_non_alphanumeric': 0.1390728476821192, 'fraction_numerical': 0.0, 'mean_word_length': 2.845321451304901, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 35, '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': '4519143', 'n_tokens_mistral': 2178, 'n_tokens_neox': 1881, 'n_words': 594}