<commit_before><?php
class ASrouterController {

	private $sid;

	function __construct( ) {

	}

	public function getPage() {

		$route = $this->route();

		$controller	= $route['controller'];
		$router		= $route['router'];

		if($controller!==false) {
			$rendered = $this->getContent( $controller, $router );
		}

		return $rendered;

	}

	private function route( ) {

		if($_SESSION['AS']['var']['router']['controller']) {
			include_once(AS_VAR_PATH.'router/controller.router.php');
			$rt = new routerController();
			$controllerInfo = $rt->getControllerName();
		}
		else {
			$controllerInfo = array( 'sid'=>'error', 'params'=>array('error'=>'404 Not Found'));
		}

		$this->sid = $controllerInfo['sid'];

		$controller = $this->loadController( $controllerInfo['sid'], $controllerInfo['params'] );
		return array('controller'=>$controller, 'router'=>$rt);
	}

	private function loadController( $controller_sid, $controller_params ) {
		if($controller_sid!='') {

			if(file_exists(AS_APP_PATH.$controller_sid.'/model.'.$controller_sid.'.php')) {
				include_once($controller_sid.'/model.'.$controller_sid.'.php');
			}

			if(file_exists(AS_APP_PATH.$controller_sid.'/controller.'.$controller_sid.'.php')) {
				include_once($controller_sid.'/controller.'.$controller_sid.'.php');
			}

			$class = $controller_sid.'Controller';

			if(class_exists($class)) {
				$controller = new $class( $controller_params, $_GET, $_POST );
			}
			else {
				error_log('Controller not found. Check controller name. ');
			}
		}
		else {
			error_log('Controller name not set.');
		}

		if(!isset($controller)) {
			http_response_code(404);
			$this->sid = 'error';
			include_once(AS__PATH.'etc/error/controller.error.php');
			$controller = new errorController( $controller_params );
		}

		return $controller;
	}

	private function getContent( $controller, $router ) {

		//BODY CONTENT (with caching)
			$allowCaching = true;
			$body = false;

			if(method_exists($controller, 'allowCaching')){
				$allowCaching = $controller->allowCaching();
			}

			//if caching is allowed, we try to get the output from the cache and only if it doesn't exist do we run the resource expensive controller view()
			if($allowCaching) {
				$cacheKey = str_replace('?', '_',cacheSys::sanitizeKey($_SERVER['REQUEST_URI']));
				$body	  = cacheSys::get( 'output', $cacheKey );
			}

			//call local variables whether cache is real or not.
			if(method_exists($router, 'getAdditionalVars')) {
				$localVariables = $router->getAdditionalVars();
			}<commit_msg>Updated error routing to not overwrite http response code if already set<commit_after><?php
class ASrouterController {

	private $sid;

	function __construct( ) {

	}

	public function getPage() {

		$route = $this->route();

		$controller	= $route['controller'];
		$router		= $route['router'];

		if($controller!==false) {
			$rendered = $this->getContent( $controller, $router );
		}

		return $rendered;

	}

	private function route( ) {

		if($_SESSION['AS']['var']['router']['controller']) {
			include_once(AS_VAR_PATH.'router/controller.router.php');
			$rt = new routerController();
			$controllerInfo = $rt->getControllerName();
		}
		else {
			$controllerInfo = array( 'sid'=>'error', 'params'=>array('error'=>'404 Not Found'));
		}

		$this->sid = $controllerInfo['sid'];

		$controller = $this->loadController( $controllerInfo['sid'], $controllerInfo['params'] );
		return array('controller'=>$controller, 'router'=>$rt);
	}

	private function loadController( $controller_sid, $controller_params ) {
		if($controller_sid!='' && $controller_sid!='error') {

			if(file_exists(AS_APP_PATH.$controller_sid.'/model.'.$controller_sid.'.php')) {
				include_once($controller_sid.'/model.'.$controller_sid.'.php');
			}

			if(file_exists(AS_APP_PATH.$controller_sid.'/controller.'.$controller_sid.'.php')) {
				include_once($controller_sid.'/controller.'.$controller_sid.'.php');
			}

			$class = $controller_sid.'Controller';

			if(class_exists($class)) {
				$controller = new $class( $controller_params, $_GET, $_POST );
			}
			else {
				error_log('Controller not found. Check controller name. ');
			}
		}
		else {
			error_log('Controller name not set.');
		}

		if(!isset($controller)) {
			if(!headers_sent()) {
				if(http_response_code()==200 || http_response_code()==null) {
					http_response_code(404);
				}
			}
			$this->sid = 'error';
			include_once(AS__PATH.'etc/error/controller.error.php');
			$controller = new errorController( $controller_params );
		}

		return $controller;
	}

	private function getContent( $controller, $router ) {

		//BODY CONTENT (with caching)
			$allowCaching = true;
			$body = false;

			if(method_exists($controller, 'allowCaching')){
				$allowCaching = $controller->allowCaching();
			}

			//if caching is allowed, we try to get the output from the cache and only if it doesn't exist do we run the resource expensive controller view()
			if($allowCaching) {
				$cacheKey = str_replace('?', '_',cacheSys::sanitizeKey($_SERVER['REQUEST_URI']));
				$body	  = cacheSys::get( 'output', $cacheKey );
			}

			//call local variables whether cache is real or not.
			if(method_exists($router, 'getAdditionalVars')) {
				$localVariables = $router->getAdditionalVars();
			}
{'fraction_non_alphanumeric': 0.17112502371466515, 'fraction_numerical': 0.0028457598178713715, 'mean_word_length': 4.368635437881874, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 29, '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': '652017', 'n_tokens_mistral': 1917, 'n_tokens_neox': 1631, 'n_words': 327}