|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class for routes config caching |
|
4
|
|
|
* |
|
5
|
|
|
* @file RoutesCacheDecorator.php |
|
6
|
|
|
* |
|
7
|
|
|
* PHP version 8.0+ |
|
8
|
|
|
* |
|
9
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
10
|
|
|
* @copyright © 2012-2021 Alexander Yancharuk |
|
11
|
|
|
* @date 2015-05-24 20:14 |
|
12
|
|
|
* @license The BSD 3-Clause License |
|
13
|
|
|
* <https://tldrlegal.com/license/bsd-3-clause-license-(revised)> |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace Veles\Routing; |
|
17
|
|
|
|
|
18
|
|
|
use Veles\Cache\Cache; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Class RoutesCacheDecorator |
|
22
|
|
|
* @author Yancharuk Alexander <alex at itvault dot info> |
|
23
|
|
|
*/ |
|
24
|
|
|
class RoutesCacheDecorator extends AbstractRoutesConfig |
|
25
|
|
|
{ |
|
26
|
|
|
protected $config; |
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
protected $prefix; |
|
29
|
|
|
/** @noinspection PhpMissingParentConstructorInspection */ |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param RoutesConfig $config |
|
33
|
|
|
*/ |
|
34
|
6 |
|
public function __construct(RoutesConfig $config) |
|
35
|
|
|
{ |
|
36
|
6 |
|
$this->config = $config; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Returns array that contains routes configuration |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
* @throws \Exception |
|
44
|
|
|
*/ |
|
45
|
2 |
|
public function getData() |
|
46
|
|
|
{ |
|
47
|
2 |
|
if (false !== ($data = Cache::get($this->getPrefix()))) { |
|
48
|
1 |
|
return $data; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
1 |
|
$data = $this->config->getData(); |
|
52
|
1 |
|
Cache::set($this->getPrefix(), $data); |
|
53
|
|
|
|
|
54
|
1 |
|
return $data; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @return string |
|
59
|
|
|
*/ |
|
60
|
6 |
|
public function getPrefix() |
|
61
|
|
|
{ |
|
62
|
6 |
|
return $this->prefix; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param string $prefix |
|
67
|
|
|
*/ |
|
68
|
6 |
|
public function setPrefix($prefix) |
|
69
|
|
|
{ |
|
70
|
6 |
|
$this->prefix = $prefix; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Returns array that contains routes configuration |
|
75
|
|
|
* |
|
76
|
|
|
* @param string $name Name of section |
|
77
|
|
|
* |
|
78
|
|
|
* @return array |
|
79
|
|
|
* @throws \Exception |
|
80
|
|
|
*/ |
|
81
|
3 |
|
public function getSection($name) |
|
82
|
|
|
{ |
|
83
|
3 |
|
if (false !== ($data = Cache::get($this->getPrefix()))) { |
|
84
|
2 |
|
return isset($data[$name]) ? $data[$name] : []; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
1 |
|
Cache::set($this->getPrefix(), $this->config->getData()); |
|
88
|
|
|
|
|
89
|
1 |
|
return $this->config->getSection($name); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|