<?php namespace demo\Middleware; use Silex\Application; use Symfony\Component\HttpFoundation\JsonResponse; use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; class AfterAll { /** * Arguments for before() middleware are fixed, unlike those of controllers. * * @param \Symfony\Component\HttpFoundation\Request $request * @param \Symfony\Component\HttpFoundation\Response $response * @param \Silex\Application $app * * @return \Symfony\Component\HttpFoundation\Response|void */ public function __invoke(Request $request, Response $response, Application $app) { $MESSAGE = "<p>" . $app['timer']->delay() .": In aAM</p>\n"; /* Note how content added to the response like this looks as if it had been added after the echo'ed content in the global middleware. Compare the timestamps for sequence */ if ($response instanceof JsonResponse) { $content = $response->getContent(); $raw = json_decode($content, TRUE); $raw[] = $MESSAGE; $newContent = json_encode($raw); $response->setContent($newContent); return $response; } // Only echo info on text responses. $ct = $response->headers->get('Content-Type'); if (isset($ct) && strpos($ct, 'text') !== 0) { return; } echo $MESSAGE; flush(); } }