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

Classes

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

Interfaces

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