# Session

class Session implements SessionInterface, IteratorAggregate, Countable

## Properties

`protected` | `$storage` |  |

## Methods

`public` | `` | `__construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)` | No description |
`public` | `bool` | `start()` | Starts the session storage. |
`public` | `bool` | `has(string $name)` | Checks if an attribute is defined. |
`public` | `mixed` | `get(string $name, mixed $default = null)` | Returns an attribute. |
`public` | `` | `set(string $name, mixed $value)` | Sets an attribute. |
`public` | `array` | `all()` | Returns attributes. |
`public` | `` | `replace(array $attributes)` | Sets attributes. |
`public` | `mixed` | `remove(string $name)` | Removes an attribute. |
`public` | `` | `clear()` | Clears all attributes. |
`public` | `bool` | `isStarted()` | Checks if the session was started. |
`public` | `ArrayIterator` | `getIterator()` | Returns an iterator for attributes. |
`public` | `int` | `count()` | Returns the number of attributes. |
`public` | `bool` | `hasBeenStarted()` | No description |
`public` | `bool` | `isEmpty()` | No description |
`public` | `bool` | `invalidate(int $lifetime = null)` | Invalidates the current session. |
`public` | `bool` | `migrate(bool $destroy = false, int $lifetime = null)` | Migrates the current session to a new session id while maintaining all session attributes. |
`public` | `` | `save()` | Force the session to be saved and closed. |
`public` | `string` | `getId()` | Returns the session ID. |
`public` | `` | `setId(string $id)` | Sets the session ID. |
`public` | `mixed` | `getName()` | Returns the session name. |
`public` | `` | `setName(string $name)` | Sets the session name. |
`public` | `MetadataBag` | `getMetadataBag()` | Gets session meta. |
`public` | `` | `registerBag(SessionBagInterface $bag)` | Registers a SessionBagInterface with the session. |
`public` | `SessionBagInterface` | `getBag(string $name)` | Gets a bag instance by name. |
`public` | `FlashBagInterface` | `getFlashBag()` | Gets the flashbag interface. |

## Details

at line 39

### `__construct()`

`public __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)`

#### Parameters

`SessionStorageInterface` | `$storage` | A SessionStorageInterface instance
`AttributeBagInterface` | `$attributes` | An AttributeBagInterface instance, (defaults null for default AttributeBag)
`FlashBagInterface` | `$flashes` | A FlashBagInterface instance (defaults null for default FlashBag)

at line 55

### `start()`

`public bool start()`

Starts the session storage.

#### Return Value

`bool` | True if session started

#### Exceptions

RuntimeException | if session fails to start

at line 63

### `has()`

`public bool has(string $name)`

Checks if an attribute is defined.

#### Parameters

`string` | `$name` | The attribute name

#### Return Value

`bool` | true if the attribute is defined, false otherwise

at line 71

### `get()`

`public mixed get(string $name, mixed $default = null)`

Returns an attribute.

#### Parameters

`string` | `$name` | The attribute name
`mixed` | `$default` | The default value if not found

#### Return Value

`mixed` |

at line 79

### `set()`

`public set(string $name, mixed $value)`

Sets an attribute.

#### Parameters

`string` | `$name` |
`mixed` | `$value` |

at line 87

### `all()`

`public array all()`

Returns attributes.

#### Return Value

`array` | Attributes

at line 95

### `replace()`

`public replace(array $attributes)`

Sets attributes.

#### Parameters

`array` | `$attributes` | Attributes

at line 103

### `remove()`

`public mixed remove(string $name)`

Removes an attribute.

#### Parameters

`string` | `$name` |

#### Return Value

`mixed` | The removed value or null when it does not exist

at line 111

### `clear()`

`public clear()`

Clears all attributes.

at line 119

### `isStarted()`

`public bool isStarted()`

Checks if the session was started.

#### Return Value

`bool` |

at line 129

### `getIterator()`

`public ArrayIterator getIterator()`

Returns an iterator for attributes.

#### Return Value

`ArrayIterator` | An \ArrayIterator instance

at line 139

### `count()`

