Skip to content

Bugfixes and improvements #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified src/Cache/LoggingCachePool.php
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion src/DataCollector/CacheDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CacheDataCollector extends DataCollector
*
* @type string
*/
const TEMPLATE = 'CacheCacheBundle:Collector:cache.html.twig';
const TEMPLATE = 'CacheBundle:Collector:cache.html.twig';

/**
* @var LoggingCachePool[]
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/CacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ public function load(array $configs, ContainerBuilder $container)
}

foreach (['router', 'session', 'doctrine'] as $section) {
if ($container[$section]['enabled']) {
$container->setParameter($this->getAlias().'.'.$section, $config[$section]);
if ($config[$section]['enabled']) {
$container->setParameter('cache.'.$section, $config[$section]);
}
}

Expand Down
21 changes: 5 additions & 16 deletions src/DependencyInjection/Compiler/DataCollectorCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@

namespace Cache\CacheBundle\DependencyInjection\Compiler;

use Cache\CacheBundle\Cache\LoggingCachePool;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class DataCollectorCompilerPass
*
* @author Aaron Scherer <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class DataCollectorCompilerPass extends BaseCompilerPass
{
Expand All @@ -25,23 +27,9 @@ class DataCollectorCompilerPass extends BaseCompilerPass
*/
protected function prepare()
{
$this->transformLoggableCachePools();

$instances = $this->container->getParameter($this->getAlias() . '.instance');

$definition = $this->container->getDefinition('data_collector.cache');

foreach (array_keys($instances) as $name) {
$instance = new Reference(sprintf("%s.instance.%s", $this->getAlias(), $name));
$definition->addMethodCall('addInstance', array($name, $instance));
}

$this->container->setDefinition('data_collector.cache', $definition);
}

private function transformLoggableCachePools()
{
$collectorDefinition = $this->container->getDefinition('data_collector.cache');
$serviceIds = $this->container->findTaggedServiceIds('cache.provider');

foreach (array_keys($serviceIds) as $id) {

// Duplicating definition to $originalServiceId.logged
Expand All @@ -53,6 +41,7 @@ private function transformLoggableCachePools()

// Overwrite the original service id with the new LoggingCachePool instance
$this->container->setAlias($id, $id.'.logger');
$collectorDefinition->addMethodCall('addInstance', [$id, new Reference($id.'.logger')]);
}
}
}
23 changes: 11 additions & 12 deletions src/DependencyInjection/Compiler/DoctrineSupportCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,16 @@

namespace Cache\CacheBundle\DependencyInjection\Compiler;

use Cache\Bridge\DoctrineCacheBridge;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

