Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
rest.php
Aller à la documentation de ce fichier.
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  */
20 {
21  /**
22  * @var boolean A boolean allowing to pass _method as parameter in POST requests
23  *
24  * @since 12.2
25  */
26  protected $methodInPostRequest = false;
27 
28  /**
29  * @var array An array of HTTP Method => controller suffix pairs for routing the request.
30  * @since 12.2
31  */
32  protected $suffixMap = array(
33  'GET' => 'Get',
34  'POST' => 'Create',
35  'PUT' => 'Update',
36  'PATCH' => 'Update',
37  'DELETE' => 'Delete',
38  'HEAD' => 'Head',
39  'OPTIONS' => 'Options'
40  );
41 
42  /**
43  * Find and execute the appropriate controller based on a given route.
44  *
45  * @param string $route The route string for which to find and execute a controller.
46  *
47  * @return void
48  *
49  * @since 12.2
50  * @throws InvalidArgumentException
51  * @throws RuntimeException
52  */
53  public function execute($route)
54  {
55  // Get the controller name based on the route patterns and requested route.
56  $name = $this->parseRoute($route);
57 
58  // Append the HTTP method based suffix.
59  $name .= $this->fetchControllerSuffix();
60 
61  // Get the controller object by name.
62  $controller = $this->fetchController($name);
63 
64  // Execute the controller.
65  $controller->execute();
66  }
67 
68  /**
69  * Set a controller class suffix for a given HTTP method.
70  *
71  * @param string $method The HTTP method for which to set the class suffix.
72  * @param string $suffix The class suffix to use when fetching the controller name for a given request.
73  *
74  * @return JApplicationWebRouter This object for method chaining.
75  *
76  * @since 12.2
77  */
78  public function setHttpMethodSuffix($method, $suffix)
79  {
80  $this->suffixMap[strtoupper((string) $method)] = (string) $suffix;
81 
82  return $this;
83  }
84 
85  /**
86  * Set to allow or not method in POST request
87  *
88  * @param boolean $value A boolean to allow or not method in POST request
89  *
90  * @return void
91  *
92  * @since 12.2
93  */
94  public function setMethodInPostRequest($value)
95  {
96  $this->methodInPostRequest = $value;
97  }
98 
99  /**
100  * Get the property to allow or not method in POST request
101  *
102  * @return boolean
103  *
104  * @since 12.2
105  */
106  public function isMethodInPostRequest()
107  {
108  return $this->methodInPostRequest;
109  }
110 
111  /**
112  * Get the controller class suffix string.
113  *
114  * @return string
115  *
116  * @since 12.2
117  * @throws RuntimeException
118  */
119  protected function fetchControllerSuffix()
120  {
121  // Validate that we have a map to handle the given HTTP method.
122  if (!isset($this->suffixMap[$this->input->getMethod()]))
123  {
124  throw new RuntimeException(sprintf('Unable to support the HTTP method `%s`.', $this->input->getMethod()), 404);
125  }
126 
127  // Check if request method is POST
128  if ( $this->methodInPostRequest == true && strcmp(strtoupper($this->input->server->getMethod()), 'POST') === 0)
129  {
130  // Get the method from input
131  $postMethod = $this->input->get->getWord('_method');
132 
133  // Validate that we have a map to handle the given HTTP method from input
134  if ($postMethod && isset($this->suffixMap[strtoupper($postMethod)]))
135  {
136  return ucfirst($this->suffixMap[strtoupper($postMethod)]);
137  }
138  }
139 
140  return ucfirst($this->suffixMap[$this->input->getMethod()]);
141  }
142 }