PageRenderTime 68ms CodeModel.GetById 35ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/agavi/request/AgaviRequest.class.php

https://github.com/digitarald/pullhub
PHP | 296 lines | 116 code | 24 blank | 156 comment | 17 complexity | 219a9b465d6c87c8a259adfb688acf66 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. // +---------------------------------------------------------------------------+
  3. // | This file is part of the Agavi package. |
  4. // | Copyright (c) 2005-2009 the Agavi Project. |
  5. // | |
  6. // | For the full copyright and license information, please view the LICENSE |
  7. // | file that was distributed with this source code. You can also view the |
  8. // | LICENSE file online at http://www.agavi.org/LICENSE.txt |
  9. // | vi: set noexpandtab: |
  10. // | Local Variables: |
  11. // | indent-tabs-mode: t |
  12. // | End: |
  13. // +---------------------------------------------------------------------------+
  14. /**
  15. * AgaviRequest provides methods for manipulating client request information
  16. * such as attributes, errors and parameters. It is also possible to manipulate
  17. * the request method originally sent by the user.
  18. *
  19. * @package agavi
  20. * @subpackage request
  21. *
  22. * @author Sean Kerr <skerr@mojavi.org>
  23. * @copyright Authors
  24. * @copyright The Agavi Project
  25. *
  26. * @since 0.9.0
  27. *
  28. * @version $Id: AgaviRequest.class.php 3915 2009-03-11 16:09:57Z saracen $
  29. */
  30. abstract class AgaviRequest extends AgaviAttributeHolder
  31. {
  32. /**
  33. * @var array An associative array of attributes
  34. */
  35. protected $attributes = array();
  36. /**
  37. * @var array An associative array of errors
  38. */
  39. protected $errors = array();
  40. /**
  41. * @var string The request method name
  42. */
  43. protected $method = null;
  44. /**
  45. * @var AgaviContext An AgaviContext instance.
  46. */
  47. protected $context = null;
  48. /**
  49. * @var AgaviRequestDataHolder The request data holder instance.
  50. */
  51. private $requestData = null;
  52. /**
  53. * @var string The key used to lock the request, or null if no lock set
  54. */
  55. private $key = null;
  56. /**
  57. * Retrieve the current application context.
  58. *
  59. * @return AgaviContext An AgaviContext instance.
  60. *
  61. * @author David Zülke <dz@bitxtender.com>
  62. * @since 0.11.0
  63. */
  64. public final function getContext()
  65. {
  66. return $this->context;
  67. }
  68. /**
  69. * Retrieve this requests method.
  70. *
  71. * @return string The request method name
  72. *
  73. * @author Sean Kerr <skerr@mojavi.org>
  74. * @author David Zülke <dz@bitxtender.com>
  75. * @since 0.9.0
  76. */
  77. public function getMethod()
  78. {
  79. return $this->method;
  80. }
  81. /**
  82. * Constructor.
  83. *
  84. * @author David Zülke <dz@bitxtender.com>
  85. * @since 0.11.0
  86. */
  87. public function __construct()
  88. {
  89. $this->setParameters(array(
  90. 'use_module_action_parameters' => false,
  91. 'module_accessor' => 'module',
  92. 'action_accessor' => 'action',
  93. 'request_data_holder_class' => 'AgaviRequestDataHolder',
  94. ));
  95. }
  96. /**
  97. * Initialize this Request.
  98. *
  99. * @param AgaviContext An AgaviContext instance.
  100. * @param array An associative array of initialization parameters.
  101. *
  102. * @throws <b>AgaviInitializationException</b> If an error occurs while
  103. * initializing this Request.
  104. *
  105. * @author David Zülke <dz@bitxtender.com>
  106. * @since 0.9.0
  107. */
  108. public function initialize(AgaviContext $context, array $parameters = array())
  109. {
  110. $this->context = $context;
  111. if(isset($parameters['default_namespace'])) {
  112. $this->defaultNamespace = $parameters['default_namespace'];
  113. unset($parameters['default_namespace']);
  114. }
  115. $this->setParameters($parameters);
  116. }
  117. /**
  118. * Set the request method.
  119. *
  120. * @param string The request method name.
  121. *
  122. * @author Sean Kerr <skerr@mojavi.org>
  123. * @author David Zülke <dz@bitxtender.com>
  124. * @since 0.9.0
  125. */
  126. public function setMethod($method)
  127. {
  128. $this->method = $method;
  129. }
  130. /**
  131. * Set the data holder instance of this request.
  132. *
  133. * @param AgaviRequestDataHolder The request data holder.
  134. *
  135. * @author David Zülke <dz@bitxtender.com>
  136. * @author Dominik del Bondio <ddb@bitxtender.com>
  137. * @since 0.11.0
  138. */
  139. final protected function setRequestData(AgaviRequestDataHolder $rd)
  140. {
  141. if(!$this->isLocked()) {
  142. $this->requestData = $rd;
  143. }
  144. }
  145. /**
  146. * Get the data holder instance of this request.
  147. *
  148. * @return AgaviRequestDataHolder The request data holder.
  149. *
  150. * @author David Zülke <dz@bitxtender.com>
  151. * @author Dominik del Bondio <ddb@bitxtender.com>
  152. * @since 0.11.0
  153. */
  154. final public function getRequestData()
  155. {
  156. if($this->isLocked()) {
  157. throw new AgaviException("Access to request data is locked during Action and View execution and while templates are rendered. Please use the local request data holder passed to your Action's or View's execute*() method to access request data.");
  158. }
  159. return $this->requestData;
  160. }
  161. /**
  162. * Do any necessary startup work after initialization.
  163. *
  164. * This method is not called directly after initialize().
  165. *
  166. * @author David Zülke <dz@bitxtender.com>
  167. * @since 0.11.0
  168. */
  169. public function startup()
  170. {
  171. if($this->getParameter("unset_input", true)) {
  172. // remove raw post data
  173. // can still be read from php://input, but we can't prevent that
  174. unset($GLOBALS['HTTP_RAW_POST_DATA']);
  175. // nuke argc and argc if necessary
  176. $rla = ini_get('register_long_arrays');
  177. if(isset($_SERVER['argc'])) {
  178. $_SERVER['argc'] = 0;
  179. if(isset($GLOBALS['argc'])) {
  180. $GLOBALS['argc'] = 0;
  181. }
  182. if($rla) {
  183. $GLOBALS['HTTP_SERVER_VARS']['argc'] = 0;
  184. }
  185. }
  186. if(isset($_SERVER['argv'])) {
  187. $_SERVER['argv'] = array();
  188. if(isset($GLOBALS['argv'])) {
  189. $GLOBALS['argv'] = array();
  190. }
  191. if($rla) {
  192. $GLOBALS['HTTP_SERVER_VARS']['argv'] = array();
  193. }
  194. }
  195. }
  196. }
  197. /**
  198. * Execute the shutdown procedure.
  199. *
  200. * @author Sean Kerr <skerr@mojavi.org>
  201. * @since 0.9.0
  202. */
  203. public function shutdown()
  204. {
  205. }
  206. /**
  207. * Get a value by trying to find the given key in $_SERVER first, then in
  208. * $_ENV. If nothing was found, return the key, or the given default value.
  209. *
  210. * @param mixed The key (or an array of keys) of the value to fetch.
  211. * @param mixed A default return value, or null if the key should be
  212. * returned (static return values can be defined this way).
  213. *
  214. * @author David Zülke
  215. * @since 0.11.0
  216. */
  217. public static function getSourceValue($keys, $default = null)
  218. {
  219. $keys = (array)$keys;
  220. // walk over all possible keys
  221. foreach($keys as $key) {
  222. if(isset($_SERVER[$key])) {
  223. return $_SERVER[$key];
  224. } elseif(isset($_ENV[$key])) {
  225. return $_ENV[$key];
  226. }
  227. }
  228. if($default !== null) {
  229. return $default;
  230. }
  231. // nothing found so far. remember that the keys list is an array
  232. if($keys) {
  233. return end($keys);
  234. }
  235. }
  236. /**
  237. * Whether or not the Request is locked.
  238. *
  239. * @author David Zülke <dz@bitxtender.com>
  240. * @since 0.11.0
  241. */
  242. public final function isLocked()
  243. {
  244. return $this->key !== null;
  245. }
  246. /**
  247. * Lock or unlock the Request so request data can(not) be fetched anymore.
  248. *
  249. * @param string The key to unlock, if the lock should be removed, or
  250. * null if the lock should be set.
  251. *
  252. * @return mixed The key, if a lock was set, or a boolean value indicating
  253. * whether or not the unlocking was successful.
  254. *
  255. * @author David Zülke <dz@bitxtender.com>
  256. * @since 0.11.0
  257. */
  258. public final function toggleLock($key = null)
  259. {
  260. if(!$this->isLocked() && $key === null) {
  261. $this->locked = true;
  262. return $this->key = AgaviToolkit::uniqid();
  263. } elseif($this->isLocked()) {
  264. if($this->key === $key) {
  265. $this->key = null;
  266. return true;
  267. }
  268. return false;
  269. }
  270. }
  271. }
  272. ?>