Packages

  • 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

  • HttpContext
  • HttpRequest
  • HttpRequestFactory
  • HttpResponse
  • HttpUploadedFile
  • Session
  • SessionSection
  • Url
  • UrlScript
  • UserStorage

Interfaces

  • IHttpRequest
  • IHttpResponse
  • ISessionStorage
  • Overview
  • Package
  • Class
  • Tree
  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:  * @package Nette\Http
 11:  */
 12: 
 13: 
 14: 
 15: /**
 16:  * Session section.
 17:  *
 18:  * @author     David Grudl
 19:  * @package Nette\Http
 20:  */
 21: final class SessionSection extends Object implements IteratorAggregate, ArrayAccess
 22: {
 23:     /** @var Session */
 24:     private $session;
 25: 
 26:     /** @var string */
 27:     private $name;
 28: 
 29:     /** @var array  session data storage */
 30:     private $data;
 31: 
 32:     /** @var array  session metadata storage */
 33:     private $meta = FALSE;
 34: 
 35:     /** @var bool */
 36:     public $warnOnUndefined = FALSE;
 37: 
 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 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:     /**
 56:      * Do not call directly. Use Session::getNamespace().
 57:      */
 58:     private function start()
 59:     {
 60:         if ($this->meta === FALSE) {
 61:             $this->session->start();
 62:             $this->data = & $_SESSION['__NF']['DATA'][$this->name];
 63:             $this->meta = & $_SESSION['__NF']['META'][$this->name];
 64:         }
 65:     }
 66: 
 67: 
 68: 
 69:     /**
 70:      * Returns an iterator over all section variables.
 71:      * @return ArrayIterator
 72:      */
 73:     public function getIterator()
 74:     {
 75:         $this->start();
 76:         if (isset($this->data)) {
 77:             return new ArrayIterator($this->data);
 78:         } else {
 79:             return new ArrayIterator;
 80:         }
 81:     }
 82: 
 83: 
 84: 
 85:     /**
 86:      * Sets a variable in this session section.
 87:      * @param  string  name
 88:      * @param  mixed   value
 89:      * @return void
 90:      */
 91:     public function __set($name, $value)
 92:     {
 93:         $this->start();
 94:         $this->data[$name] = $value;
 95:         if (is_object($value)) {
 96:             $this->meta[$name]['V'] = ClassReflection::from($value)->getAnnotation('serializationVersion');
 97:         }
 98:     }
 99: 
100: 
101: 
102:     /**
103:      * Gets a variable from this session section.
104:      * @param  string    name
105:      * @return mixed
106:      */
107:     public function &__get($name)
108:     {
109:         $this->start();
110:         if ($this->warnOnUndefined && !array_key_exists($name, $this->data)) {
111:             trigger_error("The variable '$name' does not exist in session section", E_USER_NOTICE);
112:         }
113: 
114:         return $this->data[$name];
115:     }
116: 
117: 
118: 
119:     /**
120:      * Determines whether a variable in this session section is set.
121:      * @param  string    name
122:      * @return bool
123:      */
124:     public function __isset($name)
125:     {
126:         if ($this->session->exists()) {
127:             $this->start();
128:         }
129:         return isset($this->data[$name]);
130:     }
131: 
132: 
133: 
134:     /**
135:      * Unsets a variable in this session section.
136:      * @param  string    name
137:      * @return void
138:      */
139:     public function __unset($name)
140:     {
141:         $this->start();
142:         unset($this->data[$name], $this->meta[$name]);
143:     }
144: 
145: 
146: 
147:     /**
148:      * Sets a variable in this session section.
149:      * @param  string  name
150:      * @param  mixed   value
151:      * @return void
152:      */
153:     public function offsetSet($name, $value)
154:     {
155:         $this->__set($name, $value);
156:     }
157: 
158: 
159: 
160:     /**
161:      * Gets a variable from this session section.
162:      * @param  string    name
163:      * @return mixed
164:      */
165:     public function offsetGet($name)
166:     {
167:         return $this->__get($name);
168:     }
169: 
170: 
171: 
172:     /**
173:      * Determines whether a variable in this session section is set.
174:      * @param  string    name
175:      * @return bool
176:      */
177:     public function offsetExists($name)
178:     {
179:         return $this->__isset($name);
180:     }
181: 
182: 
183: 
184:     /**
185:      * Unsets a variable in this session section.
186:      * @param  string    name
187:      * @return void
188:      */
189:     public function offsetUnset($name)
190:     {
191:         $this->__unset($name);
192:     }
193: 
194: 
195: 
196:     /**
197:      * Sets the expiration of the section or specific variables.
198:      * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
199:      * @param  mixed   optional list of variables / single variable to expire
200:      * @return SessionSection  provides a fluent interface
201:      */
202:     public function setExpiration($time, $variables = NULL)
203:     {
204:         $this->start();
205:         if (empty($time)) {
206:             $time = NULL;
207:             $whenBrowserIsClosed = TRUE;
208:         } else {
209:             $time = DateTime53::from($time)->format('U');
210:             $max = ini_get('session.gc_maxlifetime');
211:             if ($time - time() > $max + 3) { // bulgarian constant
212:                 trigger_error("The expiration time is greater than the session expiration $max seconds", E_USER_NOTICE);
213:             }
214:             $whenBrowserIsClosed = FALSE;
215:         }
216: 
217:         if ($variables === NULL) { // to entire section
218:             $this->meta['']['T'] = $time;
219:             $this->meta['']['B'] = $whenBrowserIsClosed;
220: 
221:         } elseif (is_array($variables)) { // to variables
222:             foreach ($variables as $variable) {
223:                 $this->meta[$variable]['T'] = $time;
224:                 $this->meta[$variable]['B'] = $whenBrowserIsClosed;
225:             }
226: 
227:         } else { // to variable
228:             $this->meta[$variables]['T'] = $time;
229:             $this->meta[$variables]['B'] = $whenBrowserIsClosed;
230:         }
231:         return $this;
232:     }
233: 
234: 
235: 
236:     /**
237:      * Removes the expiration from the section or specific variables.
238:      * @param  mixed   optional list of variables / single variable to expire
239:      * @return void
240:      */
241:     public function removeExpiration($variables = NULL)
242:     {
243:         $this->start();
244:         if ($variables === NULL) {
245:             // from entire section
246:             unset($this->meta['']['T'], $this->meta['']['B']);
247: 
248:         } elseif (is_array($variables)) {
249:             // from variables
250:             foreach ($variables as $variable) {
251:                 unset($this->meta[$variable]['T'], $this->meta[$variable]['B']);
252:             }
253:         } else {
254:             unset($this->meta[$variables]['T'], $this->meta[$variable]['B']);
255:         }
256:     }
257: 
258: 
259: 
260:     /**
261:      * Cancels the current session section.
262:      * @return void
263:      */
264:     public function remove()
265:     {
266:         $this->start();
267:         $this->data = NULL;
268:         $this->meta = NULL;
269:     }
270: 
271: }
272: 
Nette Framework 2.0.5 (for PHP 5.2, un-prefixed) API API documentation generated by ApiGen 2.7.0