Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

T16713 gc iterable #16715

Merged
merged 4 commits into from
Mar 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-5.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- 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)
- Fixed `Phalcon\Filter\Filter` to have the correct docblock for IDE completion
- 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)
- Fixed `Phalcon\Session\Adapter\Stream::gc()` to throw an exception if something is wrong with `glob()` [#16713](https://github.com/phalcon/cphalcon/issues/16713)

### Removed

Expand Down
48 changes: 41 additions & 7 deletions phalcon/Session/Adapter/Stream.zep
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,29 @@ class Stream extends Noop
*/
public function gc(int max_lifetime) -> int|false
{
var file, pattern, time;
var file, glob, last, pattern, time;

let pattern = this->path . this->prefix . "*",
time = time() - max_lifetime;
time = time() - max_lifetime,
glob = this->getGlobFiles(pattern);

if (false === glob) {
let last = error_get_last();
if (isset(last["message"])) {
let last = last["message"];
} else {
let last = "Unexpected gc error";
}
throw new Exception(last);
}

for file in glob(pattern) {
if true === file_exists(file) &&
true === is_file(file) &&
(filemtime(file) < time) {
unlink(file);
if (!empty(glob)) {
for file in glob {
if true === file_exists(file) &&
true === is_file(file) &&
(filemtime(file) < time) {
unlink(file);
}
}
}

Expand Down Expand Up @@ -184,6 +197,26 @@ class Stream extends Noop
return rtrim(directory, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}


/**
* Gets the glob array or returns false on failure
*
* @param string $pattern
*
* @return array|false
*/
protected function getGlobFiles(string pattern) -> array | false
{
var errorLevel, glob;

let errorLevel = error_reporting(0);
error_clear_last();
let glob = glob(pattern);
error_reporting(errorLevel);

return glob;
}

/**
* @param string $filename
*
Expand Down Expand Up @@ -268,4 +301,5 @@ class Stream extends Noop
{
return is_writable(filename);
}

}
31 changes: 31 additions & 0 deletions tests/_data/fixtures/Session/Adapter/StreamGlobFixture.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

/**
* This file is part of the Phalcon Framework.
*
* (c) Phalcon Team <team@phalcon.io>
*
* For the full copyright and license information, please view the LICENSE.txt
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Phalcon\Tests\Fixtures\Session\Adapter;

use Phalcon\Session\Adapter\Stream;

class StreamGlobFixture extends Stream
{
/**
* Gets the glob array or returns false on failure
*
* @param string $pattern
*
* @return array|false
*/
protected function getGlobFiles(string $pattern): array | false
{
return false;
}
}
23 changes: 23 additions & 0 deletions tests/integration/Session/Adapter/Stream/GcCest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@
namespace Phalcon\Tests\Integration\Session\Adapter\Stream;

use IntegrationTester;
use Phalcon\Session\Exception;
use Phalcon\Tests\Fixtures\Session\Adapter\StreamGlobFixture;
use Phalcon\Tests\Fixtures\Traits\DiTrait;

use function cacheDir;
use function file_put_contents;
use function getOptionsSessionStream;
use function sleep;
use function uniqid;

Expand Down Expand Up @@ -59,4 +62,24 @@ public function sessionAdapterStreamGc(IntegrationTester $I)
$I->dontSeeFileFound('gc_1', cacheDir('sessions'));
$I->dontSeeFileFound('gc_2', cacheDir('sessions'));
}
/**
* Tests Phalcon\Session\Adapter\Stream :: gc() -
* glob() false returns exception
*
* @return void
*
* @author Phalcon Team <team@phalcon.io>
* @since 2020-09-09
*/
public function testSessionAdapterStreamGcGlobThrowsException(IntegrationTester $I): void
{
$I->expectThrowable(
new Exception('Unexpected gc error'),
function () {
$adapter = new StreamGlobFixture(getOptionsSessionStream());

$actual = $adapter->gc(1);
}
);
}
}
Loading