Source for file rest.php

Documentation is available at rest.php

  1. <?php
  2. /**
  3.  * @package     Joomla.Platform
  4.  * @subpackage  Application
  5.  *
  6.  * @copyright   Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
  7.  * @license     GNU General Public License version 2 or later; see LICENSE
  8.  */
  9.  
  10. defined('JPATH_PLATFORM'or die;
  11.  
  12. /**
  13.  * RESTful Web application router class for the Joomla Platform.
  14.  *
  15.  * @package     Joomla.Platform
  16.  * @subpackage  Application
  17.  * @since       12.2
  18.  */
  19. {
  20.     /**
  21.      * @var     boolean  A boolean allowing to pass _method as parameter in POST requests
  22.      *
  23.      * @since  12.2
  24.      */
  25.     protected $methodInPostRequest = false;
  26.  
  27.     /**
  28.      * @var    array  An array of HTTP Method => controller suffix pairs for routing the request.
  29.      * @since  12.2
  30.      */
  31.     protected $suffixMap = array(
  32.         'GET' => 'Get',
  33.         'POST' => 'Create',
  34.         'PUT' => 'Update',
  35.         'PATCH' => 'Update',
  36.         'DELETE' => 'Delete',
  37.         'HEAD' => 'Head',
  38.         'OPTIONS' => 'Options'
  39.     );
  40.  
  41.     /**
  42.      * Find and execute the appropriate controller based on a given route.
  43.      *
  44.      * @param   string  $route  The route string for which to find and execute a controller.
  45.      *
  46.      * @return  void 
  47.      *
  48.      * @since   12.2
  49.      * @throws  InvalidArgumentException
  50.      * @throws  RuntimeException
  51.      */
  52.     public function execute($route)
  53.     {
  54.         // Get the controller name based on the route patterns and requested route.
  55.         $name $this->parseRoute($route);
  56.  
  57.         // Append the HTTP method based suffix.
  58.         $name .= $this->fetchControllerSuffix();
  59.  
  60.         // Get the controller object by name.
  61.         $controller $this->fetchController($name);
  62.  
  63.         // Execute the controller.
  64.         $controller->execute();
  65.     }
  66.  
  67.     /**
  68.      * Set a controller class suffix for a given HTTP method.
  69.      *
  70.      * @param   string  $method  The HTTP method for which to set the class suffix.
  71.      * @param   string  $suffix  The class suffix to use when fetching the controller name for a given request.
  72.      *
  73.      * @return  JApplicationWebRouter  This object for method chaining.
  74.      *
  75.      * @since   12.2
  76.      */
  77.     public function setHttpMethodSuffix($method$suffix)
  78.     {
  79.         $this->suffixMap[strtoupper((string) $method)= (string) $suffix;
  80.  
  81.         return $this;
  82.     }
  83.  
  84.     /**
  85.      * Set to allow or not method in POST request
  86.      *
  87.      * @param   boolean  $value  A boolean to allow or not method in POST request
  88.      *
  89.      * @return  void 
  90.      *
  91.      * @since   12.2
  92.      */
  93.     public function setMethodInPostRequest($value)
  94.     {
  95.         $this->methodInPostRequest = $value;
  96.     }
  97.  
  98.     /**
  99.      * Get the property to allow or not method in POST request
  100.      *
  101.      * @return  boolean 
  102.      *
  103.      * @since   12.2
  104.      */
  105.     public function isMethodInPostRequest()
  106.     {
  107.         return $this->methodInPostRequest;
  108.     }
  109.  
  110.     /**
  111.      * Get the controller class suffix string.
  112.      *
  113.      * @return  string 
  114.      *
  115.      * @since   12.2
  116.      * @throws  RuntimeException
  117.      */
  118.     protected function fetchControllerSuffix()
  119.     {
  120.         // Validate that we have a map to handle the given HTTP method.
  121.         if (!isset($this->suffixMap[$this->input->getMethod()]))
  122.         {
  123.             throw new RuntimeException(sprintf('Unable to support the HTTP method `%s`.'$this->input->getMethod())404);
  124.         }
  125.  
  126.         // Check if request method is POST
  127.         if $this->methodInPostRequest == true && strcmp(strtoupper($this->input->server->getMethod())'POST'=== 0)
  128.         {
  129.             // Get the method from input
  130.             $postMethod $this->input->get->getWord('_method');
  131.  
  132.             // Validate that we have a map to handle the given HTTP method from input
  133.             if ($postMethod && isset($this->suffixMap[strtoupper($postMethod)]))
  134.             {
  135.                 return ucfirst($this->suffixMap[strtoupper($postMethod)]);
  136.             }
  137.         }
  138.  
  139.         return ucfirst($this->suffixMap[$this->input->getMethod()]);
  140.     }
  141. }

Documentation generated on Tue, 19 Nov 2013 15:11:54 +0100 by phpDocumentor 1.4.3