|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the FOSRestBundle package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace FOS\RestBundle\Response\AllowedMethodsLoader; |
|
13
|
|
|
|
|
14
|
|
|
use Symfony\Component\Config\ConfigCache; |
|
15
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmerInterface; |
|
16
|
|
|
use Symfony\Component\Routing\RouterInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* AllowedMethodsRouterLoader implementation using RouterInterface to fetch |
|
20
|
|
|
* allowed http methods. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Boris Guéry <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
final class AllowedMethodsRouterLoader implements AllowedMethodsLoaderInterface, CacheWarmerInterface |
|
25
|
|
|
{ |
|
26
|
|
|
private $router; |
|
27
|
|
|
private $cache; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(RouterInterface $router, string $cacheDir, bool $isDebug) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->router = $router; |
|
32
|
|
|
$this->cache = new ConfigCache(sprintf('%s/allowed_methods.cache.php', $cacheDir), $isDebug); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* {@inheritdoc} |
|
37
|
|
|
*/ |
|
38
|
|
|
public function getAllowedMethods(): array |
|
39
|
|
|
{ |
|
40
|
|
|
if (!$this->cache->isFresh()) { |
|
41
|
|
|
$this->warmUp(null); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
return require $this->cache->getPath(); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function isOptional(): bool |
|
51
|
|
|
{ |
|
52
|
|
|
return true; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function warmUp($cacheDir): void |
|
59
|
|
|
{ |
|
60
|
|
|
$processedRoutes = []; |
|
61
|
|
|
|
|
62
|
|
|
$routeCollection = $this->router->getRouteCollection(); |
|
63
|
|
|
|
|
64
|
|
|
foreach ($routeCollection->all() as $name => $route) { |
|
65
|
|
|
if (!isset($processedRoutes[$route->getPath()])) { |
|
66
|
|
|
$processedRoutes[$route->getPath()] = [ |
|
67
|
|
|
'methods' => [], |
|
68
|
|
|
'names' => [], |
|
69
|
|
|
]; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$processedRoutes[$route->getPath()]['names'][] = $name; |
|
73
|
|
|
$processedRoutes[$route->getPath()]['methods'] = array_merge( |
|
74
|
|
|
$processedRoutes[$route->getPath()]['methods'], |
|
75
|
|
|
$route->getMethods() |
|
76
|
|
|
); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
$allowedMethods = []; |
|
80
|
|
|
|
|
81
|
|
|
foreach ($processedRoutes as $processedRoute) { |
|
82
|
|
|
if (count($processedRoute['methods']) > 0) { |
|
83
|
|
|
foreach ($processedRoute['names'] as $name) { |
|
84
|
|
|
$allowedMethods[$name] = array_unique($processedRoute['methods']); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->cache->write( |
|
90
|
|
|
sprintf('<?php return %s;', var_export($allowedMethods, true)), |
|
91
|
|
|
$routeCollection->getResources() |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|