StashSilex
![Software License][ico-license]
![Coverage Status][ico-scrutinizer]
, (*1)
StashSilex is a Silex service provider and session handler for the popular caching library Stash., (*2)
Requirements
StashSilex requires PHP version 5.3+, Silex version 1.* and Stash version 0.11+, (*3)
Install
Via Composer, (*4)
``` bash
$ composer require M1/StashSilex, (*5)
## Usage
You use the StashServiceProvider to register the service provider with the usual syntax for registering service providers:
``` php
$app->register(new M1\StashSilex\StashServiceProvider());
There's a few options you can use when registering the service provider., (*6)
Pools
You can register either one pool using pool.options or multiple using pools.options, this works like the
Doctrine Service Provider., (*7)
Registering one pool:
``` php
$app->register(new M1\StashSilex\StashServiceProvider(), array(
'pool.options' => array(
'driver' => 'FileSystem',
'options' => array(
'path' => DIR.'/../../app/cache',
),
)
));, (*8)
$item = $app['pool']->getItem('path/to/item');, (*9)
Registering multiple pools:
```php
$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']., (*10)
For example, in the above code, the FileSystem pool will be available through $app['pool'] and $app['pools']['fs']., (*11)
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., (*12)
You can set the driver options through options like so:, (*13)
``` php
$app->register(new M1\StashSilex\StashServiceProvider(), array(
'pool.options' => array(
'driver' => 'FileSystem',
'options' => array(
'path' => DIR.'/../../app/cache',
),
)
));, (*14)
You can see the full list of the available options for each driver [here](http://www.stashphp.com/Drivers.html).
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:
``` php
$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., (*15)
For example if you decided to not use Monolog through the service provider (not recommended), you can use your custom logger like so:, (*16)
$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., (*17)
The first way is via the service provider., (*18)
The below creates sessions with defaults:, (*19)
$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:, (*20)
$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:, (*21)
$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:, (*22)
``` yaml, (*23)
example.yml
pools:
filesystem:
driver: FileSystem
options:
path: %dir%/../../app/cache
session:
prefix: session_name
expiretime: 3200, (*24)
``` php
$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., (*25)
Change log
Please see CHANGELOG for more information what has changed recently., (*26)
Testing
bash
$ composer test, (*27)
Contributing
Please see CONTRIBUTING and CONDUCT for details., (*28)
Security
If you discover any security related issues, please email hello@milescroxford.com instead of using the issue tracker., (*29)
Credits
License
The MIT License (MIT). Please see License File for more information., (*30)