4545use GT \WebEngine \View \ViewStreamer ;
4646use Throwable ;
4747
48+ /**
49+ * @SuppressWarnings("PHPMD.TooManyFields")
50+ * @SuppressWarnings("PHPMD.ExcessiveClassComplexity")
51+ */
4852class Dispatcher {
4953 private Config $ config ;
5054 private Request $ request ;
@@ -70,12 +74,15 @@ class Dispatcher {
7074 private ViewStreamer $ viewStreamer ;
7175
7276 private HeaderManager $ headerManager ;
73- private Closure $ viewModelInitCallback ;
77+ private Closure $ viewInitCb ;
7478 private bool $ redirectPrepared = false ;
7579
7680 /**
7781 * @param array<string, array<string, string|array<string, string>>> $globals
82+ * @SuppressWarnings("PHPMD.ExcessiveMethodLength")
83+ * @SuppressWarnings("PHPMD.ExcessiveParameterList")
7884 */
85+ // phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
7986 public function __construct (
8087 Config $ config ,
8188 Request $ request ,
@@ -190,7 +197,7 @@ public function __construct(
190197 $ this ->config ->getString ("view.partial_directory " ),
191198 );
192199 $ this ->viewModelProcessor = $ viewModelInit ->getViewModelProcessor ();
193- $ this ->viewModelInitCallback = $ this ->viewModel instanceof HTMLDocument
200+ $ this ->viewInitCb = $ this ->viewModel instanceof HTMLDocument
194201 ? function ()use ($ viewModelInit ):void {
195202 $ documentBinder = $ this ->serviceContainer ->get (Binder::class);
196203 assert ($ documentBinder instanceof DocumentBinder);
@@ -215,6 +222,7 @@ public function __construct(
215222 $ this ->viewStreamer = $ viewStreamer ?? new ViewStreamer ();
216223 $ this ->headerManager = $ headerManager ?? new HeaderManager ();
217224 }
225+ // phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
218226
219227 public function generateResponse ():Response {
220228 if ($ this ->redirectPrepared ) {
@@ -251,7 +259,7 @@ public function generateErrorResponse(Throwable $throwable):Response {
251259 throw new ErrorPageNotFoundException (code: $ errorStatusCode );
252260 }
253261
254- $ this ->processResponse (true , $ throwable );
262+ $ this ->processResponse ($ throwable );
255263 $ this ->response = $ this ->response ->withStatus ($ errorStatusCode );
256264 return $ this ->response ;
257265 }
@@ -273,7 +281,8 @@ public function generateBasicErrorResponse(
273281 $ errorMessage = "The server could not find the requested resource. " ;
274282
275283 if (!$ this ->config ->getBool ("app.production " )) {
276- $ detail .= " Additionally, there was no error page found in your application at <strong> $ errorPageDir/ $ errorStatusCode.html</strong> " ;
284+ $ detail .= " Additionally, there was no error page found in your "
285+ . "application at <strong> $ errorPageDir/ $ errorStatusCode.html</strong> " ;
277286 }
278287 }
279288 }
@@ -306,11 +315,13 @@ public function generateBasicErrorResponse(
306315
307316 $ body = new Stream ();
308317 $ body ->write ($ html );
309- return new Response (request: $ this ->request )->withBody ($ body )->withStatus ($ errorStatusCode );
318+ $ response = new Response (null , null , $ this ->request );
319+ $ response = $ response ->withBody ($ body );
320+ return $ response ->withStatus ($ errorStatusCode );
310321 }
311322
312323 private function setupResponse ():Response {
313- $ response = new Response (request: $ this ->request );
324+ $ response = new Response (null , null , $ this ->request );
314325 $ response ->setExitCallback (function () {
315326 ($ this ->finishCallback )($ this ->response );
316327 });
@@ -343,7 +354,8 @@ private function handleLogicExecution(
343354 }
344355
345356 foreach ($ this ->logicExecutor ->invoke ($ logicAssembly , "go_before " , $ extraArgs ) as $ file ) {
346- // TODO: Hook up to debug output
357+ // Force generator execution even when debug output is disabled.
358+ continue ;
347359 }
348360
349361// TODO: No need to have the whole Input class. Just pass a nullable string in called $doMethod, from $input->getString("do")
@@ -356,25 +368,28 @@ function(InputData $data)use($logicAssembly, $extraArgs) {
356368 );
357369
358370 foreach ($ this ->logicExecutor ->invoke ($ logicAssembly , $ doName , $ extraArgs ) as $ file ) {
359- // TODO: Hook up to debug output
371+ // Force generator execution even when debug output is disabled.
372+ continue ;
360373 }
361374 }
362375 );
363376
364377 foreach ($ this ->logicExecutor ->invoke ($ logicAssembly , "go " , $ extraArgs ) as $ file ) {
365- // TODO: Hook up to debug output
378+ // Force generator execution even when debug output is disabled.
379+ continue ;
366380 }
367381
368382 foreach ($ this ->logicExecutor ->invoke ($ logicAssembly , "go_after " , $ extraArgs ) as $ file ) {
369- // TODO: Hook up to debug output
383+ // Force generator execution even when debug output is disabled.
384+ continue ;
370385 }
371386 }
372387
373388 /**
374389 * @return void
375390 */
391+ // phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
376392 public function processResponse (
377- bool $ processingError = false ,
378393 ?Throwable $ errorThrowable = null ,
379394 ):void {
380395 $ dynamicPath = $ this ->serviceContainer ->get (DynamicPath::class);
@@ -384,19 +399,19 @@ public function processResponse(
384399 $ dynamicPath ,
385400 );
386401
387- $ logicAssemblyComponentList = $ this ->viewModelProcessor ?->processPartialContent(
402+ $ componentList = $ this ->viewModelProcessor ?->processPartialContent(
388403 $ this ->viewModel ,
389404 );
390405
391406// TODO: CSRF handling - needs to be done on any POST request.
392- ($ this ->viewModelInitCallback )();
393- if ($ processingError && $ errorThrowable ) {
407+ ($ this ->viewInitCb )();
408+ if ($ errorThrowable ) {
394409 $ this ->bindErrorDetails ($ errorThrowable );
395410 }
396411
397- foreach ($ logicAssemblyComponentList ?? [] as $ logicAssemblyComponent ) {
398- $ assembly = $ logicAssemblyComponent ->assembly ;
399- $ componentElement = $ logicAssemblyComponent ->component ;
412+ foreach ($ componentList ?? [] as $ componentLogic ) {
413+ $ assembly = $ componentLogic ->assembly ;
414+ $ componentElement = $ componentLogic ->component ;
400415 $ this ->serviceContainer ->set ($ componentElement );
401416
402417 try {
@@ -407,7 +422,7 @@ public function processResponse(
407422 );
408423 }
409424 catch (Throwable $ throwable ) {
410- if (!$ processingError ) {
425+ if (!$ errorThrowable ) {
411426 throw $ throwable ;
412427 }
413428 }
@@ -420,7 +435,7 @@ public function processResponse(
420435 );
421436 }
422437 catch (Throwable $ throwable ) {
423- if (!$ processingError ) {
438+ if (!$ errorThrowable ) {
424439 throw $ throwable ;
425440 }
426441 }
@@ -438,6 +453,7 @@ public function processResponse(
438453
439454 $ this ->viewStreamer ->stream ($ this ->view , $ this ->viewModel );
440455 }
456+ // phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
441457
442458 public function getSessionInit ():?SessionInit {
443459 return $ this ->sessionInit ;
@@ -452,6 +468,7 @@ private function isRedirectPrepared():bool {
452468 return $ this ->response ->hasHeader ("Location " );
453469 }
454470
471+ // phpcs:disable Generic.Metrics.CyclomaticComplexity.TooHigh
455472 private function bindErrorDetails (Throwable $ throwable ):void {
456473 $ trace = $ throwable ->getTrace ();
457474 array_unshift ($ trace , [
@@ -498,4 +515,5 @@ private function bindErrorDetails(Throwable $throwable):void {
498515 $ binder ->bindKeyValue ("trace " , $ traceString );
499516 }
500517 }
518+ // phpcs:enable Generic.Metrics.CyclomaticComplexity.TooHigh
501519}
0 commit comments