Skip to content

Commit

Permalink
Fixed blog post caching
Browse files Browse the repository at this point in the history
- Some incorrect logic was preventing caching of posts.
- The console tooling needed updating to use the ServiceManager instead of the
  homegrown service implementation.
- The CachePosts console tooling needed updating to use the DisplayPost
  middleware, and to set the 'id' attribute on the request before invoking it.
- Removed the obsolete `Blog\CachingMiddlewareFactory` and `Blog\Middleware`
  classes.
  • Loading branch information
weierophinney committed Aug 10, 2015
1 parent 5156176 commit a0c8ea5
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .gitignore
@@ -1,5 +1,5 @@
config/autoload/*local.php
data/cache/posts/*.html
data/cache/posts/*
data/comics/*.html
data/comics.mustache
data/feeds/*.xml
Expand Down
7 changes: 6 additions & 1 deletion bin/mwop.net.php
Expand Up @@ -5,15 +5,20 @@
use Zend\Console\Console;
use Zend\Feed\Reader\Reader as FeedReader;
use Zend\Http\Client as HttpClient;
use Zend\ServiceManager\Config;
use Zend\ServiceManager\ServiceManager;
use ZF\Console\Application;

chdir(__DIR__ . '/../');
error_reporting(-1);
ini_set('display_errors', 1);
require_once 'vendor/autoload.php';

define('VERSION', '0.0.1');

$config = include 'config/config.php';
$services = createServiceContainer($config);
$services = new ServiceManager(new Config($config['services']));
$services->setService('Config', $config);

$routes = [
[
Expand Down
2 changes: 1 addition & 1 deletion config/autoload/global.php
Expand Up @@ -102,7 +102,7 @@
],
],
'services' => [
'delegator_factories' => [
'delegators' => [
'Mwop\Blog\DisplayPostMiddleware' => [
'Mwop\Blog\CachingDelegatorFactory',
],
Expand Down
2 changes: 1 addition & 1 deletion src/Blog/CachingMiddleware.php
Expand Up @@ -54,7 +54,7 @@ public function __invoke($req, $res, $next)
}

// Result represents an error response; cannot cache.
if (300 >= $result->getStatusCode()) {
if (300 <= $result->getStatusCode()) {
return $result;
}

Expand Down
16 changes: 0 additions & 16 deletions src/Blog/CachingMiddlewareFactory.php

This file was deleted.

16 changes: 0 additions & 16 deletions src/Blog/Middleware.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/CachePosts.php
Expand Up @@ -11,7 +11,7 @@ class CachePosts
{
private $blogMiddleware;

public function __construct(Blog\Middleware $blog)
public function __construct(callable $blog)
{
$this->blogMiddleware = $blog;
}
Expand Down Expand Up @@ -45,10 +45,9 @@ public function __invoke($route, $console)
$console->write($message, Color::BLUE);

$canonical = $baseUri->withPath(sprintf('/blog/%s.html', $entry->getId()));
$request = new Request(new PsrRequest([], [], $canonical, 'GET'));

$uri = $canonical->withPath(sprintf('/%s.html', $entry->getId()));
$request = $request->withUri($uri);
$request = (new Request(new PsrRequest([], [], $canonical, 'GET')))
->withUri($canonical)
->withAttribute('id', $entry->getId());

$failed = false;
$middleware($request, new Response(), $done);
Expand Down
2 changes: 1 addition & 1 deletion src/Factory/CachePosts.php
Expand Up @@ -7,6 +7,6 @@ class CachePosts
{
public function __invoke($services)
{
return new Command($services->get('Mwop\Blog\Middleware'));
return new Command($services->get('Mwop\Blog\DisplayPostMiddleware'));
}
}

0 comments on commit a0c8ea5

Please sign in to comment.