Skip to content

Commit e6e2d56

Browse files
committed
Document project
1 parent 97cf77b commit e6e2d56

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

README.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,83 @@
11
# PHP Sessionz [![Build Status][travis-image]][travis-url] [![Coverage Status][coveralls-image]][coveralls-url]
22

3+
Sessionz is a PHP library for smarter session management in modular applications.
4+
5+
## Quick Start
6+
7+
Use [Composer](https://getcomposer.org/) to add `ericmann/sessionz` to your project. Then, after loading all of your dependencies, initialize the core session manager and add the handlers you need to your stack.
8+
9+
```
10+
require __DIR__ . '/vendor/autoload.php';
11+
12+
EAMann\Sessionz\Manager::initialize()
13+
->addHandler( new \EAMann\Sessionz\Handlers\DefaultHandler() )
14+
->addHandler( new \EAMann\Sessionz\Handlers\EncryptionHandler( getenv('session_passkey') ) )
15+
->addHandler( new \EAMann\Sessionz\Handlers\MemoryHandler() )
16+
17+
session_start();
18+
19+
```
20+
21+
The above example adds, in order:
22+
23+
- The default PHP session handler (which uses files for storage)
24+
- An encryption middleware such that session data will be encrypted at rest on disk
25+
- An in-memory cache to avoid round-trips to the filesystem on read
26+
27+
## How Handlers Work
28+
29+
The session manager maintains a list of registered "handlers" to which it passes requests from the PHP engine to:
30+
31+
- Read a session
32+
- Write a session
33+
- Create a session store
34+
- Clean up (garbage collect) expired sessions
35+
- Delete a session
36+
37+
Each handler must implement the `Handler` interface so the session manager knows how to work with them.
38+
39+
### Middleware Structure
40+
41+
The overall structure of the handler stack is identical to that of a middleware stack in a modern PHP application. You can read more about the general philosophy [on Slim's website](https://www.slimframework.com/docs/concepts/middleware.html#how-does-middleware-work).
42+
43+
In general, stack operations will flow from the outside-in, starting by invoking the appropriate operation on the most recently registered handler and walking down the stack to the oldest handler. Each handler has the option to halt execution and return data immediately, or can invoke the passed `$next` callback to continue operation.
44+
45+
Using the quick start example above:
46+
47+
- Requests start in the `MemoryHandler`
48+
- If necessary, they then pass to the `EncryptionHandler`
49+
- Requests always pass from encryption to the `DefaultHandler`
50+
- If necessary, they then pass to the (hidden) `BaseHandler`
51+
- Then everything returns because the base handler doesn't pass anything on
52+
53+
## Available Handlers
54+
55+
### `DefaultHandler`
56+
57+
The default session handler merely exposes PHP's default session implementation to our custom manager. Including this handler will provide otherwise standard PHP session functionality to the project as a whole, but this functionality can be extended by placing other stacks on top.
58+
59+
### `EncryptionHandler`
60+
61+
Sessions stored on disk (the default implementation) or in a separate storage system (Memcache, MySQL, or similar) should be encrypted _at rest_. This handler will automatically encrypt any information passing through it on write and decrypt data on read. It does not store data on its own.
62+
63+
### `MemoryHandler`
64+
65+
If the final storage system presented to the session manager is remote, reads and writes can take a non-trivial amount of time. Storing session data in memory helps to make the application more performant. Reads will stop at this layer in the stack if the session is found (i.e. the cache is hot) but will flow to the next layer if no session exists. When a session is found in a subsequent layer, this handler will update its cache to make the data available upon the next lookup.
66+
67+
Writes will update the cache and pass through to the next layer in the stack.
68+
69+
### Abstract handlers
70+
71+
The `BaseHandler` class is always instantiated and included at the root of the handler stack by default. This is so that, no matter what handlers you add in to the stack, the session manager will always return a standard, reliable set of information.
72+
73+
The `NoopHandler` class is provided for you to build additional middleware atop a standard interface that "passes through" to the next layer in the stack by default. The `EncryptionHandler`, for example, inherits from this class as it doesn't store or read data, but merely manipulates information before passing it along. Another implementation might be a logging interface to track when sessions are accessed/updated.
74+
75+
## Credits
76+
77+
The middleware implementation is inspired heavily by the request middleware stack presented by [the Slim Framework](https://www.slimframework.com/).
78+
79+
The default encryption implementation featured by the `EncryptionHandler` is borrowed from an example in [the PHP documentation](http://php.net/manual/en/class.sessionhandler.php) on session management.
80+
381
[travis-image]: https://travis-ci.org/ericmann/sessionz.svg?branch=master
482
[travis-url]: https://travis-ci.org/ericmann/sessionz
583
[coveralls-image]: https://coveralls.io/repos/github/ericmann/sessionz/badge.svg?branch=master

0 commit comments

Comments
 (0)