Development

/branches/1.3/lib/request/sfRequest.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/request/sfRequest.class.php

Revision 28641, 8.3 kB (checked in by fabien, 5 years ago)

[1.3, 1.4] fixed typos in the PHPDocs (closes #8432 - patch from Pascal)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
5  * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
6  * (c) 2004-2006 Sean Kerr <sean@code-box.org>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 /**
13  * sfRequest provides methods for manipulating client request information such
14  * as attributes, and parameters. It is also possible to manipulate the
15  * request method originally sent by the user.
16  *
17  * @package    symfony
18  * @subpackage request
19  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
20  * @author     Sean Kerr <sean@code-box.org>
21  * @version    SVN: $Id$
22  */
23 abstract class sfRequest implements ArrayAccess
24 {
25   const GET    = 'GET';
26   const POST   = 'POST';
27   const PUT    = 'PUT';
28   const DELETE = 'DELETE';
29   const HEAD   = 'HEAD';
30
31   protected
32     $dispatcher      = null,
33     $content         = null,
34     $method          = null,
35     $options         = array(),
36     $parameterHolder = null,
37     $attributeHolder = null;
38
39   /**
40    * Class constructor.
41    *
42    * @see initialize()
43    */
44   public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
45   {
46     $this->initialize($dispatcher, $parameters, $attributes, $options);
47   }
48
49   /**
50    * Initializes this sfRequest.
51    *
52    * Available options:
53    *
54    *  * logging: Whether to enable logging or not (false by default)
55    *
56    * @param  sfEventDispatcher $dispatcher  An sfEventDispatcher instance
57    * @param  array             $parameters  An associative array of initialization parameters
58    * @param  array             $attributes  An associative array of initialization attributes
59    * @param  array             $options     An associative array of options
60    *
61    * @return bool true, if initialization completes successfully, otherwise false
62    *
63    * @throws <b>sfInitializationException</b> If an error occurs while initializing this sfRequest
64    */
65   public function initialize(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array())
66   {
67     $this->dispatcher = $dispatcher;
68
69     $this->options = $options;
70
71     if (!isset($this->options['logging']))
72     {
73       $this->options['logging'] = false;
74     }
75
76     // initialize parameter and attribute holders
77     $this->parameterHolder = new sfParameterHolder();
78     $this->attributeHolder = new sfParameterHolder();
79
80     $this->parameterHolder->add($parameters);
81     $this->attributeHolder->add($attributes);
82   }
83
84   /**
85    * Returns the options.
86    *
87    * @return array The options.
88    */
89   public function getOptions()
90   {
91     return $this->options;
92   }
93
94   /**
95    * Extracts parameter values from the request.
96    *
97    * @param  array $names  An indexed array of parameter names to extract
98    *
99    * @return array An associative array of parameters and their values. If
100    *               a specified parameter doesn't exist an empty string will
101    *               be returned for its value
102    */
103   public function extractParameters($names)
104   {
105     $array = array();
106
107     $parameters = $this->parameterHolder->getAll();
108     foreach ($parameters as $key => $value)
109     {
110       if (in_array($key, $names))
111       {
112         $array[$key] = $value;
113       }
114     }
115
116     return $array;
117   }
118
119   /**
120    * Gets the request method.
121    *
122    * @return string The request method
123    */
124   public function getMethod()
125   {
126     return $this->method;
127   }
128
129   /**
130    * Sets the request method.
131    *
132    * @param string $method  The request method
133    *
134    * @throws <b>sfException</b> - If the specified request method is invalid
135    */
136   public function setMethod($method)
137   {
138     if (!in_array(strtoupper($method), array(self::GET, self::POST, self::PUT, self::DELETE, self::HEAD)))
139     {
140       throw new sfException(sprintf('Invalid request method: %s.', $method));
141     }
142
143     $this->method = strtoupper($method);
144   }
145
146   /**
147    * Returns true if the request parameter exists (implements the ArrayAccess interface).
148    *
149    * @param  string $name The name of the request parameter
150    *
151    * @return Boolean true if the request parameter exists, false otherwise
152    */
153   public function offsetExists($name)
154   {
155     return $this->hasParameter($name);
156   }
157
158   /**
159    * Returns the request parameter associated with the name (implements the ArrayAccess interface).
160    *
161    * @param  string $name  The offset of the value to get
162    *
163    * @return mixed The request parameter if exists, null otherwise
164    */
165   public function offsetGet($name)
166   {
167     return $this->getParameter($name, false);
168   }
169
170   /**
171    * Sets the request parameter associated with the offset (implements the ArrayAccess interface).
172    *
173    * @param string $offset The parameter name
174    * @param string $value The parameter value
175    */
176   public function offsetSet($offset, $value)
177   {
178     $this->setParameter($offset, $value);
179   }
180
181   /**
182    * Removes a request parameter.
183    *
184    * @param string $offset The parameter name
185    */
186   public function offsetUnset($offset)
187   {
188     $this->getParameterHolder()->remove($offset);
189   }
190
191   /**
192    * Retrieves the parameters for the current request.
193    *
194    * @return sfParameterHolder The parameter holder
195    */
196   public function getParameterHolder()
197   {
198     return $this->parameterHolder;
199   }
200
201   /**
202    * Retrieves the attributes holder.
203    *
204    * @return sfParameterHolder The attribute holder
205    */
206   public function getAttributeHolder()
207   {
208     return $this->attributeHolder;
209   }
210
211   /**
212    * Retrieves an attribute from the current request.
213    *
214    * @param  string $name     Attribute name
215    * @param  string $default  Default attribute value
216    *
217    * @return mixed An attribute value
218    */
219   public function getAttribute($name, $default = null)
220   {
221     return $this->attributeHolder->get($name, $default);
222   }
223
224   /**
225    * Indicates whether or not an attribute exist for the current request.
226    *
227    * @param  string $name  Attribute name
228    *
229    * @return bool true, if the attribute exists otherwise false
230    */
231   public function hasAttribute($name)
232   {
233     return $this->attributeHolder->has($name);
234   }
235
236   /**
237    * Sets an attribute for the request.
238    *
239    * @param string $name   Attribute name
240    * @param string $value  Value for the attribute
241    *
242    */
243   public function setAttribute($name, $value)
244   {
245     $this->attributeHolder->set($name, $value);
246   }
247
248   /**
249    * Retrieves a parameter for the current request.
250    *
251    * @param string $name     Parameter name
252    * @param string $default  Parameter default value
253    *
254    */
255   public function getParameter($name, $default = null)
256   {
257     return $this->parameterHolder->get($name, $default);
258   }
259
260   /**
261    * Indicates whether or not a parameter exist for the current request.
262    *
263    * @param  string $name  Parameter name
264    *
265    * @return bool true, if the parameter exists otherwise false
266    */
267   public function hasParameter($name)
268   {
269     return $this->parameterHolder->has($name);
270   }
271
272   /**
273    * Sets a parameter for the current request.
274    *
275    * @param string $name   Parameter name
276    * @param string $value  Parameter value
277    *
278    */
279   public function setParameter($name, $value)
280   {
281     $this->parameterHolder->set($name, $value);
282   }
283
284   /**
285    * Returns the content of the current request.
286    *
287    * @return string|Boolean The content or false if none is available
288    */
289   public function getContent()
290   {
291     if (null === $this->content)
292     {
293       if (0 === strlen(trim($this->content = file_get_contents('php://input'))))
294       {
295         $this->content = false;
296       }
297     }
298
299     return $this->content;
300   }
301
302   /**
303    * Calls methods defined via sfEventDispatcher.
304    *
305    * @param  string $method     The method name
306    * @param  array  $arguments  The method arguments
307    *
308    * @return mixed The returned value of the called method
309    *
310    * @throws <b>sfException</b> if call fails
311    */
312   public function __call($method, $arguments)
313   {
314     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'request.method_not_found', array('method' => $method, 'arguments' => $arguments)));
315     if (!$event->isProcessed())
316     {
317       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
318     }
319
320     return $event->getReturnValue();
321   }
322
323   public function __clone()
324   {
325     $this->parameterHolder = clone $this->parameterHolder;
326     $this->attributeHolder = clone $this->attributeHolder;
327   }
328 }
329
Note: See TracBrowser for help on using the browser.