/**
  * Test if an application can be enabled.
  *
  * @param string $applicationName The name of the application to test.
  * @return bool Returns true if the application can be enabled or false otherwise.
  * @throws Exception Throws an exception if the application is not in the correct format.
  */
 public function testApplication($applicationName)
 {
     // Add the application to the $EnabledApplications array in conf/applications.php
     $ApplicationInfo = arrayValueI($applicationName, $this->availableApplications(), array());
     $applicationName = $ApplicationInfo['Index'];
     $ApplicationFolder = val('Folder', $ApplicationInfo, '');
     if ($ApplicationFolder == '') {
         throw new Exception(t('The application folder was not properly defined.'));
     }
     // Hook directly into the autoloader and force it to load the newly tested application
     Gdn_Autoloader::attachApplication($ApplicationFolder);
     // Call the application's setup method
     $hooks = $applicationName . 'Hooks';
     if (!class_exists($hooks)) {
         $hooksPath = PATH_APPLICATIONS . DS . $ApplicationFolder . '/settings/class.hooks.php';
         if (file_exists($hooksPath)) {
             include_once $hooksPath;
         }
     }
     if (class_exists($hooks)) {
         /* @var Gdn_IPlugin $hooks The hooks object should be a plugin. */
         $hooks = new $hooks();
         if (method_exists($hooks, 'setup')) {
             $hooks->setup();
         }
     }
     return true;
 }
 /**
  * Map the API request to the corrosponding controller
  *
  * @since  0.1.0
  * @access public
  * @throws Exception
  * @return void
  * @static
  */
 public static function dispatchRequest()
 {
     $request = Gdn::request();
     $requestUri = static::getRequestUri();
     $requestMethod = static::getRequestMethod();
     if (!in_array($requestMethod, static::$supportedMethods)) {
         throw new Exception(t("API.Error.MethodNotAllowed"), 405);
     }
     if (!Gdn::session()->isValid()) {
         $username = getIncomingValue("username");
         $email = getIncomingValue("email");
         if ($username || $email) {
             APIAuth::authenticateRequest();
         }
     }
     $resource = val(1, $requestUri);
     $apiClass = ucfirst($resource) . "API";
     if (!class_exists($apiClass)) {
         throw new Exception(sprintf(t("API.Error.Class.Invalid"), $apiClass), 404);
     }
     if (!is_subclass_of($apiClass, "APIMapper")) {
         throw new Exception(t("API.Error.Mapper"), 500);
     }
     $apiClass = new $apiClass();
     $isWriteMethod = in_array($requestMethod, ["post", "put", "delete"]);
     $requestArguments = $isWriteMethod ? static::getRequestArguments() : [];
     $dispatch = static::map($resource, $apiClass, $requestUri, $requestMethod, $requestArguments);
     $controller = $dispatch["controller"];
     if (!$controller) {
         throw new Exception(t("API.Error.Controller.Missing"), 500);
     }
     $inputData = array_merge($requestArguments, $dispatch["arguments"]);
     if ($isWriteMethod) {
         // Set the transient key since we no longer have a front-end that
         // takes care of doing it for us
         $inputData["TransientKey"] = Gdn::session()->transientKey();
         // Authentication is always required for write-methods
         $dispatch["authenticate"] = true;
         // As Garden doesn"t take PUT and DELETE requests into account when
         // verifying requests using IsPostBack() and IsAuthencatedPostBack(),
         // we need to mask PUTs and DELETEs as POSTs.
         $request->requestMethod("post");
         // Add any API-specific arguments to the requests arguments
         $request->setRequestArguments(Gdn_Request::INPUT_POST, $inputData);
         // Set the PHP $_POST global as the result of any form data picked
         // up by Garden.
         $_POST = $request->post();
     }
     if ($dispatch["authenticate"] && !Gdn::session()->isValid()) {
         throw new Exception(t("API.Error.AuthRequired"), 401);
     }
     $application = $dispatch["application"];
     if ($application) {
         Gdn_Autoloader::attachApplication($application);
     }
     $method = $dispatch["method"];
     $arguments = $dispatch["arguments"];
     Gdn::request()->withControllerMethod($controller, $method, $arguments);
 }