`public int count()`

Returns the number of attributes.

#### Return Value

`int` | The number of attributes

at line 149

### `hasBeenStarted()`

`public bool hasBeenStarted()`

#### Return Value

`bool` |

at line 159

### `isEmpty()`

`public bool isEmpty()`

#### Return Value

`bool` |

at line 173

### `invalidate()`

`public bool invalidate(int $lifetime = null)`

Invalidates the current session.

Clears all session attributes and flashes and regenerates the session and deletes the old session from persistence.

#### Parameters

`int` | `$lifetime` | Sets the cookie lifetime for the session cookie. A null value will leave the system settings unchanged, 0 sets the cookie to expire with browser session. Time is in seconds, and is not a Unix timestamp.

#### Return Value

`bool` | True if session invalidated, false if error

at line 183

### `migrate()`

`public bool migrate(bool $destroy = false, int $lifetime = null)`

Migrates the current session to a new session id while maintaining all session attributes.

#### Parameters

`bool` | `$destroy` | Whether to delete the old session or leave it to garbage collection
`int` | `$lifetime` | Sets the cookie lifetime for the session cookie. A null value will leave the system settings unchanged, 0 sets the cookie to expire with browser session. Time is in seconds, and is not a Unix timestamp.

#### Return Value

`bool` | True if session migrated, false if error

at line 191

### `save()`

`public save()`

Force the session to be saved and closed.

This method is generally not required for real sessions as the session will be automatically saved at the end of code execution.

at line 199

### `getId()`

`public string getId()`

Returns the session ID.

#### Return Value

`string` | The session ID

at line 207

### `setId()`

`public setId(string $id)`

Sets the session ID.

#### Parameters

`string` | `$id` |

at line 215

### `getName()`

`public mixed getName()`

Returns the session name.

#### Return Value

`mixed` | The session name

at line 223

### `setName()`

`public setName(string $name)`

Sets the session name.

#### Parameters

`string` | `$name` |

at line 231

### `getMetadataBag()`

`public MetadataBag getMetadataBag()`

Gets session meta.

#### Return Value

`MetadataBag` |

at line 239

### `registerBag()`

`public registerBag(SessionBagInterface $bag)`

Registers a SessionBagInterface with the session.

#### Parameters

`SessionBagInterface` | `$bag` |

at line 247

### `getBag()`

`public SessionBagInterface getBag(string $name)`

Gets a bag instance by name.

#### Parameters

`string` | `$name` |

#### Return Value

`SessionBagInterface` |

at line 257

### `getFlashBag()`

`public FlashBagInterface getFlashBag()`

Gets the flashbag interface.

#### Return Value

`FlashBagInterface` |

