Пример #1
0
 public function testOffsetUnset()
 {
     $bag = new OptionsBag(['antistatic' => 'polyethylene terephthalate']);
     $this->assertTrue($bag->has('antistatic'));
     $bag->offsetUnset('antistatic');
     $this->assertFalse($bag->has('antistatic'));
 }
Пример #2
0
 protected function generateCookie()
 {
     $lifetime = $this->options->getInt('cookie_lifetime');
     if ($lifetime !== 0) {
         $lifetime += time();
     }
     return new Cookie($this->session->getName(), $this->session->getId(), $lifetime, $this->options['cookie_path'], $this->options['cookie_domain'] ?: null, $this->options->getBoolean('cookie_secure'), $this->options->getBoolean('cookie_httponly'));
 }
 /**
  * Set the cookie_path from the current request's path
  *
  * @param GetResponseEvent $event
  */
 public function onRequest(GetResponseEvent $event)
 {
     if (!$event->isMasterRequest()) {
         return;
     }
     $request = $event->getRequest();
     $path = $request->getBaseUrl() . $request->getPathInfo();
     $this->options->set('cookie_path', $path);
 }
Пример #4
0
 protected function write()
 {
     $data = $this->serializer->serialize($this->data);
     if ($this->options->getBoolean('lazy_write', false) && $this->handler instanceof LazyWriteHandlerInterface && md5($data) === $this->dataHash) {
         $this->handler->updateTimestamp($this->id, $data);
     } else {
         $this->handler->write($this->id, $data);
     }
 }
Пример #5
0
 protected function collectGarbage()
 {
     $probability = $this->options->getInt('gc_probability');
     if ($probability < 0) {
         return;
     }
     $divisor = $this->options->getInt('gc_divisor');
     $rand = mt_rand(0, $divisor);
     if ($rand < $probability) {
         $this->handler->gc($this->options->getInt('gc_maxlifetime'));
     }
 }
Пример #6
0
 protected function registerOptions(Application $app)
 {
     $app['session.options'] = [];
     $app['session.options.import_from_ini'] = true;
     $app['session.options_bag'] = $app->share(function () use($app) {
         /*
          * This does two things.
          * 1) Merges options together. Precedence is as follows:
          *    - Options from session.options
          *    - Options from ini (if enabled with "session.options.import_from_ini")
          *    - Options hardcoded below
          * 2) Converts options to an OptionsBag instance
          */
         $defaults = ['save_handler' => 'files', 'save_path' => '/tmp', 'name' => 'PHPSESSID', 'lazy_write' => true, 'gc_probability' => 1, 'gc_divisor' => 1000, 'gc_maxlifetime' => 1440, 'cookie_lifetime' => 0, 'cookie_path' => '/', 'cookie_domain' => null, 'cookie_secure' => false, 'cookie_httponly' => false, 'restrict_realm' => false];
         $options = new OptionsBag($defaults);
         if ($app['session.options.import_from_ini']) {
             foreach ($options as $key => $value) {
                 $options[$key] = ini_get('session.' . $key);
             }
         }
         // @deprecated backwards compatibility:
         if (isset($app['session.storage.options'])) {
             $options->add($app['session.storage.options']);
         }
         // PHP's native C code accesses filesystem with different permissions than userland code.
         // If php.ini is using the default (files) handler, use ours instead to prevent this problem.
         if ($options->get('save_handler') === 'files') {
             $options->set('save_handler', 'filesystem');
             $options->set('save_path', 'cache://.sessions');
         }
         $options->add($app['session.options']);
         return $options;
     });
 }
Пример #7
0
 protected function registerOptions(Application $app)
 {
     $app['session.options'] = [];
     $app['session.options.import_from_ini'] = true;
     $app['session.options_bag'] = $app->share(function () use($app) {
         /*
          * This does two things.
          * 1) Merges options together. Precedence is as follows:
          *    - Options from session.options
          *    - Options from ini (if enabled with "session.options.import_from_ini")
          *    - Options hardcoded below
          * 2) Converts options to an OptionsBag instance
          */
         $defaults = ['save_handler' => 'files', 'save_path' => '/tmp', 'name' => 'PHPSESSID', 'lazy_write' => true, 'gc_probability' => 1, 'gc_divisor' => 1000, 'gc_maxlifetime' => 1440, 'cookie_lifetime' => 0, 'cookie_path' => '/', 'cookie_domain' => null, 'cookie_secure' => false, 'cookie_httponly' => false, 'restrict_realm' => false];
         $options = new OptionsBag($defaults);
         if ($app['session.options.import_from_ini']) {
             foreach ($options as $key => $value) {
                 $options[$key] = ini_get('session.' . $key);
             }
         }
         if (isset($app['session.storage.options'])) {
             $options->add($app['session.storage.options']);
         }
         $options->add($app['session.options']);
         return $options;
     });
 }