Example #1
0
 /**
  * Writes item into the cache.
  * Dependencies are:
  * - Cache::PRIORITY => (int) priority
  * - Cache::EXPIRE => (timestamp) expiration
  * - Cache::SLIDING => (bool) use sliding expiration?
  * - Cache::TAGS => (array) tags
  * - Cache::FILES => (array|string) file names
  * - Cache::ITEMS => (array|string) cache items
  * - Cache::CONSTS => (array|string) cache items
  *
  * @param  string key
  * @param  mixed  value
  * @param  array  dependencies
  * @return mixed  value itself
  * @throws InvalidArgumentException
  */
 public function save($key, $data, array $dp = NULL)
 {
     if (!is_string($key) && !is_int($key)) {
         throw new InvalidArgumentException("Cache key name must be string or integer, " . gettype($key) . " given.");
     }
     $this->key = (string) $key;
     $key = $this->namespace . self::NAMESPACE_SEPARATOR . $key;
     // convert expire into relative amount of seconds
     if (!empty($dp[Cache::EXPIRE])) {
         $dp[Cache::EXPIRE] = Tools::createDateTime($dp[Cache::EXPIRE])->format('U') - time();
     }
     // convert FILES into CALLBACKS
     if (isset($dp[self::FILES])) {
         //clearstatcache();
         foreach ((array) $dp[self::FILES] as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkFile'), $item, @filemtime($item));
             // intentionally @
         }
         unset($dp[self::FILES]);
     }
     // add namespaces to items
     if (isset($dp[self::ITEMS])) {
         $dp[self::ITEMS] = (array) $dp[self::ITEMS];
         foreach ($dp[self::ITEMS] as $k => $item) {
             $dp[self::ITEMS][$k] = $this->namespace . self::NAMESPACE_SEPARATOR . $item;
         }
     }
     // convert CONSTS into CALLBACKS
     if (isset($dp[self::CONSTS])) {
         foreach ((array) $dp[self::CONSTS] as $item) {
             $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkConst'), $item, constant($item));
         }
         unset($dp[self::CONSTS]);
     }
     if ($data instanceof Callback || $data instanceof Closure) {
         Environment::enterCriticalSection('Nette\\Caching/' . $key);
         $data = $data->__invoke();
         Environment::leaveCriticalSection('Nette\\Caching/' . $key);
     }
     if (is_object($data)) {
         $dp[self::CALLBACKS][] = array(array(__CLASS__, 'checkSerializationVersion'), get_class($data), ClassReflection::from($data)->getAnnotation('serializationVersion'));
     }
     $this->data = $data;
     if ($data === NULL) {
         $this->storage->remove($key);
     } else {
         $this->storage->write($key, $data, (array) $dp);
     }
     return $data;
 }
Example #2
0
 /**
  * Sets the amount of time allowed between requests before the session will be terminated.
  * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
  * @return Session  provides a fluent interface
  */
 public function setExpiration($time)
 {
     if (empty($time)) {
         return $this->setOptions(array('gc_maxlifetime' => self::DEFAULT_FILE_LIFETIME, 'cookie_lifetime' => 0));
     } else {
         $time = Tools::createDateTime($time)->format('U');
         return $this->setOptions(array('gc_maxlifetime' => $time, 'cookie_lifetime' => $time));
     }
 }
