Development

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

You must first sign up to be able to contribute.

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

Revision 9060, 8.2 kB (checked in by FabianLange, 6 years ago)

1.1: fixed @param phpdoc to fit specs in user (refs #2991)

  • Property svn:mime-type set to text/x-php
  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Rev Date
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
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(!is_null($this->options['culture']) ? $this->options['culture'] : (!is_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   public function getAttributeHolder()
218   {
219     return $this->attributeHolder;
220   }
221
222   public function getAttribute($name, $default = null, $ns = null)
223   {
224     return $this->attributeHolder->get($name, $default, $ns);
225   }
226
227   public function hasAttribute($name, $ns = null)
228   {
229     return $this->attributeHolder->has($name, $ns);
230   }
231
232   public function setAttribute($name, $value, $ns = null)
233   {
234     return $this->attributeHolder->set($name, $value, $ns);
235   }
236
237   /**
238    * Executes the shutdown procedure.
239    */
240   public function shutdown()
241   {
242     // remove flash that are tagged to be removed
243     if ($this->options['use_flash'] && $names = $this->attributeHolder->getNames('symfony/user/sfUser/flash/remove'))
244     {
245       if ($this->options['logging'])
246       {
247         $this->dispatcher->notify(new sfEvent($this, 'application.log', array(sprintf('Remove old flash messages ("%s")', implode('", "', $names)))));
248       }
249
250       foreach ($names as $name)
251       {
252         $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash');
253         $this->attributeHolder->remove($name, null, 'symfony/user/sfUser/flash/remove');
254       }
255     }
256
257     $attributes = array();
258     foreach ($this->attributeHolder->getNamespaces() as $namespace)
259     {
260       $attributes[$namespace] = $this->attributeHolder->getAll($namespace);
261     }
262
263     // write attributes to the storage
264     $this->storage->write(self::ATTRIBUTE_NAMESPACE, $attributes);
265
266     // write culture to the storage
267     $this->storage->write(self::CULTURE_NAMESPACE, $this->culture);
268
269     session_write_close();
270   }
271
272   /**
273    * Calls methods defined via sfEventDispatcher.
274    *
275    * @param string $method     The method name
276    * @param array  $arguments  The method arguments
277    *
278    * @return mixed The returned value of the called method
279    *
280    * @throws sfException If the calls fails
281    */
282   public function __call($method, $arguments)
283   {
284     $event = $this->dispatcher->notifyUntil(new sfEvent($this, 'user.method_not_found', array('method' => $method, 'arguments' => $arguments)));
285     if (!$event->isProcessed())
286     {
287       throw new sfException(sprintf('Call to undefined method %s::%s.', get_class($this), $method));
288     }
289
290     return $event->getReturnValue();
291   }
292 }
293
Note: See TracBrowser for help on using the browser.