forked from jorgelive/SonataAdminBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoutesCache.php
86 lines (71 loc) · 2.23 KB
/
RoutesCache.php
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
<?php
/*
* This file is part of the Sonata project.
*
* (c) Thomas Rabaix <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\AdminBundle\Route;
use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Config\Resource\FileResource;
/**
* Class RoutesCache
*
* @package Sonata\AdminBundle\Route
* @author Thomas Rabaix <[email protected]>
*/
class RoutesCache
{
/**
* @var string
*/
protected $cacheFolder;
/**
* @var boolean
*/
protected $debug;
/**
* @param string $cacheFolder
* @param bool $debug
*/
public function __construct($cacheFolder, $debug)
{
$this->cacheFolder = $cacheFolder;
$this->debug = $debug;
}
/**
* @param AdminInterface $admin
*
* @return mixed
* @throws \RuntimeException
*/
public function load(AdminInterface $admin)
{
$filename = $this->cacheFolder.'/route_'.md5($admin->getCode());
$cache = new ConfigCache($filename, $this->debug);
if (!$cache->isFresh()) {
$resources = array();
$routes = array();
$reflection = new \ReflectionObject($admin);
$resources[] = new FileResource($reflection->getFileName());
if (!$admin->getRoutes()) {
throw new \RuntimeException('Invalid data type, Admin::getRoutes must return a RouteCollection');
}
foreach ($admin->getRoutes()->getElements() as $code => $route) {
$routes[$code] = $route->getDefault('_sonata_name');
}
if (!is_array($admin->getExtensions())) {
throw new \RuntimeException('extensions must be an array');
}
foreach ($admin->getExtensions() as $extension) {
$reflection = new \ReflectionObject($extension);
$resources[] = new FileResource($reflection->getFileName());
}
$cache->write(serialize($routes), $resources);
}
return unserialize(file_get_contents($filename));
}
}