public function __invoke(Route $route, Console $console) : int { $basePath = $route->getMatchedParam('path'); $path = realpath($basePath) . '/data/blog'; $cache = realpath($basePath) . '/data/cache/posts'; $baseUri = new Uri('https://mwop.net'); $middleware = $this->blogMiddleware; $console->writeLine('Generating static cache for blog posts', Color::GREEN); // Prepare final handler for middleware $failed = false; $done = function ($req, $res, $err = null) use(&$failed) { $failed = $err ? true : false; }; $parser = new Parser(null, new CommonMarkParser()); foreach (new MarkdownFileFilter($path) as $fileInfo) { $document = $parser->parse(file_get_contents($fileInfo->getPathname())); $metadata = $document->getYAML(); $message = ' ' . $metadata['id']; $length = strlen($message); $width = $console->getWidth(); $console->write($message, Color::BLUE); $canonical = $baseUri->withPath(sprintf('/blog/%s.html', $metadata['id'])); $request = (new Request(new PsrRequest([], [], $canonical, 'GET')))->withUri($canonical)->withAttribute('id', $metadata['id']); $failed = false; $response = $middleware($request, new Response(), $done); if (!$failed) { $this->cacheResponse($metadata['id'], $cache, $response->getBody()); } $this->reportComplete($console, $width, $length, !$failed); } $console->writeLine('ALL DONE', Color::GREEN); return 0; }