@@ -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 */
0 commit comments