-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigurator.php
More file actions
145 lines (129 loc) · 3.34 KB
/
Configurator.php
File metadata and controls
145 lines (129 loc) · 3.34 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Hurtcode\Config;
use Psr\Cache\{CacheItemPoolInterface, InvalidArgumentException};
/**
* Application configurator
*
* @package Hurtcode\Config;
*/
final class Configurator
{
/** Key for compiled configuration item */
private const CACHE = 'config_cache_item';
private ConfigInterface $configs;
private CompilerInterface $compiler;
private ?CacheItemPoolInterface $cacheItemPool;
/**
* Helps to locate configurator
* part from other point of program
*
* @var Configurator|null
*/
public static ?Configurator $locator;
/**
* Configurator constructor
*
* @param ConfigInterface $configs
* @param CompilerInterface $compiler
* @param CacheItemPoolInterface|null $cacheItemPool
*/
public function __construct(
ConfigInterface $configs,
CompilerInterface $compiler,
?CacheItemPoolInterface $cacheItemPool = null,
)
{
$this->configs = $configs;
$this->compiler = $compiler;
$this->cacheItemPool = $cacheItemPool;
self::$locator = $this;
}
/**
* Gets configs
*
* @return ConfigInterface
*/
public function config(): ConfigInterface
{
return $this->configs;
}
/**
* Gets compiler
*
* @return CompilerInterface
*/
public function compiler(): CompilerInterface
{
return $this->compiler;
}
/**
* Runs config compilation
*
* @param bool $forcedRebuild
* If true rebuilds configuration every new run
*
* @return array
*
* @throws ConfigureException
*/
public function run(bool $forcedRebuild = false): array
{
$out = $this->getFromCache($forcedRebuild);
if (isset($out))
return $out;
$out = $this->compiler->compile($this->configs->main());
if (!$forcedRebuild)
$this->saveInCache($out);
$this->freeMem();
return $out;
}
/**
* Gets configuration from cache
*
* @param bool $forcedRebuild
*
* @return array|null
*
* @throws ConfigureException
*/
private function getFromCache(bool $forcedRebuild = false): ?array
{
try {
if (!isset($this->cacheItemPool))
return null;
$cacheItem = $this->cacheItemPool->getItem(self::CACHE);
if ($cacheItem->isHit() && !$forcedRebuild)
return $cacheItem->get();
} catch (InvalidArgumentException $e) {
throw new ConfigureException($e->getMessage(), $e->getCode(), $e);
}
return null;
}
/**
* Saves out configuration cache
*
* @param array $configuration
*
* @throws ConfigureException
*/
private function saveInCache(array $configuration): void
{
try {
if (!isset($this->cacheItemPool))
return;
$cacheItem = $this->cacheItemPool->getItem(self::CACHE);
$cacheItem->set($configuration);
$this->cacheItemPool->save($cacheItem);
} catch (InvalidArgumentException $e) {
throw new ConfigureException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* Free memory
*/
private function freeMem(): void
{
self::$locator = null;
}
}