Skip to content
This repository was archived by the owner on May 14, 2020. It is now read-only.

Commit 6f14a06

Browse files
committed
Fixed configuration reload.
1 parent 425d9c4 commit 6f14a06

File tree

3 files changed

+101
-42
lines changed

3 files changed

+101
-42
lines changed

src/Application.php

Lines changed: 89 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ final class Application
4040
private $header;
4141

4242
/**
43-
* @var \StaticServer\Processor\ProcessorInterface|\StaticServer\Generic\ClearCacheInterface
43+
* @var \StaticServer\Processor\ProcessorInterface|\StaticServer\Generic\ClearCacheInterface|ConfigurationAwareInterface
4444
*/
4545
private $processor;
4646

@@ -82,7 +82,7 @@ public function __construct(string $stage = '', string $sha1 = '')
8282
$this->cpuNum = swoole_cpu_num() * 2;
8383
$this->conf = $this->loadConfiguration();
8484

85-
$this->header = new Header($this->conf);
85+
$this->header = new Header();
8686

8787
// disable logs and modifiers by default
8888
$this->setLogger(new NullLogger());
@@ -100,10 +100,6 @@ public function __construct(string $stage = '', string $sha1 = '')
100100
*/
101101
public function setProcessor(ProcessorInterface $processor): void
102102
{
103-
if ($processor instanceof ConfigurationAwareInterface) {
104-
$processor->setConfiguration($this->conf);
105-
}
106-
107103
$this->processor = $processor;
108104
}
109105

@@ -116,10 +112,6 @@ public function setProcessor(ProcessorInterface $processor): void
116112
*/
117113
public function setIterator(IteratorInterface $iterator): void
118114
{
119-
if ($iterator instanceof ConfigurationAwareInterface) {
120-
$iterator->setConfiguration($this->conf);
121-
}
122-
123115
$this->iterator = $iterator;
124116
}
125117

@@ -132,10 +124,6 @@ public function setIterator(IteratorInterface $iterator): void
132124
*/
133125
public function setModifier(GenericModifyInterface $modify): void
134126
{
135-
if ($modify instanceof ConfigurationAwareInterface) {
136-
$modify->setConfiguration($this->conf);
137-
}
138-
139127
$this->modify = $modify;
140128
}
141129

@@ -191,18 +179,9 @@ private function ready(): void
191179
$this->logger->debug('Reload configuration');
192180
$this->conf = $this->loadConfiguration();
193181

194-
$this->logger->debug('Clears headers cache and prepare theirs');
195-
$this->header->clearCache();
196-
$this->header->prepare();
197-
198-
if ($this->processor instanceof ClearCacheInterface) {
199-
$this->logger->debug(sprintf('Clears cache for handled files. Used: [%s]', get_class($this->processor)));
200-
$this->processor->clearCache();
201-
}
202-
203-
if ($this->processor instanceof PrepareInterface) {
204-
$this->processor->prepare();
205-
}
182+
$this->clearObjectsCaches();
183+
$this->putConfigurationToObjects();
184+
$this->prepareObjectsBeforeAcceptRequests();
206185

207186
$this->logger->debug('Iterates files from server root dir');
208187
$files = $this->iterator->iterate();
@@ -214,6 +193,90 @@ private function ready(): void
214193
$this->processor->load($modified);
215194
}
216195

