Skip to content

Commit c273a02

Browse files
committed
Update typehints.
ServerRequestInterface::getData() and ResponseInterface::withCookie() don't exist.
1 parent b096744 commit c273a02

File tree

2 files changed

+148
-18
lines changed

2 files changed

+148
-18
lines changed

src/Cache/SimpleCache.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
<?php
2+
namespace Cake\Cache;
3+
4+
use Psr\SimpleCache\CacheInterface;
5+
use Psr\SimpleCache\InvalidArgumentException;
6+
use Cake\Cache\CacheEngine;
7+
8+
class SimpleCache implements CacheInterface
9+
{
10+
/**
11+
* @var \Cake\Cache\CacheEngine
12+
*/
13+
protected $engine;
14+
15+
public function __construct(CacheEngine $engine)
16+
{
17+
$this->engine = $engine;
18+
}
19+
20+
public function get($key, $default = null)
21+
{
22+
$result = $engine->read($key);
23+
24+
return $result === false ? $default : $result;
25+
}
26+
27+
public function set($key, $value, $ttl = null)
28+
{
29+
if ($ttl !== null) {
30+
$duration = $this->engine->getConfig('duration');
31+
$this->engine->setConfig('duration', $this->ttlToSeconds($ttl));
32+
}
33+
34+
try {
35+
$result = $this->engine->write($key, $value);
36+
} finally {
37+
if ($ttl !== null) {
38+
$this->engine->setConfig('duration', $duration);
39+
}
40+
}
41+
42+
return $result;
43+
}
44+
45+
public function delete($key)
46+
{
47+
return $this->engine->delete($key);
48+
}
49+
50+
public function clear()
51+
{
52+
return $this->engine->clear(false);
53+
}
54+
55+
public function getMultiple($keys, $default = null)
56+
{
57+
$keys = $this->getAsArray($keys);
58+
$result = [];
59+
60+
foreach ($keys as $key) {
61+
$value = $this->engine->get($key, $default);
62+
}
63+
64+
return $result;
65+
}
66+
67+
public function setMultiple($values, $ttl = null)
68+
{
69+
if ($ttl !== null) {
70+
$duration = $this->engine->getConfig('duration');
71+
$this->engine->setConfig('duration', $this->ttlToSeconds($ttl));
72+
}
73+
74+
try {
75+
$result = true;
76+
foreach ($values as $key => $value) {
77+
$result = $this->engine->set($key, $value) && $result;
78+
}
79+
} finally {
80+
if ($ttl !== null) {
81+
$this->engine->setConfig('duration', $duration);
82+
}
83+
}
84+
85+
return $result;
86+
}
87+
88+
public function deleteMultiple($keys)
89+
{
90+
$keys = $this->getAsArray($keys);
91+
92+
$result = true;
93+
foreach ($keys as $key) {
94+
$result = $this->engine->delete($key) && $result;
95+
}
96+
97+
return $result;
98+
}
99+
100+
public function has($key)
101+
{
102+
return $this->get($key) === null ? false : true;
103+
}
104+
105+
protected function getAsArray($keys)
106+
{
107+
if ($keys instanceof \Traversable) {
108+
return iterator_to_array($keys);
109+
}
110+
111+
if (is_array($keys)) {
112+
return $keys;
113+
}
114+
115+
throw new InvalidArgumentException('"$keys" must be an array or instanceof Traversable');
116+
}
117+
118+
/**
119+
* @param int|\DateInterval $ttl
120+
* @return int seconds
121+
*/
122+
function ttlToSeconds($ttl)
123+
{
124+
if (is_int($ttl)) {
125+
return $ttl;
126+
}
127+
128+
return $ttl->days * 86400 + $ttl->h * 3600 + $ttl->i * 60 + $ttl->s;
129+
}
130+
}

src/Http/Middleware/CsrfProtectionMiddleware.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
*/
1515
namespace Cake\Http\Middleware;
1616

17+
use Cake\Http\Response;
18+
use Cake\Http\ServerRequest;
1719
use Cake\I18n\Time;
1820
use Cake\Network\Exception\InvalidCsrfTokenException;
1921
use Cake\Utility\Hash;
2022
use Cake\Utility\Security;
21-
use Psr\Http\Message\ResponseInterface;
22-
use Psr\Http\Message\ServerRequestInterface;
2323

