/**
  * Registers services on the given app.
  *
  * This method should only be used to configure services and parameters.
  * It should not get services.
  *
  * @param Application $app
  */
 public function register(Application $app)
 {
     $app['zend.session.handler._factory'] = $app->protect(function ($type, $options) use($app) {
         switch ($type) {
             case 'SilexCMF\\ZendSession\\SaveHandler\\Db':
                 return new Db($app[$options['connection']], new DbOptions());
             case 'Zend\\Session\\SaveHandler\\MongoDB':
                 return new MongoDB($app['mongo'], new MongoDBOptions($options));
         }
         return null;
     });
     $app['zend.session.manager'] = $app->share(function () use($app) {
         $options = isset($app['zend.session.options']) ? $app['zend.session.options'] : [];
         $config = null;
         if (isset($options['config'])) {
             $config = new SessionConfig();
             $config->setOptions($options['config']);
         }
         $storage = null;
         if (isset($options['storage'])) {
             $storage = Factory::factory($options['storage']['type'], isset($options['storage']['options']) ? $options['storage']['options'] : []);
         }
         $handler = null;
         if (isset($options['handler'])) {
             $handler = $app['zend.session.handler._factory']($options['handler']['type'], isset($options['handler']['options']) ? $options['handler']['options'] : []);
         }
         $manager = new SessionManager($config, $storage, $handler);
         if (isset($options['validators'])) {
             /* @var $request \Symfony\Component\HttpFoundation\Request */
             $request = $app['request_stack']->getCurrentRequest();
             foreach ($options['validators'] as $validator) {
                 switch ($validator) {
                     case 'Zend\\Session\\Validator\\HttpUserAgent':
                         $validator = new $validator($request->server->get('HTTP_USER_AGENT'));
                         break;
                     case 'Zend\\Session\\Validator\\RemoteAddr':
                         $validator = new $validator($request->server->get('REMOTE_ADDR'));
                         break;
                     default:
                         $validator = new $validator();
                 }
                 $manager->getValidatorChain()->attach('session.validate', array($validator, 'isValid'));
             }
         }
         Container::setDefaultManager($manager);
         return $manager;
     });
 }
Example #2
0
 /**
  * Create session storage object
  *
  * Uses "session_storage" section of configuration to seed a StorageInterface
  * instance. That array should contain the key "type", specifying the storage
  * type to use, and optionally "options", containing any options to be used in
  * creating the StorageInterface instance.
  *
  * @param  ServiceLocatorInterface    $services
  * @return StorageInterface
  * @throws ServiceNotCreatedException if session_storage is missing, or the
  *         factory cannot create the storage instance.
  */
 public function createService(ServiceLocatorInterface $services)
 {
     $config = $services->get('Config');
     if (!isset($config['session_storage']) || !is_array($config['session_storage'])) {
         throw new ServiceNotCreatedException('Configuration is missing a "session_storage" key, or the value of that key is not an array');
     }
     $config = $config['session_storage'];
     if (!isset($config['type'])) {
         throw new ServiceNotCreatedException('"session_storage" configuration is missing a "type" key');
     }
     $type = $config['type'];
     $options = isset($config['options']) ? $config['options'] : array();
     try {
         $storage = Factory::factory($type, $options);
     } catch (SessionException $e) {
         throw new ServiceNotCreatedException(sprintf('Factory is unable to create StorageInterface instance: %s', $e->getMessage()), $e->getCode(), $e);
     }
     return $storage;
 }