for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/**
* @author jan huang <[email protected]>
* @copyright 2016
*
* @see https://www.github.com/janhuang
* @see http://www.fast-d.cn/
*/
namespace FastD\Middleware;
use FastD\Packet\Json;
use FastD\Utils\DateObject;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
* Class CacheMiddleware.
class CacheMiddleware extends Middleware
{
* @param ServerRequestInterface $request
* @param DelegateInterface $next
* @return ResponseInterface
public function handle(ServerRequestInterface $request, DelegateInterface $next)
$action = $request->getMethod();
if ('GET' !== $action) {
return $next->next($request);
}
$key = md5($request->getUri()->getPath());
$cache = cache()->getItem($key);
if ($cache->isHit()) {
$value = Json::decode($cache->get());
return json($value)
$value
string
array
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example:
function acceptsInteger($int) { } $x = '123'; // string "123" // Instead of acceptsInteger($x); // we recommend to use acceptsInteger((integer) $x);
->withHeader('X-Cache', $key);
$response = $next->next($request);
$cache->set((string) $response->getBody());
$expireAt = DateObject::createFromTimestamp(time() + config()->get('common.cache.lifetime', 60));
$cache->expiresAt($expireAt);
cache()->save($cache);
return $response->withExpires($expireAt);
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: