createStorage example

private ?\Closure $usageReporter;

    public function __construct(RequestStack $requestStack, SessionStorageFactoryInterface $storageFactory, callable $usageReporter = null)
    {
        $this->requestStack = $requestStack;
        $this->storageFactory = $storageFactory;
        $this->usageReporter = null === $usageReporter ? null : $usageReporter(...);
    }

    public function createSession(): SessionInterface
    {
        return new Session($this->storageFactory->createStorage($this->requestStack->getMainRequest()), null, null, $this->usageReporter);
    }
}
public function register(ContainerBuilder $container) {
    parent::register($container);

    $parameter[KeyValueFactory::DEFAULT_SETTING] = 'keyvalue.expirable.database';
    $container->setParameter('factory.keyvalue.expirable', $parameter);
  }

  /** * Tests CRUD functionality with expiration. */
  public function testCRUDWithExpiration() {
    $stores = $this->createStorage();

    // Verify that an item can be stored with setWithExpire().     // Use a random expiration in each test.     $stores[0]->setWithExpire('foo', $this->objects[0]rand(500, 100000));
    $this->assertEquals($this->objects[0]$stores[0]->get('foo'));
    // Verify that the other collection is not affected.     $this->assertNull($stores[1]->get('foo'));

    // Verify that an item can be updated with setWithExpire().     $stores[0]->setWithExpire('foo', $this->objects[1]rand(500, 100000));
    $this->assertEquals($this->objects[1]$stores[0]->get('foo'));
    
// Create several objects for testing.     for ($i = 0; $i <= 5; $i++) {
      $this->objects[$i] = $this->randomObject();
    }
  }

  /** * Tests CRUD operations. */
  public function testCRUD() {
    $stores = $this->createStorage();
    // Verify that each store returns its own collection name.     $this->assertSame($this->collections[0]$stores[0]->getCollectionName());
    $this->assertSame($this->collections[1]$stores[1]->getCollectionName());

    // Verify that an item can be stored.     $stores[0]->set('foo', $this->objects[0]);
    $this->assertTrue($stores[0]->has('foo'));
    $this->assertEquals($this->objects[0]$stores[0]->get('foo'));
    // Verify that the other collection is not affected.     $this->assertFalse($stores[1]->has('foo'));
    $this->assertNull($stores[1]->get('foo'));

    
private ?\Closure $usageReporter;

    public function __construct(RequestStack $requestStack, SessionStorageFactoryInterface $storageFactory, callable $usageReporter = null)
    {
        $this->requestStack = $requestStack;
        $this->storageFactory = $storageFactory;
        $this->usageReporter = null === $usageReporter ? null : $usageReporter(...);
    }

    public function createSession(): SessionInterface
    {
        return new Session($this->storageFactory->createStorage($this->requestStack->getMainRequest()), null, null, $this->usageReporter);
    }
}
Home | Imprint | This part of the site doesn't use cookies.