<commit_before><?php

namespace Parvula\Core;

/**
 * A quick, simple router
 *
 * @package Parvula
 * @since 0.3.0
 * @author <NAME>
 * @license MIT License
 */
class Router {

	/**
	 * @var \SplDoublyLinkedList
	 */
	private $routes;

	/**
	 * @var string
	 */
	private $uri;

	/**
	 * @var string
	 */
	private $method;

	/**
	 * Constructor
	 * @param string $uri (optional) If you want to override server URI
	 * @param string $method (optional) If you want to override server method
	 */
	public function __construct($uri = null, $method = null) {
		$this->routes = new \SplDoublyLinkedList;

		if($uri === null) {
			$uri = $_SERVER['SCRIPT_NAME'];
		}
		$this->uri = $uri;

		if($method === null) {
			$method = $_SERVER['REQUEST_METHOD'];
		}
		$this->method = $method;
		$this->prefix = '';
	}

	/**
	 * Add a new route with "GET" method
	 * @param string $path
	 * @param function $callback
	 */
	public function get($path, $callback) {
		$this->on('GET', $path, $callback);
	}

	/**
	 * Add a new route with "POST" method
	 * @param string $path
	 * @param function $callback
	 */
	public function post($path, $callback) {
		$this->on('POST', $path, $callback);
	}

	/**
	 * Add a new route with "PUT" method
	 * @param string $path
	 * @param function $callback
	 */
	public function put($path, $callback) {
		$this->on('PUT', $path, $callback);
	}

	/**
	 * Add a new route with "DELETE" method
	 * @param string $path
	 * @param function $callback
	 */
	public function delete($path, $callback) {
		$this->on('DELETE', $path, $callback);
	}


	/**
	 * Add a new route with all method
	 * @param string $path
	 * @param function $callback
	 */
	public function any($path, $callback) {
		$this->on('*', $path, $callback);
	}

	/**
	 * Add a new route
	 * @param string $method Method name (`*` for all methods)
	 * @param string $path
	 * @param function $callback
	 */
	public function on($method, $path, $callback) {
		$this->routes->push(array(
			"method" => $method,
			"path" => $this->prefix . $path,
			"callback" => $callback
		));
	}

	/**
	 * Spacename
	 * @param string $prefix
	 * @param function $callback
	 */
	public function space($prefix, $callback) {
		$that = clone $this;
		$that->prefix = $prefix;
		$callback($that);
	}

	/**
	 * Run the router
	 * @return mixed
	 */
	public function run() {
		$this->routes->rewind();
		return $this->dispatch();
	}

	/**
	 * Dispatch
	 * @return mixed
	 */
	private function dispatch() {
		// for each route
		for (; $this->routes->valid(); $this->routes->next()) {
			$route = $this->routes->current();

			// if method is OK
			if(strtoupper($route['method']) === $this->method || $route['method'] === '*') {

				$regex = $this->normalizeRegex($route['path']);

				// if path is OK
				if(preg_match("@^{$regex}@", $this->uri, $matches)) {
					$callback = $route['callback'];<commit_msg>return self instance for verbs methods<commit_after><?php

namespace Parvula\Core;

/**
 * A quick, simple router
 *
 * @package Parvula
 * @since 0.3.0
 * @author <NAME>
 * @license MIT License
 */
class Router {

	/**
	 * @var \SplDoublyLinkedList
	 */
	private $routes;

	/**
	 * @var string
	 */
	private $uri;

	/**
	 * @var string
	 */
	private $method;

	/**
	 * Constructor
	 * @param string $uri (optional) If you want to override server URI
	 * @param string $method (optional) If you want to override server method
	 */
	public function __construct($uri = null, $method = null) {
		$this->routes = new \SplDoublyLinkedList;

		if($uri === null) {
			$uri = $_SERVER['SCRIPT_NAME'];
		}
		$this->uri = $uri;

		if($method === null) {
			$method = $_SERVER['REQUEST_METHOD'];
		}
		$this->method = $method;
		$this->prefix = '';
	}

	/**
	 * Add a new route with "GET" method
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function get($path, $callback) {
		return $this->on('GET', $path, $callback);
	}

	/**
	 * Add a new route with "POST" method
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function post($path, $callback) {
		return $this->on('POST', $path, $callback);
	}

	/**
	 * Add a new route with "PUT" method
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function put($path, $callback) {
		return $this->on('PUT', $path, $callback);
	}

	/**
	 * Add a new route with "DELETE" method
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function delete($path, $callback) {
		return $this->on('DELETE', $path, $callback);
	}


	/**
	 * Add a new route with all method
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function any($path, $callback) {
		return $this->on('*', $path, $callback);
	}

	/**
	 * Add a new route
	 * @param string $method Method name (`*` for all methods)
	 * @param string $path
	 * @param function $callback
	 * @return Router Self
	 */
	public function on($method, $path, $callback) {
		$this->routes->push(array(
			"method" => $method,
			"path" => $this->prefix . $path,
			"callback" => $callback
		));

		return $this;
	}

	/**
	 * Spacename
	 * @param string $prefix
	 * @param function $callback
	 * @return Router Self
	 */
	public function space($prefix, $callback) {
		$that = clone $this;
		$that->prefix = $prefix;
		$callback($that);

		return $that;
	}

	/**
	 * Run the router
	 * @return mixed
	 */
	public function run() {
		$this->routes->rewind();
		return $this->dispatch();
	}

	/**
	 * Dispatch
	 * @return mixed
	 */
	private function dispatch() {
		// for each route
		for (; $this->routes->valid(); $this->routes->next()) {
			$route = $this->routes->current();

			// if method is OK
			if(strtoupper($route['method']) === $this->method || $route['method'] === '*') {

				$regex = $this->normalizeRegex($route['path']);

				// if path is OK
				if(preg_match("@^{$regex}@", $this->uri, $matches)) {
					$callback = $route['callback'];
{'fraction_non_alphanumeric': 0.18230383973288813, 'fraction_numerical': 0.001001669449081803, 'mean_word_length': 3.1460207612456745, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 61, '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': '7283835', 'n_tokens_mistral': 2206, 'n_tokens_neox': 1961, 'n_words': 566}