@@ -28,10 +28,6 @@ class ResponseRenderer
2828{
2929 private static ResponseRenderer |null $ instance = null ;
3030
31- /**
32- * Header instance
33- */
34- protected Header $ header ;
3531 /**
3632 * HTML data to be used in the response
3733 */
@@ -43,10 +39,7 @@ class ResponseRenderer
4339 * @var mixed[]
4440 */
4541 private array $ JSON = [];
46- /**
47- * PhpMyAdmin\Footer instance
48- */
49- protected Footer $ footer ;
42+
5043 /**
5144 * Whether we are servicing an ajax request.
5245 */
@@ -136,24 +129,16 @@ class ResponseRenderer
136129
137130 protected Response $ response ;
138131
139- protected Template $ template ;
140- protected Config $ config ;
141-
142- private function __construct ()
143- {
144- $ this ->config = Config::getInstance ();
145- $ this ->template = new Template ();
146- $ dbi = DatabaseInterface::getInstance ();
147- $ relation = new Relation ($ dbi );
148- $ this ->header = new Header (
149- $ this ->template ,
150- new Console ($ relation , $ this ->template , new BookmarkRepository ($ dbi , $ relation )),
151- $ this ->config ,
152- );
153- $ this ->footer = new Footer ($ this ->template , $ this ->config );
154- $ this ->response = ResponseFactory::create ()->createResponse ();
155-
156- $ this ->setAjax (! empty ($ _REQUEST ['ajax_request ' ]));
132+ protected function __construct (
133+ protected Config $ config ,
134+ protected Template $ template ,
135+ protected Header $ header ,
136+ protected Footer $ footer ,
137+ protected ErrorHandler $ errorHandler ,
138+ protected DatabaseInterface $ dbi ,
139+ ResponseFactory $ responseFactory ,
140+ ) {
141+ $ this ->response = $ responseFactory ->createResponse (StatusCodeInterface::STATUS_OK , 'OK ' );
157142 }
158143
159144 /**
@@ -172,10 +157,26 @@ public function setAjax(bool $isAjax): void
172157 */
173158 public static function getInstance (): ResponseRenderer
174159 {
175- if (self ::$ instance = == null ) {
176- self ::$ instance = new ResponseRenderer () ;
160+ if (self ::$ instance ! == null ) {
161+ return self ::$ instance ;
177162 }
178163
164+ $ config = Config::getInstance ();
165+ $ template = new Template ($ config );
166+ $ dbi = DatabaseInterface::getInstance ();
167+ $ relation = new Relation ($ dbi );
168+ $ console = new Console ($ relation , $ template , new BookmarkRepository ($ dbi , $ relation ));
169+
170+ self ::$ instance = new ResponseRenderer (
171+ $ config ,
172+ $ template ,
173+ new Header ($ template , $ console , $ config ),
174+ new Footer ($ template , $ config ),
175+ ErrorHandler::getInstance (),
176+ $ dbi ,
177+ ResponseFactory::create (),
178+ );
179+
179180 return self ::$ instance ;
180181 }
181182
@@ -271,7 +272,7 @@ private function ajaxResponse(): string
271272 $ this ->addJSON ('title ' , '<title> ' . $ this ->getHeader ()->getPageTitle () . '</title> ' );
272273 }
273274
274- if (DatabaseInterface:: getInstance () ->isConnected ()) {
275+ if ($ this -> dbi ->isConnected ()) {
275276 $ this ->addJSON ('menu ' , $ this ->getHeader ()->getMenu ()->getDisplay ());
276277 }
277278
@@ -289,7 +290,7 @@ private function ajaxResponse(): string
289290 $ this ->addJSON ('errors ' , $ errors );
290291 }
291292
292- $ promptPhpErrors = ErrorHandler:: getInstance () ->hasErrorsForPrompt ();
293+ $ promptPhpErrors = $ this -> errorHandler ->hasErrorsForPrompt ();
293294 $ this ->addJSON ('promptPhpErrors ' , $ promptPhpErrors );
294295
295296 if (empty ($ GLOBALS ['error_message ' ])) {
@@ -317,12 +318,6 @@ private function ajaxResponse(): string
317318 }
318319 }
319320
320- // Set the Content-Type header to JSON so that jQuery parses the
321- // response correctly.
322- foreach (Core::headerJSON () as $ name => $ value ) {
323- $ this ->addHeader ($ name , $ value );
324- }
325-
326321 $ result = json_encode ($ this ->JSON );
327322 if ($ result === false ) {
328323 return (string ) json_encode ([
@@ -336,9 +331,19 @@ private function ajaxResponse(): string
336331
337332 public function response (): Response
338333 {
339- $ this ->response ->getBody ()->write ($ this ->isAjax () ? $ this ->ajaxResponse () : $ this ->getDisplay ());
334+ if ($ this ->isAjax ()) {
335+ $ headers = Core::headerJSON ();
336+ $ body = $ this ->ajaxResponse ();
337+ } else {
338+ $ headers = $ this ->header ->getHttpHeaders ();
339+ $ body = $ this ->getDisplay ();
340+ }
341+
342+ foreach ($ headers as $ name => $ value ) {
343+ $ this ->response = $ this ->response ->withHeader ($ name , $ value );
344+ }
340345
341- return $ this ->response ;
346+ return $ this ->response -> write ( $ body ) ;
342347 }
343348
344349 public function addHeader (string $ name , string $ value ): void
0 commit comments