Beispiel #1
0
	/**
	 * Delegates the request to the proper controller.
	 */
	public function delegate() {
		try {
			// Setup
			$route = '';
			$parts = array();
			$args = array();

			// Find Route
			$route = (empty($_GET['route'])) ? '' : $_GET['route'];

			if(empty($route)) {
                $route = "index";
            }

			\Bedrock\Common\Logger::info('Route: ' . $route);

			// Separate the route into parts.
			$route = trim($route, "/\\");
			$parts = explode("/", $route);

			// Find Controller
			if($parts[0] == 'query' || substr($parts[0], 0, 6) == 'query:') {
            	$controller = new \Bedrock\Control\Query();
            	$controller->index($parts);
            }
			else {
				// Handle Stored Special Cases
            	$cases = \Bedrock\Common\Router\SpecialCases::retrieve();
				$specialCase = null;

            	foreach($cases as $case) {
            		if(substr($route, 0, strlen($case['route'])) == $case['route']) {
            			$controller = new $case['controller']();
            			$controller->{$case['method']}($parts);

            			$specialCase = true;
            			break;
            		}
            	}

				if(!$specialCase) {
					$parts = array_map('ucwords', $parts);
					$controller = null;

					while($parts) {
						// Check for ::index() method.
						array_push($parts, 'index');
						$controller = self::getController($this->_delegationQueue, $parts);
						if($controller) break;

						// Check for specific method.
						array_pop($parts);
						$controller = self::getController($this->_delegationQueue, $parts);
						if($controller) break;

						// Otherwise continue loop.
						array_unshift($args, strtolower(array_shift($parts)));
					}

					$action = strtolower(array_pop($parts));

					// Finally, delegate to a 404 error.
					if($controller === false) {
						\Bedrock\Common\Logger::error('No controller found using route: "' . $route . '"');
						$this->delegateToError();
					}
					elseif(!method_exists($controller, $action)) {
						\Bedrock\Common\Logger::error('No action found using route: "' . $route . '"');
						$this->delegateToError();
					}
					else {
						// Execute Controller Method
						$controller->$action($parts);
					}
				}
			}
		}
		catch(\Bedrock\Common\Router\Exception $ex) {
			\Bedrock\Common\Logger::exception($ex);
			$this->delegateToError();
		}
	}