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

Classes

Interfaces

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