Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Reflection
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

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