2424
/**
2525
* Provides CSRF protection & validation.
@@ -77,12 +77,12 @@ public function __construct(array $config = [])
7777
/**
7878
* Checks and sets the CSRF token depending on the HTTP verb.
7979
*
80-
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
81-
* @param \Psr\Http\Message\ResponseInterface $response The response.
80+
* @param \Cake\Http\ServerRequest $request The request.
81+
* @param \Cake\Http\Response $response The response.
8282
* @param callable $next Callback to invoke the next middleware.
83-
* @return \Psr\Http\Message\ResponseInterface A response
83+
* @return \Cake\Http\Response A response
8484
*/
85-
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, $next)
85+
public function __invoke(ServerRequest $request, Response $response, $next)
8686
{
8787
$cookies = $request->getCookieParams();
8888
$cookieData = Hash::get($cookies, $this->_config['cookieName']);
@@ -109,10 +109,10 @@ public function __invoke(ServerRequestInterface $request, ResponseInterface $res
109109
/**
110110
* Checks if the request is POST, PUT, DELETE or PATCH and validates the CSRF token
111111
*
112-
* @param \Psr\Http\Message\ServerRequestInterface $request The request object.
113-
* @return \Psr\Http\Message\ServerRequestInterface
112+
* @param \Cake\Http\ServerRequest $request The request object.
113+
* @return \Cake\Http\ServerRequest
114114
*/
115-
protected function _validateAndUnsetTokenField(ServerRequestInterface $request)
115+
protected function _validateAndUnsetTokenField(ServerRequest $request)
116116
{
117117
if (in_array($request->getMethod(), ['PUT', 'POST', 'DELETE', 'PATCH']) || $request->getData()) {
118118
$this->_validateToken($request);
@@ -140,10 +140,10 @@ protected function _createToken()
140140
* Add a CSRF token to the request parameters.
141141
*
142142
* @param string $token The token to add.
143-
* @param \Psr\Http\Message\ServerRequestInterface $request The request to augment
144-
* @return \Psr\Http\Message\ServerRequestInterface Modified request
143+
* @param \Cake\Http\ServerRequest $request The request to augment
144+
* @return \Cake\Http\ServerRequest Modified request
145145
*/
146-
protected function _addTokenToRequest($token, ServerRequestInterface $request)
146+
protected function _addTokenToRequest($token, ServerRequest $request)
147147
{
148148
$params = $request->getAttribute('params');
149149
$params['_csrfToken'] = $token;
@@ -155,11 +155,11 @@ protected function _addTokenToRequest($token, ServerRequestInterface $request)
155155
* Add a CSRF token to the response cookies.
156156
*
157157
* @param string $token The token to add.
158-
* @param \Psr\Http\Message\ServerRequestInterface $request The request to validate against.
159-
* @param \Psr\Http\Message\ResponseInterface $response The response.
160-
* @return @param \Psr\Http\Message\ResponseInterface $response Modified response.
158+
* @param \Cake\Http\ServerRequest $request The request to validate against.
159+
* @param \Cake\Http\Response $response The response.
160+
* @return @param \Cake\Http\Response $response Modified response.
161161
*/
162-
protected function _addTokenCookie($token, ServerRequestInterface $request, ResponseInterface $response)
162+
protected function _addTokenCookie($token, ServerRequest $request, Response $response)
163163
{
164164
$expiry = new Time($this->_config['expiry']);
165165

@@ -175,11 +175,11 @@ protected function _addTokenCookie($token, ServerRequestInterface $request, Resp
175175
/**
176176
* Validate the request data against the cookie token.
177177
*
178-
* @param \Psr\Http\Message\ServerRequestInterface $request The request to validate against.
178+
* @param \Cake\Http\ServerRequest $request The request to validate against.
179179
* @return void
180180
* @throws \Cake\Network\Exception\InvalidCsrfTokenException When the CSRF token is invalid or missing.
181181
*/
182-
protected function _validateToken(ServerRequestInterface $request)
182+
protected function _validateToken(ServerRequest $request)
183183
{
184184
$cookies = $request->getCookieParams();
185185
$cookie = Hash::get($cookies, $this->_config['cookieName']);

0 commit comments

Comments
 (0)