## Source code

    <?php

    /*
     * This file is part of the Symfony package.
     *
     * (c) Fabien Potencier <fabien@symfony.com>
     *
     * For the full copyright and license information, please view the LICENSE
     * file that was distributed with this source code.
     */

    namespace Symfony\Component\HttpFoundation\Session;

    use Symfony\Component\HttpFoundation\Session\Storage\SessionStorageInterface;
    use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBag;
    use Symfony\Component\HttpFoundation\Session\Attribute\AttributeBagInterface;
    use Symfony\Component\HttpFoundation\Session\Flash\FlashBag;
    use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
    use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

    /**
     * @author Fabien Potencier <fabien@symfony.com>
     * @author Drak <drak@zikula.org>
     */
    class Session implements SessionInterface, \IteratorAggregate, \Countable
    {
        protected $storage;

        private $flashName;
        private $attributeName;
        private $data = array();
        private $hasBeenStarted;

        /**
         * @param SessionStorageInterface $storage    A SessionStorageInterface instance
         * @param AttributeBagInterface   $attributes An AttributeBagInterface instance, (defaults null for default AttributeBag)
         * @param FlashBagInterface       $flashes    A FlashBagInterface instance (defaults null for default FlashBag)
         */
        public function __construct(SessionStorageInterface $storage = null, AttributeBagInterface $attributes = null, FlashBagInterface $flashes = null)
        {
            $this->storage = $storage ?: new NativeSessionStorage();

            $attributes = $attributes ?: new AttributeBag();
            $this->attributeName = $attributes->getName();
            $this->registerBag($attributes);

            $flashes = $flashes ?: new FlashBag();
            $this->flashName = $flashes->getName();
            $this->registerBag($flashes);
        }

        /**
         * {@inheritdoc}
         */
        public function start()
        {
            return $this->storage->start();
        }

        /**
         * {@inheritdoc}
         */
        public function has($name)
        {
            return $this->getAttributeBag()->has($name);
        }

        /**
         * {@inheritdoc}
         */
        public function get($name, $default = null)
        {
            return $this->getAttributeBag()->get($name, $default);
        }

        /**
         * {@inheritdoc}
         */
        public function set($name, $value)
        {
            $this->getAttributeBag()->set($name, $value);
        }

        /**
         * {@inheritdoc}
         */
        public function all()
        {
            return $this->getAttributeBag()->all();
        }

        /**
         * {@inheritdoc}
         */
        public function replace(array $attributes)
        {
            $this->getAttributeBag()->replace($attributes);
        }

        /**
         * {@inheritdoc}
         */
        public function remove($name)
        {
            return $this->getAttributeBag()->remove($name);
        }

        /**
         * {@inheritdoc}
         */
        public function clear()
        {
            $this->getAttributeBag()->clear();
        }

        /**
         * {@inheritdoc}
         */
        public function isStarted()
        {
            return $this->storage->isStarted();
        }

        /**
         * Returns an iterator for attributes.
         *
         * @return \ArrayIterator An \ArrayIterator instance
         */
        public function getIterator()
        {
            return new \ArrayIterator($this->getAttributeBag()->all());
        }

        /**
         * Returns the number of attributes.
         *
         * @return int The number of attributes
         */
        public function count()
        {
            return count($this->getAttributeBag()->all());
        }

        /**
         * @return bool
         *
         * @internal
         */
        public function hasBeenStarted()
        {
            return $this->hasBeenStarted;
        }

        /**
         * @return bool
         *
         * @internal
         */
        public function isEmpty()
        {
            foreach ($this->data as &$data) {
                if (!empty($data)) {
                    return false;
                }
            }

            return true;
        }

        /**
         * {@inheritdoc}
         */
        public function invalidate($lifetime = null)
        {
            $this->storage->clear();

            return $this->migrate(true, $lifetime);
        }

        /**
         * {@inheritdoc}
         */
        public function migrate($destroy = false, $lifetime = null)
        {
            return $this->storage->regenerate($destroy, $lifetime);
        }

        /**
         * {@inheritdoc}
         */
        public function save()
        {
            $this->storage->save();
        }

        /**
         * {@inheritdoc}
         */
        public function getId()
        {
            return $this->storage->getId();
        }

        /**
         * {@inheritdoc}
         */
        public function setId($id)
        {
            $this->storage->setId($id);
        }

        /**
         * {@inheritdoc}
         */
        public function getName()
        {
            return $this->storage->getName();
        }

        /**
         * {@inheritdoc}
         */
        public function setName($name)
        {
            $this->storage->setName($name);
        }

        /**
         * {@inheritdoc}
         */
        public function getMetadataBag()
        {
            return $this->storage->getMetadataBag();
        }

        /**
         * {@inheritdoc}
         */
        public function registerBag(SessionBagInterface $bag)
        {
            $this->storage->registerBag(new SessionBagProxy($bag, $this->data, $this->hasBeenStarted));
        }

        /**
         * {@inheritdoc}
         */
        public function getBag($name)
        {
            return $this->storage->getBag($name)->getBag();
        }

        /**
         * Gets the flashbag interface.
         *
         * @return FlashBagInterface
         */
        public function getFlashBag()
        {
            return $this->getBag($this->flashName);
        }

        /**
         * Gets the attributebag interface.
         *
         * Note that this method was added to help with IDE autocompletion.
         *
         * @return AttributeBagInterface
         */
        private function getAttributeBag()
        {
            return $this->getBag($this->attributeName);
        }
    }