Skip to content

Sessions#122

Merged
thekid merged 11 commits intomasterfrom
feature/sessions
Apr 13, 2025
Merged

Sessions#122
thekid merged 11 commits intomasterfrom
feature/sessions

Conversation

@thekid
Copy link
Copy Markdown
Member

@thekid thekid commented Apr 13, 2025

This pull request adds a sessioning implementation consisting of:

  • An abstract base class web.Session for libraries like https://github.com/xp-forge/sessions to implement
  • The mutator attach(Session $session): self on the web.Request class
  • The accessor session(): ?Session on the web.Request class

Example

This application always uses sessions so to have an omnipresent cart:

use web\Application;
use web\session\{UseSessions, InFileSystem};

class Shop extends Application {
  public function routes() {
    $this->install(new UseSessions(new InFileSystem()));

    return function($req, $res) {
      $session= $req->session();
      if (empty($cart= $session->value('cart'))) {
        $cart= [['name' => 'Welcome present', 'price' => 0]];
        $session->register('cart', $cart);
      }

      $res->send('You have '.sizeof($cart).' item(s) in your cart', 'text/plain');
    };
  }
}

API

// @test web.unittest.SessionTest
public abstract class web.Session {

  // Returns session ID
  public abstract function id(): string

  // Returns session expiration time
  public abstract function expires(): int

  // Registers a value by a given a name in this session
  public abstract function register(string $name, var $value): void

  // Returns a previously registered value by its given name
  public abstract function value(string $name, var $default): var

  // Removes a previously registered value by its given name
  public abstract function remove(string $name): void

  // Destroys this session
  public abstract function destroy(): void
}

@thekid thekid merged commit 6d21703 into master Apr 13, 2025
20 checks passed
@thekid
Copy link
Copy Markdown
Member Author

thekid commented Apr 13, 2025

@thekid thekid deleted the feature/sessions branch April 13, 2025 17:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant