<commit_before><?php
/**
 * Gets what we're going to do from the
 * http request.
 *
 * @package core
 * @subpackage core
 */
class router
{
	private $registry;
	private $path;
	private $args = array();

	public function set_path( $path )
	{
		$path .= DIRSEP;
		if( is_dir( $path ) == false )
		{
			throw new Exception( 'Invalid controller path: `' . $path . '`' );
		}
		$this->path = $path;
	}

	public function delegate( $route = 0 )
	{
		# Analyze route
		
		if( !$route )
		{
			$route = ( empty( $_GET[ 'route' ] ) ) ? '' : $_GET[ 'route' ];
		}
		
		$this->get_controller( $file, $controller, $action, $args, $route );
		
		foreach( $args as $k => $v )
		{
			$x = explode( ':', $v );
			if( strlen( $x[ 1 ] ) > 0 )
			{
				$args[ $x[ 0 ] ] = $x[ 1 ];
			}
			unset( $args[ $k ] );
			
		}
	
		$co = $controller;
		$cn = str_replace( "_", DIRSEP, $controller );	
		$file = str_replace( $co, $cn, $file ); 

		# File available?
		if( is_readable( $file ) == false )
		{
			die( "404" );
		}
		# Include the file
		include ( $file );
		# Initiate the class.
		$class = str_replace( "/", "_", 'controller_' . $controller);
		$controller = new $class( $this->registry );

		# If it isn't the action, set the action as index
		if( !is_callable( array( $controller, $action )))
		{
			$action = "index";
		}
		# Run action
		$controller->$action( $args );
	}

	private function get_controller( &$file, &$controller, &$action, &$args, $route = 0 )
	{
		if( empty( $route ) )
		{
			$route = 'index';
		}
		// Get separate parts
		$route = trim( $route, '/\\' );
		$parts = explode( '/', $route );
		// Find right controller
		$cmd_path = $this->path;
		
		foreach( $parts as $part )
		{
			$fullpath = $cmd_path . $part;
			// Is there a dir with this path?
			if( is_dir( $fullpath ) )
			{
				$cmd_path .= $part . DIRSEP;
				array_shift( $parts );
				$class_name[] = $part;
				continue;
			}
			// Find the file
			if( is_file( $fullpath . '.php' ) )
			{
				$controller = $part;
				array_shift( $parts );
				$class_name[] = $part;
				break;
			}
		}
		if( is_array( $class_name ))
		{
			$controller = implode( "_", $class_name );
		}
		if( empty( $controller ) )
		{
			$controller = 'index';
		}
		// Get action
		$action = array_shift( $parts );
		
		array_unshift( $parts, $action );
		
		if( empty( $action ) )
		{
			$action = 'index';
		}
		
		$file = $this->path . $controller . '.php';
		$args = $parts;
	}
}
?>
<commit_msg>added the ability to access route through _url args<commit_after><?php
/**
 * Gets what we're going to do from the
 * http request.
 *
 * @package core
 * @subpackage core
 */
class router
{
	private $registry;
	private $path;
	private $args = array();

	public function set_path( $path )
	{
		$path .= DIRSEP;
		if( is_dir( $path ) == false )
		{
			throw new Exception( 'Invalid controller path: `' . $path . '`' );
		}
		$this->path = $path;
	}

	public function delegate( $route = 0 )
	{
		# Analyze route
		
		if( !$route )
		{
			$route = ( empty( $_GET[ 'route' ] ) ) ? '' : $_GET[ 'route' ];
		}
		
		$this->get_controller( $file, $controller, $action, $args, $route );
		foreach( $args as $k => $v )
		{
			$x = explode( ':', $v );
			if( strlen( $x[ 1 ] ) > 0 )
			{
				$args[ $x[ 0 ] ] = $x[ 1 ];
			}
			unset( $args[ $k ] );
			
		}
	
		$co = $controller;
		$cn = str_replace( "_", DIRSEP, $controller );	
		$file = str_replace( $co, $cn, $file ); 

		# File available?
		if( is_readable( $file ) == false )
		{
			die( "404" );
		}
		# Include the file
		include ( $file );
		# Initiate the class.
		$class = str_replace( "/", "_", 'controller_' . $controller);
		$controller = new $class( $this->registry );

		# If it isn't the action, set the action as index
		if( !is_callable( array( $controller, $action )))
		{
			$action = "index";
		}
		# Run action
		$args[ "_url" ] = $route;
		$controller->$action( $args );
	}

	private function get_controller( &$file, &$controller, &$action, &$args, $route = 0 )
	{
		if( empty( $route ) )
		{
			$route = 'index';
		}
		// Get separate parts
		$route = trim( $route, '/\\' );
		$parts = explode( '/', $route );
		// Find right controller
		$cmd_path = $this->path;
		
		foreach( $parts as $part )
		{
			$fullpath = $cmd_path . $part;
			// Is there a dir with this path?
			if( is_dir( $fullpath ) )
			{
				$cmd_path .= $part . DIRSEP;
				array_shift( $parts );
				$class_name[] = $part;
				continue;
			}
			// Find the file
			if( is_file( $fullpath . '.php' ) )
			{
				$controller = $part;
				array_shift( $parts );
				$class_name[] = $part;
				break;
			}
		}
		if( is_array( $class_name ))
		{
			$controller = implode( "_", $class_name );
		}
		if( empty( $controller ) )
		{
			$controller = 'index';
		}
		// Get action
		$action = array_shift( $parts );
		
		array_unshift( $parts, $action );
		
		if( empty( $action ) )
		{
			$action = 'index';
		}
		
		$file = $this->path . $controller . '.php';
		$args = $parts;
	}
}
?>

{'fraction_non_alphanumeric': 0.18922735525519468, 'fraction_numerical': 0.0036312285656647167, 'mean_word_length': 2.5138199858256556, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, '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': '5745031', 'n_tokens_mistral': 2217, 'n_tokens_neox': 1870, 'n_words': 487}