Skip to content

Commit

Permalink
Merge 955769f into 1367d69
Browse files Browse the repository at this point in the history
  • Loading branch information
tedivm committed May 9, 2014
2 parents 1367d69 + 955769f commit 8e82e74
Show file tree
Hide file tree
Showing 19 changed files with 333 additions and 290 deletions.
6 changes: 3 additions & 3 deletions Adapters/DoctrineAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ public function deleteAll()
public function getStats()
{
$stats = array();
$logger = $this->cacheService->getLogger();
$stats['hits'] = $logger->getHits();
$stats['misses'] = $logger->getCalls() - $stats['hits'];
$tracker = $this->cacheService->getTracker();
$stats['hits'] = $tracker->getHits();
$stats['misses'] = $tracker->getCalls() - $stats['hits'];

return $stats;
}
Expand Down
30 changes: 15 additions & 15 deletions Collector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@ class CacheDataCollector extends DataCollector
protected $cacheOptions;

/**
* The logger classes for all cache services.
* The tracker classes for all cache services.
*
* @var array
*/
protected $loggers = array();
protected $trackers = array();

public function __construct($default, $caches, $options)
{
Expand All @@ -50,13 +50,13 @@ public function __construct($default, $caches, $options)
}

/**
* Inject the logger for a cache service.
* Inject the tracker for a cache service.
*
* @param $logger
* @param $tracker
*/
public function addLogger($logger)
public function addTracker($tracker)
{
$this->loggers[] = $logger;
$this->trackers[] = $tracker;
}

