Skip to content

Commit 1b12389

Browse files
committed
Added a router listener
1 parent e61c73c commit 1b12389

File tree

7 files changed

+122
-238
lines changed

7 files changed

+122
-238
lines changed

src/CacheBundle.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public function build(ContainerBuilder $container)
3131

3232
$container->addCompilerPass(new Compiler\SessionSupportCompilerPass());
3333
$container->addCompilerPass(new Compiler\DoctrineSupportCompilerPass());
34-
$container->addCompilerPass(new Compiler\RouterCompilerPass());
3534

3635
if ($container->getParameter('kernel.debug')) {
3736
$container->addCompilerPass(new Compiler\DataCollectorCompilerPass());

src/DependencyInjection/CacheExtension.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
namespace Cache\CacheBundle\DependencyInjection;
1313

14-
use Cache\CacheBundle\Cache\LoggingCachePool;
1514
use Cache\CacheBundle\DataCollector\CacheDataCollector;
15+
use Cache\CacheBundle\Routing\RouterListener;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
1818
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
@@ -45,6 +45,14 @@ public function load(array $configs, ContainerBuilder $container)
4545
}
4646
}
4747

48+
if ($config['router']['enabled']) {
49+
$container->register('cache.router_listener', RouterListener::class)
50+
->addArgument(new Reference($config['router']['service_id']))
51+
->addArgument($config['router']['ttl'])
52+
->addTag('kernel.event_listener', ['event'=>'kernel.request', 'method'=>'onBeforeRouting', 'priority'=>33])
53+
->addTag('kernel.event_listener', ['event'=>'kernel.request', 'method'=>'onAfterRouting', 'priority'=>31]);
54+
}
55+
4856
}
4957

5058
public function getAlias()

src/DependencyInjection/Compiler/RouterCompilerPass.php

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,7 @@ private function addRouterSection()
142142
->defaultFalse()
143143
->end()
144144
->integerNode('ttl')
145-
->defaultValue(86400)
146-
->end()
147-
->booleanNode('auto-register')
148-
->defaultTrue()
145+
->defaultValue(604800)
149146
->end()
150147
->scalarNode('service_id')
151148
->isRequired()

src/Routing/Matcher/CacheUrlMatcher.php

Lines changed: 0 additions & 78 deletions
This file was deleted.

src/Routing/Router.php

Lines changed: 0 additions & 109 deletions
This file was deleted.

src/Routing/RouterListener.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Cache\CacheBundle\Routing;
4+
5+
use Cache\Taggable\TaggablePoolInterface;
6+
use Psr\Cache\CacheItemPoolInterface;
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
use Symfony\Component\HttpFoundation\Request;
9+
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
10+
use Symfony\Component\HttpKernel\KernelEvents;
11+
12+
/**
13+
* @author Tobias Nyholm <[email protected]>
14+
*/
15+
class RouterListener
16+
{
17+
/**
18+
* @var CacheItemPoolInterface
19+
*/
20+
private $cache;
21+
22+
/**
23+
* @var int
24+
*/
25+
private $ttl;
26+
27+
/**
28+
* @param CacheItemPoolInterface $cache
29+
* @param int $ttl
30+
*/
31+
public function __construct(CacheItemPoolInterface $cache, $ttl)
32+
{
33+
$this->cache = $cache;
34+
$this->ttl = $ttl;
35+
}
36+
37+
/**
38+
* Before routing, try to fetch route result from cache.
39+
*
40+
* @param GetResponseEvent $event
41+
*/
42+
public function onBeforeRouting(GetResponseEvent $event)
43+
{
44+
$request = $event->getRequest();
45+
46+
if ($request->attributes->has('_controller')) {
47+
// routing is already done
48+
return;
49+
}
50+
51+
$item = $this->getCacheItem($request);
52+
if (!$item->isHit()) {
53+
return;
54+
}
55+
56+
$request->attributes->add($item->get());
57+
}
58+
59+
/**
60+
* After the routing, make sure to store the result in cache.
61+
*
62+
* @param GetResponseEvent $event
63+
*/
64+
public function onAfterRouting(GetResponseEvent $event)
65+
{
66+
$request = $event->getRequest();
67+
68+
if (!$request->attributes->has('_controller')) {
69+
// routing has not taken place
70+
return;
71+
}
72+
73+
$item = $this->getCacheItem($request);
74+
if ($item->isHit()) {
75+
return;
76+
}
77+
78+
$item->set($request->attributes->all());
79+
$this->cache->save($item);
80+
}
81+
82+
/**
83+
* Get a good key that varies on method, host, path info etc etc.
84+
*
85+
* @param Request $request
86+
*
87+
* @return string
88+
*/
89+
private function createCacheKey(Request $request)
90+
{
91+
$key = 'route_'.$request->getMethod().'_'.$request->getHost().'_'.$request->getPathInfo();
92+
93+
return $key;
94+
}
95+
96+
/**
97+
* @param Request $request
98+
*
99+
* @return \Psr\Cache\CacheItemInterface
100+
*/
101+
private function getCacheItem(Request $request)
102+
{
103+
$key = $this->createCacheKey($request);
104+
if ($this->cache instanceof TaggablePoolInterface) {
105+
$item = $this->cache->getItem($key, ['routing']);
106+
} else {
107+
$item = $this->cache->getItem($key);
108+
}
109+
110+
return $item;
111+
}
112+
}

0 commit comments

Comments
 (0)