AfterAll.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace demo\Middleware;
  3. use Silex\Application;
  4. use Symfony\Component\HttpFoundation\JsonResponse;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Symfony\Component\HttpFoundation\Response;
  7. class AfterAll {
  8. /**
  9. * Arguments for before() middleware are fixed, unlike those of controllers.
  10. *
  11. * @param \Symfony\Component\HttpFoundation\Request $request
  12. * @param \Symfony\Component\HttpFoundation\Response $response
  13. * @param \Silex\Application $app
  14. *
  15. * @return \Symfony\Component\HttpFoundation\Response|void
  16. */
  17. public function __invoke(Request $request, Response $response, Application $app) {
  18. $MESSAGE = "<p>" . $app['timer']->delay() .": In aAM</p>\n";
  19. /* Note how content added to the response like this looks as if it had been
  20. added after the echo'ed content in the global middleware. Compare the
  21. timestamps for sequence */
  22. if ($response instanceof JsonResponse) {
  23. $content = $response->getContent();
  24. $raw = json_decode($content, TRUE);
  25. $raw[] = $MESSAGE;
  26. $newContent = json_encode($raw);
  27. $response->setContent($newContent);
  28. return $response;
  29. }
  30. // Only echo info on text responses.
  31. $ct = $response->headers->get('Content-Type');
  32. if (isset($ct) && strpos($ct, 'text') !== 0) {
  33. return;
  34. }
  35. echo $MESSAGE;
  36. flush();
  37. }
  38. }