/**
Expand All @@ -65,23 +65,23 @@ public function addLogger($logger)
public function collect(Request $request, Response $response, \Exception $exception = null)
{
$info = array('calls' => 0, 'hits' => 0);
foreach ($this->loggers as $logger) {
$name = $logger->getName();
$calls = $logger->getCalls();
$hits = $logger->getHits();
foreach ($this->trackers as $tracker) {
$name = $tracker->getName();
$calls = $tracker->getCalls();
$hits = $tracker->getHits();

$info['calls'] += $calls;
$info['hits'] += $hits;

$info['caches'][$name]['options'] = $this->cacheOptions[$name];
$info['caches'][$name]['queries'] = $logger->getQueries();
$info['caches'][$name]['queries'] = $tracker->getQueries();
$info['caches'][$name]['calls'] = $calls;
$info['caches'][$name]['hits'] = $hits;
}

$handlers = Drivers::getDrivers();
foreach ($handlers as $handler) {
$pieces = explode('\\', $handler);
$drivers = Drivers::getDrivers();
foreach ($drivers as $driver) {
$pieces = explode('\\', $driver);
$name = array_pop($pieces);
if (!in_array($name, array('Ephemeral', 'Composite'))) {
$info['availableDrivers'][] = $name;
Expand Down Expand Up @@ -112,7 +112,7 @@ public function getHits()
/**
* Returns the list of available drivers.
*/
public function gethandlers()
public function getDrivers()
{
return $this->data['availableDrivers'];
}
Expand Down
40 changes: 20 additions & 20 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
class Configuration implements ConfigurationInterface
{

protected $handlerSettings = array(
protected $driverSettings = array(
'FileSystem' => array(
'dirSplit' => 2,
'path' => '%kernel.cache_dir%/stash',
Expand Down Expand Up @@ -53,7 +53,7 @@ public function getConfigTreeBuilder()
->then(function ($v) {
$cache = array();
foreach ($v as $key => $value) {
if (in_array($key, array('default_cache', 'logging'))) {
if (in_array($key, array('default_cache', 'tracking'))) {
continue;
}
$cache[$key] = $v[$key];
Expand All @@ -67,7 +67,7 @@ public function getConfigTreeBuilder()
->end()
->children()
->scalarNode('default_cache')->end()
->booleanNode('logging')->end()
->booleanNode('tracking')->end()
->end()
->fixXmlConfig('cache')
->append($this->getCachesNode())
Expand All @@ -78,24 +78,24 @@ public function getConfigTreeBuilder()

protected function getCachesNode()
{
$handlers = array_keys(Drivers::getDrivers());
$drivers = array_keys(Drivers::getDrivers());

$treeBuilder = new TreeBuilder();
$node = $treeBuilder->root('caches');

$childNode = $node
->fixXmlConfig('handler')
->fixXmlConfig('driver')
->requiresAtLeastOneElement()
->useAttributeAsKey('name')
->prototype('array')
->children()
->arrayNode('handlers')
->arrayNode('drivers')
->requiresAtLeastOneElement()
->defaultValue(array('FileSystem'))
->prototype('scalar')
->validate()
->ifNotInArray($handlers)
->thenInvalid('A handler of that name is not registered.')
->ifNotInArray($drivers)
->thenInvalid('A driver of that name is not registered.')
->end()
->end()
->end()
Expand All @@ -104,9 +104,9 @@ protected function getCachesNode()
->booleanNode('inMemory')->defaultTrue()->end()
;

foreach ($handlers as $handler) {
if ($handler !== 'Composite') {
$this->addHandlerSettings($handler, $childNode);
foreach ($drivers as $driver) {
if ($driver !== 'Composite') {
$this->addDriverSettings($driver, $childNode);
}
}

Expand All @@ -116,14 +116,14 @@ protected function getCachesNode()
return $node;
}

public function addHandlerSettings($handler, $rootNode)
public function addDriverSettings($driver, $rootNode)
{
$handlerNode = $rootNode
->arrayNode($handler)
$driverNode = $rootNode
->arrayNode($driver)
->fixXmlConfig('server');

if ($handler == 'Memcache') {
$finalNode = $handlerNode
if ($driver == 'Memcache') {
$finalNode = $driverNode
->info('All options except "servers" are Memcached options. See http://www.php.net/manual/en/memcached.constants.php')
->addDefaultsIfNotSet()
->children()
Expand Down Expand Up @@ -161,8 +161,8 @@ public function addHandlerSettings($handler, $rootNode)
->end()
->end()
;
} elseif ($handler == 'Redis') {
$finalNode = $handlerNode
} elseif ($driver == 'Redis') {
$finalNode = $driverNode
->info("Accepts server info, password, and database.")
->addDefaultsIfNotSet()
->children()
Expand All @@ -185,9 +185,9 @@ public function addHandlerSettings($handler, $rootNode)
->end()
;
} else {
$defaults = isset($this->handlerSettings[$handler]) ? $this->handlerSettings[$handler] : array();
$defaults = isset($this->driverSettings[$driver]) ? $this->driverSettings[$driver] : array();

$node = $handlerNode
$node = $driverNode
->addDefaultsIfNotSet()
->children();

Expand Down
35 changes: 20 additions & 15 deletions DependencyInjection/TedivmStashExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/**
* Bundle extension to handle configuration of the Stash bundle. Based on the specification provided
* in the configuration file, this extension instantiates and dynamically injects the selected caching provider into
* the Stash service, passing it any handler-specific settings from the configuration.
* the Stash service, passing it any driver-specific settings from the configuration.
*
* @author Josh Hall-Bachner <jhallbachner@gmail.com>
*/
Expand All @@ -30,10 +30,10 @@ public function load(array $configs, ContainerBuilder $container)

$container->setAlias('cache', sprintf('stash.%s_cache', $config['default_cache']));

$lq = isset($config['logging'])
? $config['logging']
$lq = isset($config['tracking'])
? $config['tracking']
: (in_array($container->getParameter('kernel.environment'), array('dev', 'test')));
$container->setParameter('stash.logging', $lq);
$container->setParameter('stash.tracker', $lq);

$caches = array();
$options = array();
Expand All @@ -50,13 +50,18 @@ public function load(array $configs, ContainerBuilder $container)

protected function addCacheService($name, $cache, $container)
{
$logqueries = $container->getParameter('stash.logging');
$logqueries = $container->getParameter('stash.tracker');

$handlers = $cache['handlers'];
unset($cache['handlers']);
if (isset($cache['drivers'])) {
$drivers = $cache['drivers'];
} else {
$drivers = $cache['drivers'];
}

unset($cache['drivers']);

if (isset($cache['inMemory']) && $cache['inMemory']) {
array_unshift($handlers, 'Ephemeral');
array_unshift($drivers, 'Ephemeral');
}
unset($cache['inMemory']);

Expand All @@ -67,16 +72,16 @@ protected function addCacheService($name, $cache, $container)
unset($cache['registerSessionHandler']);

$container
->setDefinition(sprintf('stash.handler.%s_cache', $name), new DefinitionDecorator('stash.handler'))
->setDefinition(sprintf('stash.driver.%s_cache', $name), new DefinitionDecorator('stash.driver'))
->setArguments(array(
$handlers,
$drivers,
$cache
))
->setAbstract(false)
;

$container
->setDefinition(sprintf('stash.logger.%s_cache', $name), new DefinitionDecorator('stash.logger'))
->setDefinition(sprintf('stash.tracker.%s_cache', $name), new DefinitionDecorator('stash.tracker'))
->setArguments(array(
$name
))
Expand All @@ -88,8 +93,8 @@ protected function addCacheService($name, $cache, $container)
->setDefinition(sprintf('stash.%s_cache', $name), new DefinitionDecorator('stash.cache'))
->setArguments(array(
$name,
new Reference(sprintf('stash.handler.%s_cache', $name)),
new Reference(sprintf('stash.logger.%s_cache', $name))
new Reference(sprintf('stash.driver.%s_cache', $name)),
new Reference(sprintf('stash.tracker.%s_cache', $name))
))
->setAbstract(false)
;
Expand All @@ -116,8 +121,8 @@ protected function addCacheService($name, $cache, $container)

$container
->getDefinition('data_collector.stash')
->addMethodCall('addLogger', array(
new Reference(sprintf('stash.logger.%s_cache', $name))
->addMethodCall('addTracker', array(
new Reference(sprintf('stash.tracker.%s_cache', $name))
))
;

Expand Down
14 changes: 7 additions & 7 deletions Factory/HandlerFactory.php → Factory/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use Stash\Drivers,
Stash\Interfaces\DriverInterface;

class HandlerFactory
class DriverFactory
{
/**
* Given a list of cache types and options, creates a CompositeDrivers wrapping the specified drivers.
Expand All @@ -13,14 +13,14 @@ class HandlerFactory
* @param $options
* @return DriverInterface
*/
public static function createHandler($types, $options)
public static function createDriver($types, $options)
{
$handlers = Drivers::getDrivers();
$drivers = Drivers::getDrivers();

$h = array();

foreach ($types as $type) {
$class = $handlers[$type];
$class = $drivers[$type];
if ($type === 'Memcache' && isset($options[$type])) {
// Fix servers spec since underlying drivers expect plain arrays, not hashes.
$servers = array();
Expand All @@ -43,9 +43,9 @@ public static function createHandler($types, $options)
return reset($h);
}

$class = $handlers['Composite'];
$handler = new $class(array('drivers' => $h));
$class = $drivers['Composite'];
$driver = new $class(array('drivers' => $h));

return $handler;
return $driver;
}
}
14 changes: 7 additions & 7 deletions Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,22 @@

<parameters>
<parameter key="stash.cache.class">Tedivm\StashBundle\Service\CacheService</parameter>
<parameter key="stash.logger.class">Tedivm\StashBundle\Service\CacheLogger</parameter>
<parameter key="stash.handler.class">Stash\Handler\HandlerInterface</parameter>
<parameter key="stash.factory.class">Tedivm\StashBundle\Factory\HandlerFactory</parameter>
<parameter key="stash.tracker.class">Tedivm\StashBundle\Service\CacheTracker</parameter>
<parameter key="stash.driver.class">Stash\Interfaces\DriverInterface</parameter>
<parameter key="stash.factory.class">Tedivm\StashBundle\Factory\DriverFactory</parameter>
<parameter key="stash.adapter.doctrine.class">Tedivm\StashBundle\Adapters\DoctrineAdapter</parameter>
<parameter key="stash.adapter.session.class">Tedivm\StashBundle\Adapters\SessionHandlerAdapter</parameter>
<parameter key="stash.data_collector.class">Tedivm\StashBundle\Collector\CacheDataCollector</parameter>
</parameters>

<services>
<service id="stash.cache" class="%stash.cache.class%" abstract="true"/>
<service id="stash.logger" class="%stash.logger.class%" abstract="true"/>
<service id="stash.handler"
class="%stash.handler.class%"
<service id="stash.tracker" class="%stash.tracker.class%" abstract="true"/>
<service id="stash.driver"
class="%stash.driver.class%"
abstract="true"
synthetic="true"
factory-method="createHandler"
factory-method="createDriver"
factory-class="%stash.factory.class%"
/>
<service id="stash.adapter.doctrine" class="%stash.adapter.doctrine.class%" abstract="true"/>
Expand Down
Empty file added Resources/doc/index.rst
Empty file.

0 comments on commit 8e82e74

Please sign in to comment.