196+
/**
197+
* Prepares objects if object support it.
198+
*
199+
* @return void
200+
*/
201+
private function prepareObjectsBeforeAcceptRequests(): void
202+
{
203+
if ($this->header instanceof PrepareInterface) {
204+
$this->logger->debug(sprintf('Prepare object before accept requests: %s', get_class($this->header)));
205+
$this->header->prepare();
206+
}
207+
208+
if ($this->iterator instanceof PrepareInterface) {
209+
$this->logger->debug(sprintf('Prepare object before accept requests: %s', get_class($this->iterator)));
210+
$this->iterator->prepare();
211+
}
212+
213+
if ($this->modify instanceof PrepareInterface) {
214+
$this->logger->debug(sprintf('Prepare object before accept requests: %s', get_class($this->modify)));
215+
$this->modify->prepare();
216+
}
217+
218+
if ($this->processor instanceof PrepareInterface) {
219+
$this->logger->debug(sprintf('Prepare object before accept requests: %s', get_class($this->processor)));
220+
$this->processor->prepare();
221+
}
222+
}
223+
224+
/**
225+
* Clear objects caches if it supports.
226+
*
227+
* @return void
228+
*/
229+
private function clearObjectsCaches(): void
230+
{
231+
if ($this->header instanceof ClearCacheInterface) {
232+
$this->logger->debug(sprintf('Clear cache for: %s', get_class($this->header)));
233+
$this->header->clearCache();
234+
}
235+
236+
if ($this->iterator instanceof ClearCacheInterface) {
237+
$this->logger->debug(sprintf('Clear cache for: %s', get_class($this->iterator)));
238+
$this->iterator->clearCache();
239+
}
240+
241+
if ($this->modify instanceof ClearCacheInterface) {
242+
$this->logger->debug(sprintf('Clear cache for: %s', get_class($this->modify)));
243+
$this->modify->clearCache();
244+
}
245+
246+
if ($this->processor instanceof ClearCacheInterface) {
247+
$this->logger->debug(sprintf('Clear cache for: %s', get_class($this->processor)));
248+
$this->processor->clearCache();
249+
}
250+
}
251+
252+
/**
253+
* Put config to objects if it supports.
254+
*
255+
* @return void
256+
*/
257+
private function putConfigurationToObjects(): void
258+
{
259+
if ($this->header instanceof ConfigurationAwareInterface) {
260+
$this->logger->debug(sprintf('Put configuration to: %s', get_class($this->header)));
261+
$this->header->setConfiguration($this->conf);
262+
}
263+
264+
if ($this->iterator instanceof ConfigurationAwareInterface) {
265+
$this->logger->debug(sprintf('Put configuration to: %s', get_class($this->iterator)));
266+
$this->iterator->setConfiguration($this->conf);
267+
}
268+
269+
if ($this->modify instanceof ConfigurationAwareInterface) {
270+
$this->logger->debug(sprintf('Put configuration to: %s', get_class($this->modify)));
271+
$this->modify->setConfiguration($this->conf);
272+
}
273+
274+
if ($this->processor instanceof ConfigurationAwareInterface) {
275+
$this->logger->debug(sprintf('Put configuration to: %s', get_class($this->processor)));
276+
$this->processor->setConfiguration($this->conf);
277+
}
278+
}
279+
217280
/**
218281
* @return \Swoole\Http\Server
219282
*/

src/Header.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
namespace StaticServer;
44

55
use InvalidArgumentException;
6-
use Microparts\Configuration\ConfigurationInterface;
6+
use Microparts\Configuration\ConfigurationAwareInterface;
7+
use Microparts\Configuration\ConfigurationAwareTrait;
78
use StaticServer\Generic\ClearCacheInterface;
89
use StaticServer\Generic\PrepareInterface;
910
use Swoole\Http\Response;
1011

11-
final class Header implements PrepareInterface, ClearCacheInterface
12+
final class Header implements PrepareInterface, ClearCacheInterface, ConfigurationAwareInterface
1213
{
14+
use ConfigurationAwareTrait;
15+
1316
/**
1417
* Map headers names in config to real http headers names
1518
*/
@@ -40,16 +43,6 @@ final class Header implements PrepareInterface, ClearCacheInterface
4043
*/
4144
private $prepared = [];
4245

43-
/**
44-
* Header constructor.
45-
*
46-
* @param \Microparts\Configuration\ConfigurationInterface $conf
47-
*/
48-
public function __construct(ConfigurationInterface $conf)
49-
{
50-
$this->conf = $conf;
51-
}
52-
5346
/**
5447
* Prepare headers before handling requests.
5548
*
@@ -59,7 +52,7 @@ public function __construct(ConfigurationInterface $conf)
5952
*/
6053
public function prepare(): void
6154
{
62-
foreach ($this->conf->get('server.headers') as $header => $values) {
55+
foreach ($this->configuration->get('server.headers') as $header => $values) {
6356
if (!isset(self::CONFIG_MAP[$header])) {
6457
throw new InvalidArgumentException('Header not supported.');
6558
}

tests/HeaderTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ public function testSendingHeaders()
2020
->method('header')
2121
->willReturn(null);
2222

23-
$h = new Header($conf);
23+
$h = new Header();
24+
$h->setConfiguration($conf);
2425
$h->prepare();
2526
$h->sent($response);
2627

@@ -40,7 +41,8 @@ public function testHowInvalidHeaderNameCheckWorks()
4041
->method('header')
4142
->willReturn(null);
4243

43-
$h = new Header($conf);
44+
$h = new Header();
45+
$h->setConfiguration($conf);
4446
$h->prepare();
4547
$h->sent($response);
4648

@@ -60,7 +62,8 @@ public function testHowInvalidHeaderValueCheckWorks()
6062
->method('header')
6163
->willReturn(null);
6264

63-
$h = new Header($conf);
65+
$h = new Header();
66+
$h->setConfiguration($conf);
6467
$h->prepare();
6568
$h->sent($response);
6669

0 commit comments

Comments
 (0)