123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- * (c) 2004-2006 Sean Kerr <sean@code-box.org>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- * sfNamespacedParameterHolder provides a class for managing parameters
- * with support for namespaces.
- *
- * Parameters, in this case, are used to extend classes with additional data
- * that requires no additional logic to manage.
- *
- * @package symfony
- * @subpackage util
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @author Sean Kerr <sean@code-box.org>
- * @version SVN: $Id: sfNamespacedParameterHolder.class.php 9051 2008-05-19 11:43:00Z FabianLange $
- */
- class sfNamespacedParameterHolder extends sfParameterHolder
- {
- protected $default_namespace = null;
- protected $parameters = array();
- /**
- * The constructor for sfNamespacedParameterHolder.
- *
- * The default namespace may be overridden at initialization as follows:
- * <code>
- * <?php
- * $mySpecialPH = new sfNamespacedParameterHolder('symfony/special');
- * ?>
- * </code>
- */
- public function __construct($namespace = 'symfony/default')
- {
- $this->default_namespace = $namespace;
- }
- /**
- * Sets the default namespace value.
- *
- * @param string $namespace Default namespace
- * @param bool $move Move all values of the old default namespace to the new one or not
- */
- public function setDefaultNamespace($namespace, $move = true)
- {
- if ($move)
- {
- $values = $this->removeNamespace();
- $this->addByRef($values, $namespace);
- }
- $this->default_namespace = $namespace;
- }
- /**
- * Get the default namespace value.
- *
- * The $default_namespace is defined as 'symfony/default'.
- *
- * @return string The default namespace
- */
- public function getDefaultNamespace()
- {
- return $this->default_namespace;
- }
- /**
- * Clear all parameters associated with this request.
- */
- public function clear()
- {
- $this->parameters = null;
- $this->parameters = array();
- }
- /**
- * Retrieve a parameter with an optionally specified namespace.
- *
- * An isolated namespace may be identified by providing a value for the third
- * argument. If not specified, the default namespace 'symfony/default' is
- * used.
- *
- * @param string $name A parameter name
- * @param mixed $default A default parameter value
- * @param string $ns A parameter namespace
- *
- * @return mixed A parameter value, if the parameter exists, otherwise null
- */
- public function & get($name, $default = null, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (isset($this->parameters[$ns][$name]))
- {
- $value = & $this->parameters[$ns][$name];
- }
- else if (isset($this->parameters[$ns]))
- {
- $value = sfToolkit::getArrayValueForPath($this->parameters[$ns], $name, $default);
- }
- else
- {
- $value = $default;
- }
- return $value;
- }
- /**
- * Retrieve an array of parameter names from an optionally specified namespace.
- *
- * @param string $ns A parameter namespace.
- *
- * @return array An indexed array of parameter names, if the namespace exists, otherwise null
- */
- public function getNames($ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (isset($this->parameters[$ns]))
- {
- return array_keys($this->parameters[$ns]);
- }
- return array();
- }
- /**
- * Retrieve an array of parameter namespaces.
- *
- * @return array An indexed array of parameter namespaces
- */
- public function getNamespaces()
- {
- return array_keys($this->parameters);
- }
- /**
- * Retrieve an array of parameters, within a namespace.
- *
- * This method is limited to a namespace. Without any argument,
- * it returns the parameters of the default namespace. If a
- * namespace is passed as an argument, only the parameters of the
- * specified namespace are returned.
- *
- * @param string $ns A parameter namespace
- *
- * @return array An associative array of parameters
- */
- public function & getAll($ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- $parameters = array();
- if (isset($this->parameters[$ns]))
- {
- $parameters = $this->parameters[$ns];
- }
- return $parameters;
- }
- /**
- * Indicates whether or not a parameter exists.
- *
- * @param string $name A parameter name
- * @param string $ns A parameter namespace
- *
- * @return bool true, if the parameter exists, otherwise false
- */
- public function has($name, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (isset($this->parameters[$ns][$name]))
- {
- return true;
- }
- else if (isset($this->parameters[$ns]))
- {
- return sfToolkit::hasArrayValueForPath($this->parameters[$ns], $name);
- }
- return false;
- }
- /**
- * Indicates whether or not A parameter namespace exists.
- *
- * @param string $ns A parameter namespace
- *
- * @return bool true, if the namespace exists, otherwise false
- */
- public function hasNamespace($ns)
- {
- return isset($this->parameters[$ns]);
- }
- /**
- * Remove a parameter.
- *
- * @param string $name A parameter name
- * @param mixed $default A default parameter value
- * @param string $ns A parameter namespace
- *
- * @return string A parameter value, if the parameter was removed, otherwise null
- */
- public function remove($name, $default = null, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- $retval = $default;
- if (isset($this->parameters[$ns]) && array_key_exists($name, $this->parameters[$ns]))
- {
- $retval = $this->parameters[$ns][$name];
- unset($this->parameters[$ns][$name]);
- }
- else
- {
- $retval = sfToolkit::removeArrayValueForPath($this->parameters[$ns], $name, $default);
- }
- return $retval;
- }
- /**
- * Remove A parameter namespace and all of its associated parameters.
- *
- * @param string $ns A parameter namespace.
- */
- public function & removeNamespace($ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- $retval = null;
- if (isset($this->parameters[$ns]))
- {
- $retval =& $this->parameters[$ns];
- unset($this->parameters[$ns]);
- }
- return $retval;
- }
- /**
- * Set a parameter.
- *
- * If a parameter with the name already exists the value will be overridden.
- *
- * @param string $name A parameter name
- * @param mixed $value A parameter value
- * @param string $ns A parameter namespace
- */
- public function set($name, $value, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (!isset($this->parameters[$ns]))
- {
- $this->parameters[$ns] = array();
- }
- $this->parameters[$ns][$name] = $value;
- }
- /**
- * Set a parameter by reference.
- *
- * If a parameter with the name already exists the value will be overridden.
- *
- * @param string $name A parameter name
- * @param mixed $value A reference to a parameter value
- * @param string $ns A parameter namespace
- */
- public function setByRef($name, & $value, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (!isset($this->parameters[$ns]))
- {
- $this->parameters[$ns] = array();
- }
- $this->parameters[$ns][$name] =& $value;
- }
- /**
- * Set an array of parameters.
- *
- * If an existing parameter name matches any of the keys in the supplied
- * array, the associated value will be overridden.
- *
- * @param array $parameters An associative array of parameters and their associated values
- * @param string $ns A parameter namespace
- */
- public function add($parameters, $ns = null)
- {
- if ($parameters === null) return;
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (!isset($this->parameters[$ns]))
- {
- $this->parameters[$ns] = array();
- }
- foreach ($parameters as $key => $value)
- {
- $this->parameters[$ns][$key] = $value;
- }
- }
- /**
- * Set an array of parameters by reference.
- *
- * If an existing parameter name matches any of the keys in the supplied
- * array, the associated value will be overridden.
- *
- * @param array $parameters An associative array of parameters and references to their associated values
- * @param string $ns A parameter namespace
- */
- public function addByRef(& $parameters, $ns = null)
- {
- if (!$ns)
- {
- $ns = $this->default_namespace;
- }
- if (!isset($this->parameters[$ns]))
- {
- $this->parameters[$ns] = array();
- }
- foreach ($parameters as $key => &$value)
- {
- $this->parameters[$ns][$key] =& $value;
- }
- }
- /**
- * Serializes the current instance.
- *
- * @return array Objects instance
- */
- public function serialize()
- {
- return serialize(array($this->default_namespace, $this->parameters));
- }
- /**
- * Unserializes a sfNamespacedParameterHolder instance.
- *
- * @param string $serialized A serialized sfNamespacedParameterHolder instance
- */
- public function unserialize($serialized)
- {
- $data = unserialize($serialized);
- $this->default_namespace = $data[0];
- $this->parameters = $data[1];
- }
- }
|