Skip to content

Commit b1d6923

Browse files
authored
Merge pull request #16715 from niden-code/T16713-gc-iterable
T16713 gc iterable
2 parents 8e17493 + b02861c commit b1d6923

File tree

4 files changed

+96
-7
lines changed

4 files changed

+96
-7
lines changed

CHANGELOG-5.0.md

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Fixed `Phalcon\Di\Injectable` to reference the correct instance of `Phalcon\Di\Di` in the docblock property [#16634](https://github.com/phalcon/cphalcon/issues/16634)
2525
- Fixed `Phalcon\Filter\Filter` to have the correct docblock for IDE completion
2626
- Fixed `Phalcon\Mvc\Model\Query` to use the lifetime in the "cache" service if none has been supplied by the options [#16696](https://github.com/phalcon/cphalcon/issues/16696)
27+
- Fixed `Phalcon\Session\Adapter\Stream::gc()` to throw an exception if something is wrong with `glob()` [#16713](https://github.com/phalcon/cphalcon/issues/16713)
2728

2829
### Removed
2930

phalcon/Session/Adapter/Stream.zep

+41-7
Original file line numberDiff line numberDiff line change
@@ -95,16 +95,29 @@ class Stream extends Noop
9595
*/
9696
public function gc(int max_lifetime) -> int|false
9797
{
98-
var file, pattern, time;
98+
var file, glob, last, pattern, time;
9999

100100
let pattern = this->path . this->prefix . "*",
101-
time = time() - max_lifetime;
101+
time = time() - max_lifetime,
102+
glob = this->getGlobFiles(pattern);
103+
104+
if (false === glob) {
105+
let last = error_get_last();
106+
if (isset(last["message"])) {
107+
let last = last["message"];
108+
} else {
109+
let last = "Unexpected gc error";
110+
}
111+
throw new Exception(last);
112+
}
102113

103-
for file in glob(pattern) {
104-
if true === file_exists(file) &&
105-
true === is_file(file) &&
106-
(filemtime(file) < time) {
107-
unlink(file);
114+
if (!empty(glob)) {
115+
for file in glob {
116+
if true === file_exists(file) &&
117+
true === is_file(file) &&
118+
(filemtime(file) < time) {
119+
unlink(file);
120+
}
108121
}
109122
}
110123

@@ -184,6 +197,26 @@ class Stream extends Noop
184197
return rtrim(directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
185198
}
186199

200+
201+
/**
202+
* Gets the glob array or returns false on failure
203+
*
204+
* @param string $pattern
205+
*
206+
* @return array|false
207+
*/
208+
protected function getGlobFiles(string pattern) -> array | false
209+
{
210+
var errorLevel, glob;
211+
212+
let errorLevel = error_reporting(0);
213+
error_clear_last();
214+
let glob = glob(pattern);
215+
error_reporting(errorLevel);
216+
217+
return glob;
218+
}
219+
187220
/**
188221
* @param string $filename
189222
*
@@ -268,4 +301,5 @@ class Stream extends Noop
268301
{
269302
return is_writable(filename);
270303
}
304+
271305
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
/**
4+
* This file is part of the Phalcon Framework.
5+
*
6+
* (c) Phalcon Team <team@phalcon.io>
7+
*
8+
* For the full copyright and license information, please view the LICENSE.txt
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Phalcon\Tests\Fixtures\Session\Adapter;
15+
16+
use Phalcon\Session\Adapter\Stream;
17+
18+
class StreamGlobFixture extends Stream
19+
{
20+
/**
21+
* Gets the glob array or returns false on failure
22+
*
23+
* @param string $pattern
24+
*
25+
* @return array|false
26+
*/
27+
protected function getGlobFiles(string $pattern): array | false
28+
{
29+
return false;
30+
}
31+
}

tests/integration/Session/Adapter/Stream/GcCest.php

+23
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
namespace Phalcon\Tests\Integration\Session\Adapter\Stream;
1515

1616
use IntegrationTester;
17+
use Phalcon\Session\Exception;
18+
use Phalcon\Tests\Fixtures\Session\Adapter\StreamGlobFixture;
1719
use Phalcon\Tests\Fixtures\Traits\DiTrait;
1820

1921
use function cacheDir;
2022
use function file_put_contents;
23+
use function getOptionsSessionStream;
2124
use function sleep;
2225
use function uniqid;
2326

@@ -59,4 +62,24 @@ public function sessionAdapterStreamGc(IntegrationTester $I)
5962
$I->dontSeeFileFound('gc_1', cacheDir('sessions'));
6063
$I->dontSeeFileFound('gc_2', cacheDir('sessions'));
6164
}
65+
/**
66+
* Tests Phalcon\Session\Adapter\Stream :: gc() -
67+
* glob() false returns exception
68+
*
69+
* @return void
70+
*
71+
* @author Phalcon Team <team@phalcon.io>
72+
* @since 2020-09-09
73+
*/
74+
public function testSessionAdapterStreamGcGlobThrowsException(IntegrationTester $I): void
75+
{
76+
$I->expectThrowable(
77+
new Exception('Unexpected gc error'),
78+
function () {
79+
$adapter = new StreamGlobFixture(getOptionsSessionStream());
80+
81+
$actual = $adapter->gc(1);
82+
}
83+
);
84+
}
6285
}

0 commit comments

Comments
 (0)