/**
* Class DoctrineSupportCompilerPass
*
* @author Aaron Scherer <[email protected]>
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineSupportCompilerPass extends BaseCompilerPass
{
Expand All @@ -27,7 +31,7 @@ class DoctrineSupportCompilerPass extends BaseCompilerPass
protected function prepare()
{
// If disabled, continue
if (!$this->container->hasParameter($this->getAlias().'.doctrine')) {
if (!$this->container->hasParameter('cache.doctrine')) {
return;
}

Expand All @@ -37,7 +41,7 @@ protected function prepare()
);
}

$this->enableDoctrineSupport($this->container->getParameter($this->getAlias().'.doctrine'));
$this->enableDoctrineSupport($this->container->getParameter('cache.doctrine'));
}

/**
Expand All @@ -56,15 +60,10 @@ protected function enableDoctrineSupport(array $config)
continue;
}

if (!isset($cacheData['instance'])) {
throw new InvalidConfigurationException(
sprintf(
"There was no instance passed. Please specify a instance under the %s type",
$cacheType
)
);
}
$cacheDefinitionName = sprintf('%s.instance.%s.bridge', $this->getAlias(), $cacheData['instance']);
$bridgeServiceId = sprintf('cache.provider.doctrine.%s.bridge', $cacheType);
$bridgeDef = $this->container->register($bridgeServiceId, DoctrineCacheBridge::class);
$bridgeDef->addArgument(0, new Reference($cacheData['service_id']))
->setPublic(false);

foreach ($cacheData[$type] as $manager) {
$doctrineDefinitionName =
Expand All @@ -76,7 +75,7 @@ protected function enableDoctrineSupport(array $config)
);

// Replace the doctrine entity manager cache with our bridge
$this->container->setAlias($doctrineDefinitionName, $cacheDefinitionName);
$this->container->setAlias($doctrineDefinitionName, $bridgeServiceId);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/DependencyInjection/Compiler/RouterCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ protected function prepare()

$def = clone $this->container->findDefinition('router');
$def->setClass('Cache\CacheBundle\Routing\Router');
$def->addMethodCall('setCache', [new Reference(sprintf('cache.instance.%s', $router['instance']))]);
$def->addMethodCall('setCache', [new Reference($router['service_id'])]);
$def->addMethodCall('setTtl', [$router['ttl']]);

$this->container->setDefinition('cache.router', $def);
$this->container->setAlias('router.alias', 'cache.router');

if ($router['auto-register']) {
$this->container->setAlias('router', 'router.cache');
$this->container->setAlias('router', 'cache.router');
}
}
}
25 changes: 4 additions & 21 deletions src/DependencyInjection/Compiler/SessionSupportCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Cache\CacheBundle\DependencyInjection\Compiler;

use Cache\CacheBundle\Session\SessionHandler;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
Expand Down Expand Up @@ -49,35 +50,17 @@ protected function prepare()
*/
private function enableSessionSupport(array $config)
{
if (empty($config['instance'])) {
throw new InvalidConfigurationException("Instance must be passed under the `session` config.");
}

$instance = $config['instance'];
$instances = $this->container->getParameter($this->getAlias().'.instance');

if (!isset($instances[$instance])) {
throw new InvalidConfigurationException(
sprintf(
"Failed to hook into the session. The instance \"%s\" doesn't exist!",
$instance
)
);
}

// calculate options
$sessionOptions = $this->container->getParameter('session.storage.options');
if (isset($sessionOptions['cookie_lifetime']) && !isset($config['cookie_lifetime'])) {
$config['cookie_lifetime'] = $sessionOptions['cookie_lifetime'];
}
// load the session handler
$definition =
new Definition($this->container->getParameter(sprintf('%s.session.handler.class', $this->getAlias())));
$definition->addArgument(new Reference(sprintf('%s.instance.%s.bridge', $this->getAlias(), $instance)))
$definition = new Definition(SessionHandler::class);
$definition->addArgument(new Reference($config['service_id']))
->addArgument($config);

$this->container->setDefinition(sprintf('%s.session_handler', $this->getAlias()), $definition);
$this->container->setAlias('cache.session_handler', sprintf('%s.session_handler', $this->getAlias()));
$this->container->setDefinition('cache.session_handler', $definition);

$this->container->setAlias('session.handler', 'cache.session_handler');
}
Expand Down
10 changes: 5 additions & 5 deletions src/DependencyInjection/Configuration.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ private function addSessionSupportSection()
->booleanNode('enabled')
->defaultFalse()
->end()
->scalarNode('instance')->end()
->scalarNode('service_id')->isRequired()->end()
->scalarNode('prefix')
->defaultValue("session_")
->end()
Expand Down Expand Up @@ -94,7 +94,7 @@ private function addDoctrineSection()
->arrayNode($type)
->canBeUnset()
->children()
->scalarNode('instance')->end()
->scalarNode('service_id')->isRequired()->end()
->arrayNode('entity_managers')
->defaultValue(array())
->beforeNormalization()
Expand Down Expand Up @@ -142,13 +142,13 @@ private function addRouterSection()
->defaultFalse()
->end()
->integerNode('ttl')
->isRequired()
->defaultValue(86400)
->end()
->booleanNode('auto-register')
->defaultTrue()
->end()
->scalarNode('instance')
->defaultNull()
->scalarNode('service_id')
->isRequired()
->end()
->end();

Expand Down
9 changes: 5 additions & 4 deletions src/Routing/Matcher/CacheUrlMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ public function match($pathInfo)
$method = strtolower($this->context->getMethod());
$key = 'route_'.$method.'_'.$host.'_'.$pathInfo;

if ($this->cachePool->hasItem($key)) {
return $this->cachePool->getItem($key)->get();
$cacheItem = $this->cachePool->getItem($key);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}

$match = parent::match($pathInfo);
$item = $this->cachePool->getItem($key);
$item->set($match)
$cacheItem->set($match)
->expiresAfter($this->ttl);
$this->cachePool->save($cacheItem);

return $match;
}
Expand Down
16 changes: 13 additions & 3 deletions src/Routing/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@
*/
class Router extends BaseRouter
{
const CACHE_LIFETIME = 604800; // a week

/**
* @var ContainerInterface
*/
Expand Down Expand Up @@ -92,8 +90,20 @@ public function setCache(CacheItemPoolInterface $cache)
/**
* @return int
*/
public function getTtl()
protected function getTtl()
{
return $this->ttl;
}

/**
* @param int $ttl
*
* @return Router
*/
public function setTtl($ttl)
{
$this->ttl = $ttl;

return $this;
}
}
4 changes: 1 addition & 3 deletions src/Session/SessionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,7 @@ public function write($sessionId, $data)
*/
public function destroy($sessionId)
{
$item = $this->cache->getItem($this->prefix . $sessionId);

return $this->cache->deleteItem($item);
return $this->cache->deleteItem($this->prefix . $sessionId);
}

/**
Expand Down