Ejemplo n.º 1
0
 /**
  * Process the given URL, creating the appropriate controller and executing it.
  * 
  * This method will:
  *  - iterate over all of the rules given in {@link Director::addRules()}, and find the first one that matches.
  *  - instantiate the {@link Controller} object required by that rule, and call {@link Controller::setURLParams()} to give the URL paramters to the controller.
  *  - link the Controller's session to PHP's main session, using {@link Controller::setSession()}.
  *  - call {@link Controller::run()} on that controller
  *  - save the Controller's session back into PHP's main session.
  *  - output the response to the browser, using {@link HTTPResponse::output()}.
  * 
  * @param $url String, the URL the user is visiting, without the querystring.
  * @uses getControllerForURL() rule-lookup logic is handled by this.
  * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
  */
 function direct($url)
 {
     if (isset($_GET['debug_profile'])) {
         Profiler::mark("Director", "direct");
     }
     $controllerObj = Director::getControllerForURL($url);
     if (is_string($controllerObj) && substr($controllerObj, 0, 9) == 'redirect:') {
         $response = new HTTPResponse();
         $response->redirect(substr($controllerObj, 9));
         $response->output();
     } else {
         if ($controllerObj) {
             // Load the session into the controller
             $controllerObj->setSession(new Session($_SESSION));
             $response = $controllerObj->run(array_merge((array) $_GET, (array) $_POST, (array) $_FILES));
             $controllerObj->getSession()->inst_save();
             if (isset($_GET['debug_profile'])) {
                 Profiler::mark("Outputting to browser");
             }
             $response->output();
             if (isset($_GET['debug_profile'])) {
                 Profiler::unmark("Outputting to browser");
             }
         }
     }
     if (isset($_GET['debug_profile'])) {
         Profiler::unmark("Director", "direct");
     }
 }
Ejemplo n.º 2
0
 /**
  * Process the given URL, creating the appropriate controller and executing it.
  * 
  * Request processing is handled as folows:
  *  - Director::direct() creates a new HTTPResponse object and passes this to Director::handleRequest().
  *  - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this 
  *    request.
  *  - Controller::handleRequest($request) is then called.  This will find a rule to handle the URL, and call the rule
  *    handling method.
  *  - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a
  *    RequestHandler object.
  *
  * In addition to request processing, Director will manage the session, and perform the output of the actual response
  * to the browser.
  * 
  * @param $url String, the URL the user is visiting, without the querystring.
  * @uses handleRequest() rule-lookup logic is handled by this.
  * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
  */
 static function direct($url)
 {
     // Validate $_FILES array before merging it with $_POST
     foreach ($_FILES as $k => $v) {
         if (is_array($v['tmp_name'])) {
             foreach ($v['tmp_name'] as $tmpFile) {
                 if ($tmpFile && !is_uploaded_file($tmpFile)) {
                     user_error("File upload '{$k}' doesn't appear to be a valid upload", E_USER_ERROR);
                 }
             }
         } else {
             if ($v['tmp_name'] && !is_uploaded_file($v['tmp_name'])) {
                 user_error("File upload '{$k}' doesn't appear to be a valid upload", E_USER_ERROR);
             }
         }
     }
     $req = new HTTPRequest(isset($_SERVER['X-HTTP-Method-Override']) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'], $url, $_GET, array_merge((array) $_POST, (array) $_FILES), @file_get_contents('php://input'));
     // @todo find better way to extract HTTP headers
     if (isset($_SERVER['HTTP_ACCEPT'])) {
         $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']);
     }
     if (isset($_SERVER['CONTENT_TYPE'])) {
         $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']);
     }
     if (isset($_SERVER['HTTP_REFERER'])) {
         $req->addHeader("Referer", $_SERVER['HTTP_REFERER']);
     }
     // Load the session into the controller
     $session = new Session($_SESSION);
     $result = Director::handleRequest($req, $session);
     $session->inst_save();
     // Return code for a redirection request
     if (is_string($result) && substr($result, 0, 9) == 'redirect:') {
         $response = new HTTPResponse();
         $response->redirect(substr($result, 9));
         $response->output();
         // Handle a controller
     } else {
         if ($result) {
             if ($result instanceof HTTPResponse) {
                 $response = $result;
             } else {
                 $response = new HTTPResponse();
                 $response->setBody($result);
             }
             // ?debug_memory=1 will output the number of bytes of memory used for this request
             if (isset($_REQUEST['debug_memory']) && $_REQUEST['debug_memory']) {
                 echo number_format(memory_get_peak_usage(), 0);
             } else {
                 $response->output();
             }
             //$controllerObj->getSession()->inst_save();
         }
     }
 }
Ejemplo n.º 3
0
	/**
	 * Process the given URL, creating the appropriate controller and executing it.
	 * 
	 * Request processing is handled as folows:
	 *  - Director::direct() creates a new HTTPResponse object and passes this to Director::handleRequest().
	 *  - Director::handleRequest($request) checks each of the Director rules and identifies a controller to handle this 
	 *    request.
	 *  - Controller::handleRequest($request) is then called.  This will find a rule to handle the URL, and call the rule
	 *    handling method.
	 *  - RequestHandler::handleRequest($request) is recursively called whenever a rule handling method returns a
	 *    RequestHandler object.
	 *
	 * In addition to request processing, Director will manage the session, and perform the output of the actual response
	 * to the browser.
	 * 
	 * @param $url String, the URL the user is visiting, without the querystring.
	 * @uses handleRequest() rule-lookup logic is handled by this.
	 * @uses Controller::run() Controller::run() handles the page logic for a Director::direct() call.
	 */
	static function direct($url) {
		$req = new HTTPRequest(
			(isset($_SERVER['X-HTTP-Method-Override'])) ? $_SERVER['X-HTTP-Method-Override'] : $_SERVER['REQUEST_METHOD'],
			$url, 
			$_GET, 
			array_merge((array)$_POST, (array)$_FILES),
			@file_get_contents('php://input')
		);
		
		// @todo find better way to extract HTTP headers
		if(isset($_SERVER['HTTP_ACCEPT'])) $req->addHeader("Accept", $_SERVER['HTTP_ACCEPT']);
		if(isset($_SERVER['CONTENT_TYPE'])) $req->addHeader("Content-Type", $_SERVER['CONTENT_TYPE']);
		if(isset($_SERVER['HTTP_REFERER'])) $req->addHeader("Referer", $_SERVER['HTTP_REFERER']);

		// Load the session into the controller
		$session = new Session($_SESSION);
		$result = Director::handleRequest($req, $session);
		$session->inst_save();

		// Return code for a redirection request
		if(is_string($result) && substr($result,0,9) == 'redirect:') {
			$response = new HTTPResponse();
			$response->redirect(substr($result, 9));
			$response->output();

		// Handle a controller
		} else if($result) {
			if($result instanceof HTTPResponse) {
				$response = $result;
				
			} else {
				$response = new HTTPResponse();
				$response->setBody($result);
			}
			
			// ?debug_memory=1 will output the number of bytes of memory used for this request
			if(isset($_REQUEST['debug_memory']) && $_REQUEST['debug_memory']) {
				echo number_format(memory_get_peak_usage(),0);
			} else {
				$response->output();
			}

			//$controllerObj->getSession()->inst_save();
		}
	}