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

  • NHttpContext
  • NHttpRequest
  • NHttpRequestFactory
  • NHttpResponse
  • NHttpUploadedFile
  • NSession
  • NSessionSection
  • NUrl
  • NUrlScript
  • NUserStorage

Interfaces

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