Development

/branches/1.4/lib/user/sfUser.class.php

You must first sign up to be able to contribute.

root/branches/1.4/lib/user/sfUser.class.php

Revision 23810, 9.4 kB (checked in by Kris.Wallsmith, 5 years ago)

[1.3] set svn:eol-style property to native and svn:keywords property to Id on all .php files

  • 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  *
14  * sfUser wraps a client session and provides accessor methods for user
15  * attributes. It also makes storing and retrieving multiple page form data
16  * rather easy by allowing user attributes to be stored in namespaces, which
17  * help organize data.
18  *
19  * @package    symfony
20  * @subpackage user
21  * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
22  * @author     Sean Kerr <sean@code-box.org>
23  * @version    SVN: $Id$
24  */
25 class sfUser implements ArrayAccess
26 {
27   /**
28    * The namespace under which attributes will be stored.
29    */
30   const ATTRIBUTE_NAMESPACE = 'symfony/user/sfUser/attributes';
31
32   const CULTURE_NAMESPACE = 'symfony/user/sfUser/culture';
33
34   protected
35     $options         = array(),
36     $attributeHolder = null,
37     $culture         = null,
38     $storage         = null,
39     $dispatcher      = null;
40
41   /**
42    * Class constructor.
43    *
44    * @see initialize()
45    */
46   public function __construct(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
47   {
48     $this->initialize($dispatcher, $storage, $options);
49
50     if ($this->options['auto_shutdown'])
51     {
52       register_shutdown_function(array($this, 'shutdown'));
53     }
54   }
55
56   /**
57    * Initializes this sfUser.
58    *
59    * Available options:
60    *
61    *  * auto_shutdown:   Whether to automatically save the changes to the session (true by default)
62    *  * culture:         The user culture
63    *  * default_culture: The default user culture (en by default)
64    *  * use_flash:       Whether to enable flash usage (false by default)
65    *  * logging:         Whether to enable logging (false by default)
66    *
67    * @param sfEventDispatcher $dispatcher  An sfEventDispatcher instance.
68    * @param sfStorage         $storage     An sfStorage instance.
69    * @param array             $options     An associative array of options.
70    *
71    * @return Boolean          true, if initialization completes successfully, otherwise false.
72    */
73   public function initialize(sfEventDispatcher $dispatcher, sfStorage $storage, $options = array())
74   {
75     $this->dispatcher = $dispatcher;
76     $this->storage    = $storage;
77
78     $this->options = array_merge(array(
79       'auto_shutdown'   => true,
80       'culture'         => null,
81       'default_culture' => 'en',
82       'use_flash'       => false,
83       'logging'         => false,
84     ), $options);
85
86     $this->attributeHolder = new sfNamespacedParameterHolder(self::ATTRIBUTE_NAMESPACE);
87
88     // read attributes from storage
89     $attributes = $storage->read(self::ATTRIBUTE_NAMESPACE);
90     if (is_array($attributes))
91     {
92       foreach ($attributes as $namespace => $values)
93       {
94         $this->attributeHolder->add($values, $namespace);
95       }
96     }
97
98     // set the user culture to sf_culture parameter if present in the request
99     // otherwise
100     //  - use the culture defined in the user session
101     //  - use the default culture set in settings.yml
102     $currentCulture = $storage->read(self::CULTURE_NAMESPACE);
103     $this->setCulture(null !== $this->options['culture'] ? $this->options['culture'] : (null !== $currentCulture ? $currentCulture : $this->options['default_culture']));
104
105     // flag current flash to be removed at shutdown
106     if ($this->options['use_flash'] && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash'))
107     {
108       if ($this->options['logging'])
109       {
110         $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Flag old flash messages ("%s")', implode('", "', $names)))));
111       }
112
113       foreach ($names as $name)
114       {
115         $this->attributeHolder->set($name, true, 'symfony/user/sfUser/flash/remove');
116       }
117     }
118   }
119
120   /**
121    * Returns the initialization options
122    *
123    * @return array The options used to initialize sfUser
124    */
125   public function getOptions()
126   {
127     return $this->options;
128   }
129
130   /**
131    * Sets the user culture.
132    *
133    * @param string $culture
134    */
135   public function setCulture($culture)
136   {
137     if ($this->culture != $culture)
138     {
139       $this->culture = $culture;
140
141       $this->dispatcher->notify(new sfEvent($this, 'user.change_culture', array('culture' => $culture)));
142     }
143   }
144
145   /**
146    * Sets a flash variable that will be passed to the very next action.
147    *
148    * @param  string $name     The name of the flash variable
149    * @param  string $value    The value of the flash variable
150    * @param  bool   $persist  true if the flash have to persist for the following request (true by default)
151    */
152   public function setFlash($name, $value, $persist = true)
153   {
154     if (!$this->options['use_flash'])
155     {
156       return;
157     }
158
159     $this->setAttribute($name, $value, 'symfony/user/sfUser/flash');
160
161     if ($persist)
162     {
163       // clear removal flag
164       $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash/remove');
165     }
166     else
167     {
168       $this->setAttribute($name, true, 'symfony/user/sfUser/flash/remove');
169     }
170   }
171
172   /**
173    * Gets a flash variable.
174    *
175    * @param  string $name     The name of the flash variable
176    * @param  string $default  The default value returned when named variable does not exist.
177    *
178    * @return mixed The value of the flash variable
179    */
180   public function getFlash($name, $default = null)
181   {
182     if (!$this->options['use_flash'])
183     {
184       return $default;
185     }
186
187     return $this->getAttribute($name, $default, 'symfony/user/sfUser/flash');
188   }
189
190   /**
191    * Returns true if a flash variable of the specified name exists.
192    *
193    * @param  string $name  The name of the flash variable
194    *
195    * @return bool true if the variable exists, false otherwise
196    */
197   public function hasFlash($name)
198   {
199     if (!$this->options['use_flash'])
200     {
201       return false;
202     }
203
204     return $this->hasAttribute($name, 'symfony/user/sfUser/flash');
205   }
206
207   /**
208    * Gets culture.
209    *
210    * @return string
211    */
212   public function getCulture()
213   {
214     return $this->culture;
215   }
216
217   /**
218    * Returns true if the user attribute exists (implements the ArrayAccess interface).
219    *
220    * @param  string $name The name of the user attribute
221    *
222    * @return Boolean true if the user attribute exists, false otherwise
223    */
224   public function offsetExists($name)
225   {
226     return $this->hasAttribute($name);
227   }
228
229   /**
230    * Returns the user attribute associated with the name (implements the ArrayAccess interface).
231    *
232    * @param  string $name  The offset of the value to get
233    *
234    * @return mixed The user attribute if exists, null otherwise
235    */
236   public function offsetGet($name)
237   {
238     return $this->getAttribute($name, false);
239   }
240
241   /**
242    * Sets the user attribute associated with the offset (implements the ArrayAccess interface).
243    *
244    * @param string $offset The parameter name
245    * @param string $value The parameter value
246    */
247   public function offsetSet($offset, $value)
248   {
249     $this->setAttribute($offset, $value);
250   }
251
252   /**
253    * Unsets the user attribute associated with the offset (implements the ArrayAccess interface).
254    *
255    * @param string $offset The parameter name
256    */
257   public function offsetUnset($offset)
258   {
259     $this->getAttributeHolder()->remove($offset);
260   }
261
262   public function getAttributeHolder()
263   {
264     return $this->attributeHolder;
265   }
266
267   public function getAttribute($name, $default = null, $ns = null)
268   {
269     return $this->attributeHolder->get($name, $default, $ns);
270   }
271
272   public function hasAttribute($name, $ns = null)
273   {
274     return $this->attributeHolder->has($name, $ns);
275   }
276
277   public function setAttribute($name, $value, $ns = null)
278   {
279     return $this->attributeHolder->set($name, $value, $ns);
280   }
281
282   /**
283    * Executes the shutdown procedure.
284    */
285   public function shutdown()
286   {
287     // remove flash that are tagged to be removed
288     if ($this->options['use_flash'] && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash/remove'))
289     {
290       if ($this->options['logging'])
291       {
292         $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Remove old flash messages ("%s")', implode('", "', $names)))));
293       }
294
295       foreach ($names as $name)
296       {
297         $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash');
298         $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash/remove');
299       }
300     }
301
302     $attributes = array();
303     foreach ($this->attributeHolder->getNamespaces() as $namespace)
304     {
305       $attributes[$namespace] = $this->attributeHolder->getAll($namespace);
306     }
307
308     // write attributes to the storage
309     $this->storage->write(self::ATTRIBUTE_NAMESPACE, $attributes);
310
311     // write culture to the storage
312     $this->storage->write(self::CULTURE_NAMESPACE, $this->culture);
313   }
314
315   /**
316    * Calls methods defined via sfEventDispatcher.
317    *
318    * @param string $method     The method name
319    * @param array  $arguments  The method arguments
320    *
321    * @return mixed The returned value of the called method
322    *
323    * @throws sfException If the calls fails
324    */
325   public function __call($method, $arguments)
326   {
327     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'user.method_not_found', array('method' => $method, 'arguments' => $arguments)));
328     if (!$event->isProcessed())
329     {
330       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
331     }
332
333     return $event->getReturnValue();
334   }
335 }
336
Note: See TracBrowser for help on using the browser.