Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Context
  • FileUpload
  • Request
  • RequestFactory
  • Response
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IRequest
  • IResponse
  • ISessionStorage
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (http://nette.org)
  5:  *
  6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
  7:  *
  8:  * For the full copyright and license information, please view
  9:  * the file license.txt that was distributed with this source code.
 10:  */
 11: 
 12: namespace Nette\Http;
 13: 
 14: use Nette;
 15: 
 16: 
 17: /**
 18:  * Session section.
 19:  *
 20:  * @author     David Grudl
 21:  */
 22: class SessionSection extends Nette\Object implements \IteratorAggregate, \ArrayAccess
 23: {
 24:     /** @var Session */
 25:     private $session;
 26: 
 27:     /** @var string */
 28:     private $name;
 29: 
 30:     /** @var array  session data storage */
 31:     private $data;
 32: 
 33:     /** @var array  session metadata storage */
 34:     private $meta = FALSE;
 35: 
 36:     /** @var bool */
 37:     public $warnOnUndefined = FALSE;
 38: 
 39: 
 40:     /**
 41:      * Do not call directly. Use Session::getSection().
 42:      */
 43:     public function __construct(Session $session, $name)
 44:     {
 45:         if (!is_string($name)) {
 46:             throw new Nette\InvalidArgumentException("Session namespace must be a string, " . gettype($name) ." given.");
 47:         }
 48: 
 49:         $this->session = $session;
 50:         $this->name = $name;
 51:     }
 52: 
 53: 
 54:     /**
 55:      * Do not call directly. Use Session::getNamespace().
 56:      */
 57:     private function start()
 58:     {
 59:         if ($this->meta === FALSE) {
 60:             $this->session->start();
 61:             $this->data = & $_SESSION['__NF']['DATA'][$this->name];
 62:             $this->meta = & $_SESSION['__NF']['META'][$this->name];
 63:         }
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Returns an iterator over all section variables.
 69:      * @return \ArrayIterator
 70:      */
 71:     public function getIterator()
 72:     {
 73:         $this->start();
 74:         if (isset($this->data)) {
 75:             return new \ArrayIterator($this->data);
 76:         } else {
 77:             return new \ArrayIterator;
 78:         }
 79:     }
 80: 
 81: 
 82:     /**
 83:      * Sets a variable in this session section.
 84:      * @param  string  name
 85:      * @param  mixed   value
 86:      * @return void
 87:      */
 88:     public function __set($name, $value)
 89:     {
 90:         $this->start();
 91:         $this->data[$name] = $value;
 92:         if (is_object($value)) {
 93:             $this->meta[$name]['V'] = Nette\Reflection\ClassType::from($value)->getAnnotation('serializationVersion');
 94:         }
 95:     }
 96: 
 97: 
 98:     /**
 99:      * Gets a variable from this session section.
100:      * @param  string    name
101:      * @return mixed
102:      */
103:     public function &__get($name)
104:     {
105:         $this->start();
106:         if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
107:             trigger_error("The variable '$name' does not exist in session section", E_USER_NOTICE);
108:         }
109: 
110:         return $this->data[$name];
111:     }
112: 
113: 
114:     /**
115:      * Determines whether a variable in this session section is set.
116:      * @param  string    name
117:      * @return bool
118:      */
119:     public function __isset($name)
120:     {
121:         if ($this->session->exists()) {
122:             $this->start();
123:         }
124:         return isset($this->data[$name]);
125:     }
126: 
127: 
128:     /**
129:      * Unsets a variable in this session section.
130:      * @param  string    name
131:      * @return void
132:      */
133:     public function __unset($name)
134:     {
135:         $this->start();
136:         unset($this->data[$name], $this->meta[$name]);
137:     }
138: 
139: 
140:     /**
141:      * Sets a variable in this session section.
142:      * @param  string  name
143:      * @param  mixed   value
144:      * @return void
145:      */
146:     public function offsetSet($name, $value)
147:     {
148:         $this->__set($name, $value);
149:     }
150: 
151: 
152:     /**
153:      * Gets a variable from this session section.
154:      * @param  string    name
155:      * @return mixed
156:      */
157:     public function offsetGet($name)
158:     {
159:         return $this->__get($name);
160:     }
161: 
162: 
163:     /**
164:      * Determines whether a variable in this session section is set.
165:      * @param  string    name
166:      * @return bool
167:      */
168:     public function offsetExists($name)
169:     {
170:         return $this->__isset($name);
171:     }
172: 
173: 
174:     /**
175:      * Unsets a variable in this session section.
176:      * @param  string    name
177:      * @return void
178:      */
179:     public function offsetUnset($name)
180:     {
181:         $this->__unset($name);
182:     }
183: 
184: 
185:     /**
186:      * Sets the expiration of the section or specific variables.
187:      * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
188:      * @param  mixed   optional list of variables / single variable to expire
189:      * @return self
190:      */
191:     public function setExpiration($time, $variables = NULL)
192:     {
193:         $this->start();
194:         if (empty($time)) {
195:             $time = NULL;
196:             $whenBrowserIsClosed = TRUE;
197:         } else {
198:             $time = Nette\DateTime::from($time)->format('U');
199:             $max = ini_get('session.gc_maxlifetime');
200:             if ($time - time() > $max + 3) { // bulgarian constant
201:                 trigger_error("The expiration time is greater than the session expiration $max seconds", E_USER_NOTICE);
202:             }
203:             $whenBrowserIsClosed = FALSE;
204:         }
205: 
206:         if ($variables === NULL) { // to entire section
207:             $this->meta['']['T'] = $time;
208:             $this->meta['']['B'] = $whenBrowserIsClosed;
209: 
210:         } elseif (is_array($variables)) { // to variables
211:             foreach ($variables as $variable) {
212:                 $this->meta[$variable]['T'] = $time;
213:                 $this->meta[$variable]['B'] = $whenBrowserIsClosed;
214:             }
215: 
216:         } else { // to variable
217:             $this->meta[$variables]['T'] = $time;
218:             $this->meta[$variables]['B'] = $whenBrowserIsClosed;
219:         }
220:         return $this;
221:     }
222: 
223: 
224:     /**
225:      * Removes the expiration from the section or specific variables.
226:      * @param  mixed   optional list of variables / single variable to expire
227:      * @return void
228:      */
229:     public function removeExpiration($variables = NULL)
230:     {
231:         $this->start();
232:         if ($variables === NULL) {
233:             // from entire section
234:             unset($this->meta['']['T'], $this->meta['']['B']);
235: 
236:         } elseif (is_array($variables)) {
237:             // from variables
238:             foreach ($variables as $variable) {
239:                 unset($this->meta[$variable]['T'], $this->meta[$variable]['B']);
240:             }
241:         } else {
242:             unset($this->meta[$variables]['T'], $this->meta[$variable]['B']);
243:         }
244:     }
245: 
246: 
247:     /**
248:      * Cancels the current session section.
249:      * @return void
250:      */
251:     public function remove()
252:     {
253:         $this->start();
254:         $this->data = NULL;
255:         $this->meta = NULL;
256:     }
257: 
258: }
259: 
Nette Framework 2.0.11 API API documentation generated by ApiGen 2.8.0