Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpFoundation\Session\Attribute; Chris@0: Chris@0: /** Chris@0: * This class provides structured storage of session attributes using Chris@0: * a name spacing character in the key. Chris@0: * Chris@0: * @author Drak Chris@0: */ Chris@0: class NamespacedAttributeBag extends AttributeBag Chris@0: { Chris@0: /** Chris@0: * Namespace character. Chris@0: * Chris@0: * @var string Chris@0: */ Chris@0: private $namespaceCharacter; Chris@0: Chris@0: /** Chris@0: * Constructor. Chris@0: * Chris@0: * @param string $storageKey Session storage key Chris@0: * @param string $namespaceCharacter Namespace character to use in keys Chris@0: */ Chris@0: public function __construct($storageKey = '_sf2_attributes', $namespaceCharacter = '/') Chris@0: { Chris@0: $this->namespaceCharacter = $namespaceCharacter; Chris@0: parent::__construct($storageKey); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function has($name) Chris@0: { Chris@0: // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is Chris@0: $attributes = $this->resolveAttributePath($name); Chris@0: $name = $this->resolveKey($name); Chris@0: Chris@0: if (null === $attributes) { Chris@0: return false; Chris@0: } Chris@0: Chris@0: return array_key_exists($name, $attributes); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function get($name, $default = null) Chris@0: { Chris@0: // reference mismatch: if fixed, re-introduced in array_key_exists; keep as it is Chris@0: $attributes = $this->resolveAttributePath($name); Chris@0: $name = $this->resolveKey($name); Chris@0: Chris@0: if (null === $attributes) { Chris@0: return $default; Chris@0: } Chris@0: Chris@0: return array_key_exists($name, $attributes) ? $attributes[$name] : $default; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function set($name, $value) Chris@0: { Chris@0: $attributes = &$this->resolveAttributePath($name, true); Chris@0: $name = $this->resolveKey($name); Chris@0: $attributes[$name] = $value; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function remove($name) Chris@0: { Chris@0: $retval = null; Chris@0: $attributes = &$this->resolveAttributePath($name); Chris@0: $name = $this->resolveKey($name); Chris@0: if (null !== $attributes && array_key_exists($name, $attributes)) { Chris@0: $retval = $attributes[$name]; Chris@0: unset($attributes[$name]); Chris@0: } Chris@0: Chris@0: return $retval; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves a path in attributes property and returns it as a reference. Chris@0: * Chris@0: * This method allows structured namespacing of session attributes. Chris@0: * Chris@0: * @param string $name Key name Chris@0: * @param bool $writeContext Write context, default false Chris@0: * Chris@0: * @return array Chris@0: */ Chris@0: protected function &resolveAttributePath($name, $writeContext = false) Chris@0: { Chris@0: $array = &$this->attributes; Chris@0: $name = (strpos($name, $this->namespaceCharacter) === 0) ? substr($name, 1) : $name; Chris@0: Chris@0: // Check if there is anything to do, else return Chris@0: if (!$name) { Chris@0: return $array; Chris@0: } Chris@0: Chris@0: $parts = explode($this->namespaceCharacter, $name); Chris@0: if (count($parts) < 2) { Chris@0: if (!$writeContext) { Chris@0: return $array; Chris@0: } Chris@0: Chris@0: $array[$parts[0]] = array(); Chris@0: Chris@0: return $array; Chris@0: } Chris@0: Chris@0: unset($parts[count($parts) - 1]); Chris@0: Chris@0: foreach ($parts as $part) { Chris@0: if (null !== $array && !array_key_exists($part, $array)) { Chris@0: $array[$part] = $writeContext ? array() : null; Chris@0: } Chris@0: Chris@0: $array = &$array[$part]; Chris@0: } Chris@0: Chris@0: return $array; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Resolves the key from the name. Chris@0: * Chris@0: * This is the last part in a dot separated string. Chris@0: * Chris@0: * @param string $name Chris@0: * Chris@0: * @return string Chris@0: */ Chris@0: protected function resolveKey($name) Chris@0: { Chris@0: if (false !== $pos = strrpos($name, $this->namespaceCharacter)) { Chris@0: $name = substr($name, $pos + 1); Chris@0: } Chris@0: Chris@0: return $name; Chris@0: } Chris@0: }