Subversion-Projekte lars-tiefland.php_share

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
1 lars 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
 * sfNamespacedParameterHolder provides a class for managing parameters
14
 * with support for namespaces.
15
 *
16
 * Parameters, in this case, are used to extend classes with additional data
17
 * that requires no additional logic to manage.
18
 *
19
 * @package    symfony
20
 * @subpackage util
21
 * @author     Fabien Potencier <fabien.potencier@symfony-project.com>
22
 * @author     Sean Kerr <sean@code-box.org>
23
 * @version    SVN: $Id: sfNamespacedParameterHolder.class.php 29521 2010-05-19 11:47:56Z fabien $
24
 */
25
class sfNamespacedParameterHolder extends sfParameterHolder
26
{
27
  protected $default_namespace = null;
28
  protected $parameters = array();
29
 
30
  /**
31
   * The constructor for sfNamespacedParameterHolder.
32
   *
33
   * The default namespace may be overridden at initialization as follows:
34
   * <code>
35
   * <?php
36
   * $mySpecialPH = new sfNamespacedParameterHolder('symfony/special');
37
   * ?>
38
   * </code>
39
   */
40
  public function __construct($namespace = 'symfony/default')
41
  {
42
    $this->default_namespace = $namespace;
43
  }
44
 
45
  /**
46
   * Sets the default namespace value.
47
   *
48
   * @param string $namespace  Default namespace
49
   * @param bool   $move       Move all values of the old default namespace to the new one or not
50
   */
51
  public function setDefaultNamespace($namespace, $move = true)
52
  {
53
    if ($move)
54
    {
55
      if (null !== $values = $this->removeNamespace())
56
      {
57
          $this->addByRef($values, $namespace);
58
      }
59
    }
60
 
61
    $this->default_namespace = $namespace;
62
  }
63
 
64
  /**
65
   * Get the default namespace value.
66
   *
67
   * The $default_namespace is defined as 'symfony/default'.
68
   *
69
   * @return string The default namespace
70
   */
71
  public function getDefaultNamespace()
72
  {
73
    return $this->default_namespace;
74
  }
75
 
76
  /**
77
   * Clear all parameters associated with this request.
78
   */
79
  public function clear()
80
  {
81
    $this->parameters = null;
82
    $this->parameters = array();
83
  }
84
 
85
  /**
86
   * Retrieve a parameter with an optionally specified namespace.
87
   *
88
   * An isolated namespace may be identified by providing a value for the third
89
   * argument.  If not specified, the default namespace 'symfony/default' is
90
   * used.
91
   *
92
   * @param string $name     A parameter name
93
   * @param mixed  $default  A default parameter value
94
   * @param string $ns       A parameter namespace
95
   *
96
   * @return mixed A parameter value, if the parameter exists, otherwise null
97
   */
98
  public function & get($name, $default = null, $ns = null)
99
  {
100
    if (!$ns)
101
    {
102
      $ns = $this->default_namespace;
103
    }
104
 
105
    if (isset($this->parameters[$ns][$name]))
106
    {
107
      $value = & $this->parameters[$ns][$name];
108
    }
109
    else
110
    {
111
      $value = $default;
112
    }
113
 
114
    return $value;
115
  }
116
 
117
  /**
118
   * Retrieve an array of parameter names from an optionally specified namespace.
119
   *
120
   * @param  string $ns  A parameter namespace.
121
   *
122
   * @return array An indexed array of parameter names, if the namespace exists, otherwise null
123
   */
124
  public function getNames($ns = null)
125
  {
126
    if (!$ns)
127
    {
128
      $ns = $this->default_namespace;
129
    }
130
 
131
    if (isset($this->parameters[$ns]))
132
    {
133
      return array_keys($this->parameters[$ns]);
134
    }
135
 
136
    return array();
137
  }
138
 
139
  /**
140
   * Retrieve an array of parameter namespaces.
141
   *
142
   * @return array An indexed array of parameter namespaces
143
   */
144
  public function getNamespaces()
145
  {
146
    return array_keys($this->parameters);
147
  }
148
 
149
  /**
150
   * Retrieve an array of parameters, within a namespace.
151
   *
152
   * This method is limited to a namespace.  Without any argument,
153
   * it returns the parameters of the default namespace.  If a
154
   * namespace is passed as an argument, only the parameters of the
155
   * specified namespace are returned.
156
   *
157
   * @param  string $ns  A parameter namespace
158
   *
159
   * @return array An associative array of parameters
160
   */
161
  public function & getAll($ns = null)
162
  {
163
    if (!$ns)
164
    {
165
      $ns = $this->default_namespace;
166
    }
167
 
168
    $parameters = array();
169
 
170
    if (isset($this->parameters[$ns]))
171
    {
172
      $parameters = $this->parameters[$ns];
173
    }
174
 
175
    return $parameters;
176
  }
177
 
178
  /**
179
   * Indicates whether or not a parameter exists.
180
   *
181
   * @param  string $name  A parameter name
182
   * @param  string $ns    A parameter namespace
183
   *
184
   * @return bool true, if the parameter exists, otherwise false
185
   */
186
  public function has($name, $ns = null)
187
  {
188
    if (!$ns)
189
    {
190
      $ns = $this->default_namespace;
191
    }
192
 
193
    return isset($this->parameters[$ns][$name]);
194
  }
195
 
196
  /**
197
   * Indicates whether or not A parameter namespace exists.
198
   *
199
   * @param  string $ns  A parameter namespace
200
   *
201
   * @return bool true, if the namespace exists, otherwise false
202
   */
203
  public function hasNamespace($ns)
204
  {
205
    return isset($this->parameters[$ns]);
206
  }
207
 
208
  /**
209
   * Remove a parameter.
210
   *
211
   * @param  string $name     A parameter name
212
   * @param  mixed  $default  A default parameter value
213
   * @param  string $ns       A parameter namespace
214
   *
215
   * @return string A parameter value, if the parameter was removed, otherwise null
216
   */
217
  public function remove($name, $default = null, $ns = null)
218
  {
219
    if (!$ns)
220
    {
221
      $ns = $this->default_namespace;
222
    }
223
 
224
    $retval = $default;
225
 
226
    if (isset($this->parameters[$ns]) && array_key_exists($name, $this->parameters[$ns]))
227
    {
228
      $retval = $this->parameters[$ns][$name];
229
      unset($this->parameters[$ns][$name]);
230
    }
231
 
232
    return $retval;
233
  }
234
 
235
  /**
236
   * Remove A parameter namespace and all of its associated parameters.
237
   *
238
   * @param string $ns  A parameter namespace.
239
   */
240
  public function & removeNamespace($ns = null)
241
  {
242
    if (!$ns)
243
    {
244
      $ns = $this->default_namespace;
245
    }
246
 
247
    $retval = null;
248
 
249
    if (isset($this->parameters[$ns]))
250
    {
251
      $retval =& $this->parameters[$ns];
252
      unset($this->parameters[$ns]);
253
    }
254
 
255
    return $retval;
256
  }
257
 
258
  /**
259
   * Set a parameter.
260
   *
261
   * If a parameter with the name already exists the value will be overridden.
262
   *
263
   * @param string $name   A parameter name
264
   * @param mixed  $value  A parameter value
265
   * @param string $ns     A parameter namespace
266
   */
267
  public function set($name, $value, $ns = null)
268
  {
269
    if (!$ns)
270
    {
271
      $ns = $this->default_namespace;
272
    }
273
 
274
    if (!isset($this->parameters[$ns]))
275
    {
276
      $this->parameters[$ns] = array();
277
    }
278
 
279
    $this->parameters[$ns][$name] = $value;
280
  }
281
 
282
  /**
283
   * Set a parameter by reference.
284
   *
285
   * If a parameter with the name already exists the value will be overridden.
286
   *
287
   * @param string $name   A parameter name
288
   * @param mixed  $value  A reference to a parameter value
289
   * @param string $ns     A parameter namespace
290
   */
291
  public function setByRef($name, & $value, $ns = null)
292
  {
293
    if (!$ns)
294
    {
295
      $ns = $this->default_namespace;
296
    }
297
 
298
    if (!isset($this->parameters[$ns]))
299
    {
300
      $this->parameters[$ns] = array();
301
    }
302
 
303
    $this->parameters[$ns][$name] =& $value;
304
  }
305
 
306
  /**
307
   * Set an array of parameters.
308
   *
309
   * If an existing parameter name matches any of the keys in the supplied
310
   * array, the associated value will be overridden.
311
   *
312
   * @param array  $parameters  An associative array of parameters and their associated values
313
   * @param string $ns          A parameter namespace
314
   */
315
  public function add($parameters, $ns = null)
316
  {
317
    if ($parameters === null) return;
318
 
319
    if (!$ns)
320
    {
321
      $ns = $this->default_namespace;
322
    }
323
 
324
    if (!isset($this->parameters[$ns]))
325
    {
326
      $this->parameters[$ns] = array();
327
    }
328
 
329
    foreach ($parameters as $key => $value)
330
    {
331
      $this->parameters[$ns][$key] = $value;
332
    }
333
  }
334
 
335
  /**
336
   * Set an array of parameters by reference.
337
   *
338
   * If an existing parameter name matches any of the keys in the supplied
339
   * array, the associated value will be overridden.
340
   *
341
   * @param array  $parameters  An associative array of parameters and references to their associated values
342
   * @param string $ns          A parameter namespace
343
   */
344
  public function addByRef(& $parameters, $ns = null)
345
  {
346
    if (!$ns)
347
    {
348
      $ns = $this->default_namespace;
349
    }
350
 
351
    if (!isset($this->parameters[$ns]))
352
    {
353
      $this->parameters[$ns] = array();
354
    }
355
 
356
    foreach ($parameters as $key => &$value)
357
    {
358
      $this->parameters[$ns][$key] =& $value;
359
    }
360
  }
361
 
362
  /**
363
   * Serializes the current instance.
364
   *
365
   * @return array Objects instance
366
   */
367
  public function serialize()
368
  {
369
    return serialize(array($this->default_namespace, $this->parameters));
370
  }
371
 
372
  /**
373
   * Unserializes a sfNamespacedParameterHolder instance.
374
   *
375
   * @param string $serialized  A serialized sfNamespacedParameterHolder instance
376
   */
377
  public function unserialize($serialized)
378
  {
379
    $data = unserialize($serialized);
380
 
381
    $this->default_namespace = $data[0];
382
    $this->parameters = $data[1];
383
  }
384
}