Esempio n. 1
0
 /**
  * Parses the query string looking for supplied request parameters.
  *
  * Places anything useful into this object's Controller properties.
  *
  * @param Gdn_Request $request The request to analyze.
  */
 private function analyzeRequest($request)
 {
     // Initialize the result of our request.
     $result = ['method' => $request->requestMethod(), 'path' => $request->path(), 'addon' => null, 'controller' => '', 'controllerMethod' => '', 'pathArgs' => [], 'query' => array_change_key_case($request->get()), 'post' => $request->post()];
     // Here is the basic format of a request:
     // [/application]/controller[/method[.json|.xml]]/argn|argn=valn
     // Here are some examples of what this method could/would receive:
     // /application/controller/method/argn
     // /controller/method/argn
     // /application/controller/argn
     // /controller/argn
     // /controller
     $parts = explode('/', str_replace('\\', '/', $request->path()));
     // Decode path parts at the dispatcher level.
     array_walk($parts, function (&$value) {
         $value = rawurldecode($value);
     });
     // Parse the file extension.
     list($parts, $deliveryMethod) = $this->parseDeliveryMethod($parts);
     // Set some special properties based on the deliver method.
     $deliveryType = $this->deliveryType;
     switch ($deliveryMethod) {
         case DELIVERY_METHOD_JSON:
         case DELIVERY_METHOD_XML:
             $deliveryType = DELIVERY_TYPE_DATA;
             break;
         case DELIVERY_METHOD_ATOM:
         case DELIVERY_METHOD_RSS:
             $result['syndicationMethod'] = DELIVERY_METHOD_RSS;
             //$deliveryMethod;
             break;
         case DELIVERY_METHOD_TEXT:
             $deliveryType = DELIVERY_TYPE_VIEW;
             break;
     }
     // An explicitly passed delivery type/method overrides the default.
     $result['deliveryMethod'] = self::requestVal('DeliveryMethod', $result['query'], $result['post'], $deliveryMethod ?: $this->deliveryMethod);
     $result['deliveryType'] = self::requestVal('DeliveryType', $result['query'], $result['post'], $deliveryType);
     // Figure out the controller.
     list($controllerName, $pathArgs) = $this->findController($parts);
     $result['pathArgs'] = $pathArgs;
     if ($controllerName) {
         // The controller was found based on the path.
         $result['controller'] = $controllerName;
     } elseif (Gdn::pluginManager()->hasNewMethod('RootController', val(0, $parts))) {
         // There is a plugin defining a new root method.
         $result['controller'] = 'RootController';
     } else {
         // No controller was found, fire a not found event.
         // TODO: Move this outside this method.
         $this->EventArguments['Handled'] = false;
         $Handled =& $this->EventArguments['Handled'];
         $this->fireEvent('NotFound');
         if (!$Handled) {
             safeHeader("HTTP/1.1 404 Not Found");
             return $this->passData('Reason', 'controller_notfound')->analyzeRequest($request->withRoute('Default404'));
         }
         return;
     }
     // A controller has been found. Find the addon that manages it.
     $addon = Gdn::addonManager()->lookupByClassname($result['controller']);
     // The result should be properly set now. Set the legacy properties though.
     if ($addon) {
         $result['addon'] = $addon;
         $this->applicationFolder = stringBeginsWith($addon->getSubdir(), 'applications/', true, true);
     }
     $this->ControllerName = $result['controller'];
     $this->controllerMethodArgs = [];
     $this->syndicationMethod = val('syndicationMethod', $result, SYNDICATION_NONE);
     $this->deliveryMethod = $result['deliveryMethod'];
     return $result;
 }