<commit_before><?php

namespace Primal\Routing;
use \Exception;

class Router {
	protected $routes_path;
	protected $filter_paired_arguments = false;
	protected $pair_all_arguments = false;

	protected $index_route = 'index';
	protected $catchall_route = '_catchall';
	protected $notfound_route = '404';

	protected $route;

	/**
	 * Static shortcut for initializing a new Router object for chaining.
	 *
	 * @param string $path optional Path to the routes folder
	 * @param string|true $url optional URL to immediately parse and execute. If passed `true`, will parse the page request URL instead.
	 * @return $this
	 */
	static function Create() {
		return new static();
	}


	/**
	 * Sets the search path for finding route files
	 *
	 * @param string $path
	 * @return $this
	 */
	function setRoutesPath($path) {
		$this->routes_path = realpath($path);
		return $this;
	}


	/**
	 * Filters url segments with value pairs from the indexed arguments array
	 *
	 * @param boolean $yes optional
	 * @return $this
	 */
	function enablePairedArgumentFiltering($yes = true) {
		$this->filter_paired_arguments = $yes;
		return $this;
	}


	/**
	 * Filters url segments without values out of the named arguments array
	 *
	 * @param boolean $yes optional
	 * @return $this
	 */
	function enableEmptySegmentInclusion($yes = true) {
		$this->pair_all_arguments = $yes;
		return $this;
	}


	/**
	 * Overwrites the default site index route ("index") with the supplied name.
	 *
	 * @param string $name
	 * @return $this
	 */
	function setSiteIndex($name) {
		$this->index_route = $name;
		return $this;
	}


	/**
	 * Overwrites the default site page not found route ("404") with the supplied name.
	 *
	 * @param string $index
	 * @return $this
	 */
	function setNotFound($path) {
		$this->notfound_route = $path;
		return $this;
	}


	/**
	 * Overwrites the default catchall route ("_catchall") with the supplied name.
	 *
	 * @param string $index
	 * @return $this
	 */
	function setCatchAll($path) {
		$this->catchall_route = $path;
		return $this;
	}

	/**
	 * Returns the last matched route
	 * @return Route
	 */
	public function getRoute() {
		return $this->route;
	}

/**

 */


	/**
	 * Parses the current page request URL and finds the relevant route file
	 *
	 * @return $this
	 */
	public function parseCurrentRequest($route_object = '/Primal/Routing/Route') {
		return $this->parseURL($_SERVER['REQUEST_URI'], $route_object);
	}


	/**
	 * Parses the passed url and finds the relevant route file.
	 *
	 * @param string $url
	 * @return $this
	 */
	public function parseURL($url, $route_object = '/Primal/Routing/Route') {
		if (!$this->_validateRoutesPath($this->routes_path)) {
			throw new Exception("Routes directory does not exist or is undefined.");
		}

		//grab only the path argument, ignoring the domain, query or fragments
		$original_url = parse_url($url, PHP_URL_PATH);

		//split the path by the slashes
		$segments = explode('/',$original_url);

		//and strip the first value (which is always empty)
		array_shift($segments);

		//process the segments for values
		list($indexed_segments, $named_segments, $map) = $this->_processSegments($segments, $this->pair_all_arguments);

		//if the arguments array is empty, then this is a request to the site index
		//replace the segments array with index route segment<commit_msg>Herp derp, namespaces are back slashes.<commit_after><?php

namespace Primal\Routing;
use \Exception;

class Router {
	protected $routes_path;
	protected $filter_paired_arguments = false;
	protected $pair_all_arguments = false;

	protected $index_route = 'index';
	protected $catchall_route = '_catchall';
	protected $notfound_route = '404';

	protected $route;

	/**
	 * Static shortcut for initializing a new Router object for chaining.
	 *
	 * @param string $path optional Path to the routes folder
	 * @param string|true $url optional URL to immediately parse and execute. If passed `true`, will parse the page request URL instead.
	 * @return $this
	 */
	static function Create() {
		return new static();
	}


	/**
	 * Sets the search path for finding route files
	 *
	 * @param string $path
	 * @return $this
	 */
	function setRoutesPath($path) {
		$this->routes_path = realpath($path);
		return $this;
	}


	/**
	 * Filters url segments with value pairs from the indexed arguments array
	 *
	 * @param boolean $yes optional
	 * @return $this
	 */
	function enablePairedArgumentFiltering($yes = true) {
		$this->filter_paired_arguments = $yes;
		return $this;
	}


	/**
	 * Filters url segments without values out of the named arguments array
	 *
	 * @param boolean $yes optional
	 * @return $this
	 */
	function enableEmptySegmentInclusion($yes = true) {
		$this->pair_all_arguments = $yes;
		return $this;
	}


	/**
	 * Overwrites the default site index route ("index") with the supplied name.
	 *
	 * @param string $name
	 * @return $this
	 */
	function setSiteIndex($name) {
		$this->index_route = $name;
		return $this;
	}


	/**
	 * Overwrites the default site page not found route ("404") with the supplied name.
	 *
	 * @param string $index
	 * @return $this
	 */
	function setNotFound($path) {
		$this->notfound_route = $path;
		return $this;
	}


	/**
	 * Overwrites the default catchall route ("_catchall") with the supplied name.
	 *
	 * @param string $index
	 * @return $this
	 */
	function setCatchAll($path) {
		$this->catchall_route = $path;
		return $this;
	}

	/**
	 * Returns the last matched route
	 * @return Route
	 */
	public function getRoute() {
		return $this->route;
	}

/**

 */


	/**
	 * Parses the current page request URL and finds the relevant route file
	 *
	 * @return $this
	 */
	public function parseCurrentRequest($route_object = '\Primal\Routing\Route') {
		return $this->parseURL($_SERVER['REQUEST_URI'], $route_object);
	}


	/**
	 * Parses the passed url and finds the relevant route file.
	 *
	 * @param string $url
	 * @return $this
	 */
	public function parseURL($url, $route_object = '\Primal\Routing\Route') {
		if (!$this->_validateRoutesPath($this->routes_path)) {
			throw new Exception("Routes directory does not exist or is undefined.");
		}

		//grab only the path argument, ignoring the domain, query or fragments
		$original_url = parse_url($url, PHP_URL_PATH);

		//split the path by the slashes
		$segments = explode('/',$original_url);

		//and strip the first value (which is always empty)
		array_shift($segments);

		//process the segments for values
		list($indexed_segments, $named_segments, $map) = $this->_processSegments($segments, $this->pair_all_arguments);

		//if the arguments array is empty, then this is a request to the site index
		//replace the segments array with index route segment
{'fraction_non_alphanumeric': 0.11869967193558008, 'fraction_numerical': 0.001789442290486132, 'mean_word_length': 3.647955647955648, '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': '790832', 'n_tokens_mistral': 2193, 'n_tokens_neox': 1984, 'n_words': 678}