-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizationCacheWarmer.php
More file actions
120 lines (101 loc) · 3.38 KB
/
Copy pathAuthorizationCacheWarmer.php
File metadata and controls
120 lines (101 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
<?php
/*
*
* (c) Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Yarhon\RouteGuardBundle\Cache;
use Psr\Cache\CacheItemPoolInterface;
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RouteCollection;
use Yarhon\RouteGuardBundle\Cache\DataCollector\RouteCollectionDataCollector;
/**
* @author Yaroslav Honcharuk <yaroslav.xs@gmail.com>
*/
class AuthorizationCacheWarmer implements CacheWarmerInterface
{
/**
* @var RouteCollectionDataCollector
*/
private $dataCollector;
/**
* @var RouteCollection
*/
private $routeCollection;
/**
* @var CacheItemPoolInterface
*/
private $testsCache;
/**
* @var CacheItemPoolInterface
*/
private $controllerMetadataCache;
/**
* @var CacheItemPoolInterface
*/
private $routeMetadataCache;
/**
* @param RouteCollectionDataCollector $dataCollector
* @param RouterInterface $router
* @param CacheItemPoolInterface $testsCache
* @param CacheItemPoolInterface $controllerMetadataCache
* @param CacheItemPoolInterface $routeMetadataCache
*/
public function __construct(RouteCollectionDataCollector $dataCollector, RouterInterface $router, CacheItemPoolInterface $testsCache, CacheItemPoolInterface $controllerMetadataCache, CacheItemPoolInterface $routeMetadataCache)
{
$this->dataCollector = $dataCollector;
$this->routeCollection = $router->getRouteCollection();
$this->testsCache = $testsCache;
$this->controllerMetadataCache = $controllerMetadataCache;
$this->routeMetadataCache = $routeMetadataCache;
}
/**
* {@inheritdoc}
*/
public function isOptional()
{
return false;
}
/**
* {@inheritdoc}
*/
public function warmUp($cacheDir)
{
$this->clear();
$data = $this->dataCollector->collect($this->routeCollection);
foreach ($data as $routeName => list($tests, $controllerMetadata, $routeMetadata)) {
$this->saveDeferred($this->testsCache, $routeName, $tests);
$this->saveDeferred($this->controllerMetadataCache, $routeName, $controllerMetadata);
$this->saveDeferred($this->routeMetadataCache, $routeName, $routeMetadata);
}
// Note: CacheAdapter saves cache items automatically on destruct, even without explicit "commit" call.
$this->commit();
}
private function clear()
{
$this->testsCache->clear();
$this->controllerMetadataCache->clear();
$this->routeMetadataCache->clear();
}
private function commit()
{
$this->testsCache->commit();
$this->controllerMetadataCache->commit();
$this->routeMetadataCache->commit();
}
/**
* @param CacheItemPoolInterface $cache
* @param string $routeName
* @param mixed $item
*/
private function saveDeferred(CacheItemPoolInterface $cache, $routeName, $item)
{
$cacheKey = CacheFactory::getValidCacheKey($routeName);
$cacheItem = $cache->getItem($cacheKey);
$cacheItem->set($item);
$cache->saveDeferred($cacheItem);
}
}