Example #3
0
 /**
  * Sends a cookie.
  * @param  string name of the cookie
  * @param  string value
  * @param  string|int|DateTime  expiration time, value 0 means "until the browser is closed"
  * @param  string
  * @param  string
  * @param  bool
  * @return HttpResponse  provides a fluent interface
  * @throws InvalidStateException  if HTTP headers have been sent
  */
 public function setCookie($name, $value, $time, $path = NULL, $domain = NULL, $secure = NULL)
 {
     if (headers_sent($file, $line)) {
         throw new InvalidStateException("Cannot set cookie after HTTP headers have been sent" . ($file ? " (output started at {$file}:{$line})." : "."));
     }
     setcookie($name, $value, $time ? Tools::createDateTime($time)->format('U') : 0, $path === NULL ? $this->cookiePath : (string) $path, $domain === NULL ? $this->cookieDomain : (string) $domain, $secure === NULL ? $this->cookieSecure : (bool) $secure, TRUE);
     return $this;
 }
 /**
  * Sets the expiration of the namespace or specific variables.
  * @param  string|int|DateTime  time, value 0 means "until the browser is closed"
  * @param  mixed   optional list of variables / single variable to expire
  * @return SessionNamespace  provides a fluent interface
  */
 public function setExpiration($time, $variables = NULL)
 {
     if (empty($time)) {
         $time = NULL;
         $whenBrowserIsClosed = TRUE;
     } else {
         $time = Tools::createDateTime($time)->format('U');
         $whenBrowserIsClosed = FALSE;
     }
     if ($variables === NULL) {
         // to entire namespace
         $this->meta['']['T'] = $time;
         $this->meta['']['B'] = $whenBrowserIsClosed;
     } elseif (is_array($variables)) {
         // to variables
         foreach ($variables as $variable) {
             $this->meta[$variable]['T'] = $time;
             $this->meta[$variable]['B'] = $whenBrowserIsClosed;
         }
     } else {
         // to variable
         $this->meta[$variables]['T'] = $time;
         $this->meta[$variables]['B'] = $whenBrowserIsClosed;
     }
     return $this;
 }
Example #5
0
 /**
  * Enables log out after inactivity.
  * @param  string|int|DateTime number of seconds or timestamp
  * @param  bool  log out when the browser is closed?
  * @param  bool  clear the identity from persistent storage?
  * @return User  provides a fluent interface
  */
 public function setExpiration($time, $whenBrowserIsClosed = TRUE, $clearIdentity = FALSE)
 {
     $session = $this->getSessionNamespace(TRUE);
     if ($time) {
         $time = Tools::createDateTime($time)->format('U');
         $session->expireTime = $time;
         $session->expireDelta = $time - time();
     } else {
         unset($session->expireTime, $session->expireDelta);
     }
     $session->expireIdentity = (bool) $clearIdentity;
     $session->expireBrowser = (bool) $whenBrowserIsClosed;
     $session->browserCheck = TRUE;
     $session->setExpiration(0, 'browserCheck');
     return $this;
 }
Example #6
0
 /**
  * Convert date to RFC822
  * @param string|date $date
  * @return string
  */
 public static function prepareDate($date)
 {
     $timestamp = Tools::createDateTime($date)->getTimestamp();
     return gmdate('D, d M Y H:i:s', $timestamp) . " GMT";
 }
Example #7
0
    /**
     * Restricts the search by modified time.
     * @param  string  "[operator] [date]" example: >1978-01-23
     * @param  mixed
     * @return Finder  provides a fluent interface
     */
    public function date($operator, $date = NULL)
    {
        if (func_num_args() === 1) {
            // in $operator is predicate
            if (!preg_match('#^(?:([=<>!]=?|<>)\\s*)?(.+)$#i', $operator, $matches)) {
                throw new InvalidArgumentException('Invalid date predicate format.');
            }
            list(, $operator, $date) = $matches;
            $operator = $operator ? $operator : '=';
        }
        $date = Tools::createDateTime($date)->format('U');
        return $this->filter(create_function('$file', 'extract(NClosureFix::$vars[' . NClosureFix::uses(array('operator' => $operator, 'date' => $date)) . '], EXTR_REFS); 
			return Tools::compare($file->getMTime(), $operator, $date);
		'));
    }
 /**
  * Date/time formatting.
  * @param  string|int|DateTime
  * @param  string
  * @return string
  */
 public static function date($time, $format = "%x")
 {
     if ($time == NULL) {
         // intentionally ==
         return NULL;
     }
     $time = Tools::createDateTime($time);
     return strpos($format, '%') === FALSE ? $time->format($format) : strftime($format, $time->format('U'));
     // formats according to locales
 }
Example #9
0
 /**
  * Enables log out after inactivity.
  * @param  string|int|DateTime number of seconds or timestamp
  * @param  bool  clear the identity from persistent storage?
  * @return \Stupid\User  provides a fluent interface
  */
 public function setExpiration($time, $clearIdentity = false)
 {
     $session = $this->getUserNamespace(true);
     if ($time) {
         $time = Tools::createDateTime($time)->format('U');
         $session->expireTime = $time;
         $session->expireDelta = $time - time();
     } else {
         unset($session->expireTime, $session->expireDelta);
     }
     $session->expireIdentity = (bool) $clearIdentity;
     return $this;
 }