View file application/engine.php

File size: 1.66Kb
<?PHP
	require('start.php');
	
	/*
		Роутинг
	*/
	class Routing {
		private
		$Packages = [],
		$Routes = [];
		
		public function __construct() {
			$this->ReadPackages()->Execute(
				$_SERVER['REQUEST_URI']
			);
		}
		
		public function ReadPackages() {
			$this->Packages = pkg()->Load([
				'admin',
				'pages',
				'account',
				'messages'
			]);
			
			foreach($this->Packages as $Key => $Value) {
				foreach($Value as $_KEY => $_VALUE) {
					$this->Routes['/^' . str_replace('/', '\/', $_VALUE['uri']) . '$/'] = [
						$_KEY => $Key
					];
				}
			}
			
			return $this;
		}
		
		public function Get($Page) {
			if(is_array($Page) || is_object($Page)) {
				foreach($Page as $_KEY => $_VALUE) {
					return $this->Packages[$_VALUE][$_KEY];
				}
			}
		}
		
		public function Execute($Uri) {
			foreach($this->Routes as $Pattern => $Page) {
				if(preg_match($Pattern, $Uri, $Params)) {
					global $_PAGE;
					
					$_PAGE = $this->Get(
						$Page
					);
					
					$_PAGE += [
						'params' => $Params,
						'url' => $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']
					];
					
					if(!file_exists('application/routing/' . $_PAGE['module'] . '.php')) {
						die('Не найден модуль: application/routing/' . $_PAGE['module'] . '.php');
					}
					
					array_shift($Params);
					return require('application/routing/' . $_PAGE['module'] . '.php');
				}
			}
			
			$this->pageNotFound();
		}
		
		/*
			Выводим, что страница не найдена
		*/
		public function pageNotFound() {
			pageLoad('system', 'errors/page_not_found', '');
		}
	}
	
	new Routing;