m1 / stash-silex
StashSilex is a Silex service provider and session handler for the popular caching library Stash
Installs: 3 237
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 3
Forks: 2
Open Issues: 0
Requires
- php: >=5.3.0
- silex/silex: 1.*
- tedivm/stash: ^0.14
Requires (Dev)
- m1/vars: ~0.6
- monolog/monolog: ^1.17
- phpunit/phpunit: 4.*
- scrutinizer/ocular: ~1.1
- squizlabs/php_codesniffer: ~2.3
- symfony/yaml: ~2.8
This package is auto-updated.
Last update: 2024-12-21 21:08:24 UTC
README
StashSilex is a Silex service provider and session handler for the popular caching library Stash.
Requirements
StashSilex requires PHP version 5.3+
, Silex version 1.*
and Stash version 0.11+
Install
Via Composer
$ composer require M1/StashSilex
Usage
You use the StashServiceProvider to register the service provider with the usual syntax for registering service providers:
$app->register(new M1\StashSilex\StashServiceProvider());
There's a few options you can use when registering the service provider.
Pools
You can register either one pool using pool.options
or multiple using pools.options
, this works like the
Doctrine Service Provider.
Registering one pool:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), ) )); $item = $app['pool']->getItem('path/to/item');
Registering multiple pools:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pools.options' => array( 'fs' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), ), 'mc' => array( 'driver' => 'Memcache', 'options' => array( 'servers' => array( '127.0.0.1', '11211' ) ), ), ), )); // same thing $item1 = $app['pools']['fs']->getItem('path/to/item'); $item1 = $app['pool']->getItem('path/to/item'); $item2 = $app['pools']['mc']->getItem('path/to/item');
You can access your pools through $app['pool']
and $app['pools']['the_key_of_your_pool']
. If you have multiple pools, then your
first pool registered will be available through $app['pool']
.
For example, in the above code, the FileSystem
pool will be available through $app['pool']
and $app['pools']['fs']
.
Drivers
The driver option is based on the class names for the available drivers, see here for the class names defined by Stash. The driver names are case sensitive.
You can set the driver options through options
like so:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), ) ));
You can see the full list of the available options for each driver here.
The default driver if no driver is defined is the Ephemeral
driver.
Logger
You can also set the logger for the pool via the logger
option like so:
$app->register(new Silex\Provider\MonologServiceProvider(), array( 'monolog.logfile' => __DIR__.'/../../app/logs/app/dev.log', )); $app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), 'logger' => 'monolog' ) ));
The logger is monolog
due to the MonologServiceProvider
populating $app['monolog']
. The logger option is a string
which your logger service can be accessed through $app
.
For example if you decided to not use Monolog
through the service provider (not recommended), you can use your custom logger like so:
$app['mylog'] = $app->share(function($app) { $logger = new \Monolog\Logger('mylog'); $logger->pushHandler(new Monolog\Handler\StreamHandler('/logfile/mylog.log', Logger::INFO)); return $logger; }); $app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), 'logger' => 'mylog' ) ));
Sessions
You can choose to handle your sessions through Stash in a couple of different ways.
The first way is via the service provider.
The below creates sessions with defaults:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), 'session' => true ) ));
You can also set the ttl
and the session prefix
(what namespace it is stored in in stash, more info here) like so:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ), 'session' => array( 'prefix' => 'session_name', 'expiretime' => 3200 ) ) ));
You can also set the SessionHandler
manually via:
$app->register(new M1\StashSilex\StashServiceProvider(), array( 'pool.options' => array( 'driver' => 'FileSystem', 'options' => array( 'path' => __DIR__.'/../../app/cache', ) ) )); // Without options $app['session.storage.handler'] = $app->share(function ($app) { return new M1\StashSilex\StashSessionHandler($app['pool']); }); // With options $app['session.storage.handler'] = $app->share(function ($app) { return new M1\StashSilex\StashSessionHandler($app['pool'], array( 'prefix' => 'session_name', 'expiretime' => 3200 )); });
Recommendations
Instead of setting the options through an array, think about using a config loader like m1/vars
,
where you can just load the configuration of your pool(s) via a file like so:
# example.yml pools: filesystem: driver: FileSystem options: path: %dir%/../../app/cache session: prefix: session_name expiretime: 3200
$app->register(new M1\Vars\Provider\Silex\VarsServiceProvider('example.yml'), array( 'vars.options' => array( 'variables' => array( 'dir' => __DIR__ ), ))); $app->register(new M1\StashSilex\StashServiceProvider(), array( 'pools.options' => $app['vars']['pools'] ));
This way makes it so much easier to make small little changes without having to dive into code.
Change log
Please see CHANGELOG for more information what has changed recently.
Testing
$ composer test
Contributing
Please see CONTRIBUTING and CONDUCT for details.
Security
If you discover any security related issues, please email hello@milescroxford.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.