-
Notifications
You must be signed in to change notification settings - Fork 9
Conversation
need to fix lowered coverage... |
Fixes #10 |
src/PhpSessionPersistence.php
Outdated
use function sprintf; | ||
use function gmdate; | ||
use function time; | ||
use function filemtime; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Import statements should be alphabetized (simplifies finding declarations, and reduces chances of duplication).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sorry, that's ignorance on my part...I always used to set the order based on which func was called first
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No worries - just providing some guidance. :)
src/PhpSessionPersistence.php
Outdated
'private_no_expire' => true, | ||
]; | ||
|
||
public const CACHE_PAST_DATE = 'Thu, 19 Nov 1981 08:52:00 GMT'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this date in particular, and not the unix epoch?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was curious about that value as well 😄
https://github.com/php/php-src/blob/master/ext/session/session.c#L1194
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wow, that's super bizarre! I guess we should use the value for consistency with the engine... maybe throw a comment in indicating where we get the value from, though. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I used it for consistency....but I'm still wondering where (or when) it comes from....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably some PHP contributor's birthday. 😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
src/PhpSessionPersistence.php
Outdated
} | ||
} | ||
|
||
return $response; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invert the conditional so you can return early, combine conditions, and promote the body of the conditional:
if (! $this->cacheLimiter || $this->responseAlreadyHasCacheHeaders($response)) {
return $response;
}
$cacheHeaders = $this->generateCacheHeaders($this->cacheLimiter, $this->cacheExpire);
// ...
return $response;
src/PhpSessionPersistence.php
Outdated
/** | ||
* Generate cache headers for a given session cache_limiter value. | ||
* @param string $cacheLimiter | ||
* @param int $cacheExpire |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Parameter annotations are discouraged unless they add descriptions; these can be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do I keep the method's purpose explaination?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
src/PhpSessionPersistence.php
Outdated
} | ||
|
||
$maxAge = 60 * $cacheExpire; | ||
$lastModified = $this->getLastModified($_SERVER['SCRIPT_FILENAME'] ?? ''); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We typically try not to access superglobals directly. Please pass this value to the method by using the following from within initializeSessionFromRequest()
:
$request->getServerParams()['SCRIPT_FILENAME']
Considering that this may not be set, you'll need to come up with a default value to use; in that case, I'd likely use __FILE__
:
$request->getServerParams()['SCRIPT_FILENAME'] ?? __FILE__
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I thought about that too...I was actually waiting for your review. I actually used __FILE__
in the test case
I'd have a few questions before applying the changes:
-
generateCacheHeaders(...) is privately used only with internal properties, should I remove the arguments and use them directly?
-
same question getLastModified, since the scriptFile will be saved into an internal property
-
Should I remove thi PR and re-add a 1-commit PR? (I started uploading to upstream while forgetting i had already opened the PR, I apologize for the number of commits)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Sure, that should work fine.
- Same.
- No, I'm fine with multiple commits. If YOU are not, then run
git rebase -i
and squash some commits and force push back to your branch.
src/PhpSessionPersistence.php
Outdated
/** | ||
* Check if the response already carries cache headers | ||
* @param ResponseInterface $response | ||
* @return bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both of these annotations may be removed, as they duplicate the typehints.
src/PhpSessionPersistence.php
Outdated
private function responseAlreadyHasCacheHeaders(ResponseInterface $response) : bool | ||
{ | ||
return ( | ||
$response->hasHeader('Expires') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reduce the indentation of this line; ||
operators should align with this line.
test/PhpSessionPersistenceTest.php
Outdated
@@ -23,6 +23,8 @@ | |||
use function session_name; | |||
use function session_start; | |||
use function session_status; | |||
use function time; | |||
use function gmdate; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alphabetize imports, please.
test/PhpSessionPersistenceTest.php
Outdated
$expires = strtotime($expires); | ||
|
||
$this->assertTrue($expires >= $expiresMin); | ||
$this->assertTrue($expires <= $expiresMax); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use assertGreaterThanOrEqual()
and/or assertLessThanOrEqual()
.
test/PhpSessionPersistenceTest.php
Outdated
|
||
// temporarily set SCRIPT_FILENAME to current file for testing | ||
$scriptFilename = $_SERVER['SCRIPT_FILENAME'] ?? null; | ||
$_SERVER['SCRIPT_FILENAME'] = __FILE__; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the changes I suggested earlier around usage of superglobals, this test will need to change.
What I didn't realize is that you're accessing it during persistSession()
as well. I'm wondering if the script filename should be memoized in the Session instance, and removed during persistence?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, see my previous reply...I don't think we need to remove it though whit it being refreshed at every request...and now that I think about it...should we just memoize it once....the running script is not going to change even in asynchronous contexts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
....mhmmm....but mocked requests could contain a different SCRIPT_FILENAME....
..i forgot another question: |
Using them in tests is public behavior, so you can mark them public if you want. Otherwise you'll need to use reflection in your tests to get at the values, which is generally not fun. :) |
mhmm...I'm not sure why test are failing on travis, everything works locally...I'll check later.... |
Still no clue, locally all test pass and in a test app the appropriate headers are correctly sent. |
Travis build continue to fail, I believe this has smt to do with the merging of my previous (unrelated) merged PR, even if there is no report of conflicts. My same fork and another test branch forked from it are still passing tests successfully. I also see a strange decrease in coverage. https://travis-ci.org/pine3ree/zend-expressive-session-ext/builds/371829367 any idea? PS In the getLastModified() method we may have a memoized scriptFile either from a request server param or from the current In the end we should always get a value, but since gmdate could also return //....
public function initializeSessionFromRequest(ServerRequestInterface $request) : SessionInterface
{
// comment note: we set it to __FILE__ only once inside getLastModified if necessary
$this->scriptFile = $request->getServerParams()['SCRIPT_FILENAME'] ?? null;
//....
}
//....
private function getLastModified()
{
if (! $this->scriptFile || ! is_file($this->scriptFile)) {
$this->scriptFile = __FILE__;
}
return gmdate(self::HTTP_DATE_FORMAT, filemtime($this->scriptFile));
}
//---- (edited) |
Hello @weierophinney,
When you review last commits/comments pls let me also know if you prefer a PR from the new branch (closing this one) kind regards |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Final changes look perfect, @pine3ree !
Cache limiter support attempt
Thanks, @pine3ree! Merged to develop for release with 1.1.0. |
Inject cache headers based on cache_limiter value, while removing the automatic job from the php engine.
The php engine automatically sends cache-http-headers based on the values of
session.cache_limiter
,session.cache_expire
ini/session_start settings and the script last mtime.The idea is to steal the value of
session.cache_limiter
inside the constructor and set an empty cache_limiter when starting the session, thus disabling the automatic headers.Then we recreate the logic of the zend-engine to build the headers ourselves and inject them into the response.
TODO/TOCHECK