Skip to content

Commit 58efb74

Browse files
committed
Merge remote-tracking branch 'upstream/develop' into 4.8
2 parents d0e419d + 9361461 commit 58efb74

11 files changed

Lines changed: 594 additions & 27 deletions

File tree

‎composer.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
"mikey179/vfsstream": "^1.6.12",
2525
"nexusphp/tachycardia": "^2.0",
2626
"phpstan/extension-installer": "^1.4",
27-
"phpstan/phpstan": "^2.2.15",
27+
"phpstan/phpstan": "^2.3.4",
2828
"phpstan/phpstan-strict-rules": "^2.0",
2929
"phpunit/phpcov": "^9.0.2 || ^10.0",
3030
"phpunit/phpunit": "^10.5.16 || ^11.2",
3131
"predis/predis": "^3.0",
32-
"rector/rector": "2.8.1",
32+
"rector/rector": "2.8.3",
3333
"shipmonk/phpstan-baseline-per-identifier": "^2.0"
3434
},
3535
"replace": {

‎system/Cache/FactoriesCache/FileVarExportHandler.php‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,18 @@
1313

1414
namespace CodeIgniter\Cache\FactoriesCache;
1515

16-
final class FileVarExportHandler
16+
final readonly class FileVarExportHandler
1717
{
18-
private string $path = WRITEPATH . 'cache';
18+
private string $path;
19+
20+
/**
21+
* @param string|null $path The directory where the cache files are stored.
22+
* Defaults to the "writable/cache" directory.
23+
*/
24+
public function __construct(?string $path = null)
25+
{
26+
$this->path = rtrim($path ?? WRITEPATH . 'cache', '/');
27+
}
1928

2029
public function save(string $key, mixed $val): void
2130
{

‎system/Database/MySQLi/Connection.php‎

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ class Connection extends BaseConnection
9696
*/
9797
protected bool $strictOn = false;
9898

99+
/**
100+
* InnoDB errno 1213 ended this transaction.
101+
*/
102+
private bool $transServerRolledBack = false;
103+
104+
/**
105+
* The next statement must BEGIN before it runs.
106+
*/
107+
private bool $transNeedsServerRestart = false;
108+
99109
/**
100110
* Checks whether the native database error represents a unique constraint violation.
101111
*/
@@ -350,6 +360,18 @@ protected function execute(string $sql)
350360
}
351361

352362
try {
363+
// 1213 ended the server transaction. BEGIN again so this statement
364+
// does not autocommit. A failed BEGIN stays pending. 1205 is unchanged:
365+
// innodb_rollback_on_timeout decides that case.
366+
if ($this->connID instanceof mysqli && $this->serverTransactionRestartPending()) {
367+
// Exception reporting off: a failed BEGIN returns false.
368+
if (! $this->connID->begin_transaction()) {
369+
return false;
370+
}
371+
372+
$this->transNeedsServerRestart = false;
373+
}
374+
353375
return $this->connID->query($this->prepQuery($sql), $this->resultMode);
354376
} catch (mysqli_sql_exception $e) {
355377
log_message('error', "{message}\nin {exFile} on line {exLine}.\n{trace}", [
@@ -359,6 +381,10 @@ protected function execute(string $sql)
359381
'trace' => render_backtrace($e->getTrace()),
360382
]);
361383

384+
if ($e->getCode() === 1213 && $this->transDepth > 0) {
385+
$this->markServerRolledBackTransaction();
386+
}
387+
362388
$exception = $this->createDatabaseException($e->getMessage(), $e->getCode(), $e);
363389

364390
if ($this->DBDebug) {
@@ -637,16 +663,53 @@ public function insertID(): int
637663

638664
protected function _transBegin(): bool
639665
{
640-
return $this->connID->begin_transaction();
666+
if (! $this->connID->begin_transaction()) {
667+
return false;
668+
}
669+
670+
$this->clearDeadlockTransactionState();
671+
672+
return true;
641673
}
642674

643675
protected function _transCommit(): bool
644676
{
677+
// mysqli::commit() returns true after 1213. End the transaction
678+
// and report failure instead.
679+
if ($this->transServerRolledBack) {
680+
$this->transRollback();
681+
682+
return false;
683+
}
684+
645685
return $this->connID->commit();
646686
}
647687

648688
protected function _transRollback(): bool
649689
{
650-
return $this->connID->rollback();
690+
if (! $this->connID->rollback()) {
691+
return false;
692+
}
693+
694+
$this->clearDeadlockTransactionState();
695+
696+
return true;
697+
}
698+
699+
private function markServerRolledBackTransaction(): void
700+
{
701+
$this->transServerRolledBack = true;
702+
$this->transNeedsServerRestart = true;
703+
}
704+
705+
private function serverTransactionRestartPending(): bool
706+
{
707+
return $this->transNeedsServerRestart && $this->transDepth > 0;
708+
}
709+
710+
private function clearDeadlockTransactionState(): void
711+
{
712+
$this->transServerRolledBack = false;
713+
$this->transNeedsServerRestart = false;
651714
}
652715
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Cache;
15+
16+
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
17+
18+
/**
19+
* Gives the test class its own directory for the files created
20+
* by the FileVarExportHandler.
21+
*
22+
* The handler stores its files in the shared "writable/cache" directory by
23+
* default. Since the random test execution runs the components in parallel,
24+
* one component may delete the cache file that another component is
25+
* asserting on. A dedicated directory per test class avoids that.
26+
*/
27+
trait IsolatedCacheTrait
28+
{
29+
/**
30+
* The directory used to store the cache files of the test class.
31+
*/
32+
private static string $isolatedCachePath;
33+
34+
public static function setUpBeforeClassIsolatedCache(): void
35+
{
36+
helper('filesystem');
37+
38+
self::$isolatedCachePath = WRITEPATH . 'cache_test_' . str_replace('\\', '_', static::class);
39+
40+
// Remove the files left over by a previous run.
41+
if (is_dir(self::$isolatedCachePath)) {
42+
delete_files(self::$isolatedCachePath, true);
43+
@rmdir(self::$isolatedCachePath);
44+
}
45+
}
46+
47+
public static function tearDownAfterClassIsolatedCache(): void
48+
{
49+
if (! isset(self::$isolatedCachePath) || ! is_dir(self::$isolatedCachePath)) {
50+
return;
51+
}
52+
53+
delete_files(self::$isolatedCachePath, true);
54+
@rmdir(self::$isolatedCachePath);
55+
}
56+
57+
/**
58+
* Returns a handler that stores its files in the isolated directory.
59+
*/
60+
protected function getIsolatedCacheHandler(): FileVarExportHandler
61+
{
62+
return new FileVarExportHandler(self::$isolatedCachePath);
63+
}
64+
65+
/**
66+
* Returns the path of the isolated cache directory.
67+
*/
68+
protected static function getIsolatedCachePath(): string
69+
{
70+
return self::$isolatedCachePath;
71+
}
72+
}

‎tests/system/Autoloader/FileLocatorCachedTest.php‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,30 @@
1717
use Config\Autoload;
1818
use Config\Modules;
1919
use PHPUnit\Framework\Attributes\Group;
20+
use Tests\Support\Cache\IsolatedCacheTrait;
2021

2122
/**
2223
* @internal
2324
*/
2425
#[Group('Others')]
2526
final class FileLocatorCachedTest extends FileLocatorTest
2627
{
28+
use IsolatedCacheTrait;
29+
2730
private FileVarExportHandler $handler;
2831

32+
public static function setUpBeforeClass(): void
33+
{
34+
parent::setUpBeforeClass();
35+
36+
self::setUpBeforeClassIsolatedCache();
37+
}
38+
2939
public static function tearDownAfterClass(): void
3040
{
31-
parent::tearDownAfterClass();
41+
self::tearDownAfterClassIsolatedCache();
3242

33-
// Delete cache file.
34-
$autoloader = new Autoloader();
35-
$handler = new FileVarExportHandler();
36-
$fileLocator = new FileLocator($autoloader);
37-
$locator = new FileLocatorCached($fileLocator, $handler);
38-
$locator->deleteCache();
43+
parent::tearDownAfterClass();
3944
}
4045

4146
protected function setUp(): void
@@ -61,7 +66,7 @@ protected function setUp(): void
6166
'Acme\Sample' => TESTPATH . '_support/does/not/exists',
6267
]);
6368

64-
$this->handler = new FileVarExportHandler();
69+
$this->handler = $this->getIsolatedCacheHandler();
6570
$fileLocator = new FileLocator($autoloader);
6671
$this->locator = new FileLocatorCached($fileLocator, $this->handler);
6772
}
@@ -70,6 +75,10 @@ protected function tearDown(): void
7075
{
7176
$this->locator->__destruct();
7277

78+
// Prevent the destructor from writing the cache file
79+
// when the test object is destroyed.
80+
$this->locator->deleteCache();
81+
7382
parent::tearDown();
7483
}
7584

‎tests/system/Cache/FactoriesCacheFileVarExportHandlerTest.php‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,50 @@
1515

1616
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
1717
use PHPUnit\Framework\Attributes\Group;
18+
use Tests\Support\Cache\IsolatedCacheTrait;
1819

1920
/**
2021
* @internal
2122
*/
2223
#[Group('Others')]
2324
final class FactoriesCacheFileVarExportHandlerTest extends AbstractFactoriesCacheHandlerTestCase
2425
{
26+
use IsolatedCacheTrait;
27+
2528
public static function setUpBeforeClass(): void
2629
{
2730
parent::setUpBeforeClass();
2831

29-
helper('filesystem');
32+
self::setUpBeforeClassIsolatedCache();
33+
}
34+
35+
public static function tearDownAfterClass(): void
36+
{
37+
self::tearDownAfterClassIsolatedCache();
38+
39+
parent::tearDownAfterClass();
3040
}
3141

3242
protected function createFactoriesCache(): void
3343
{
34-
$this->handler = new FileVarExportHandler();
44+
$this->handler = $this->getIsolatedCacheHandler();
3545
$this->cache = new FactoriesCache($this->handler);
3646
}
3747

48+
public function testUsesTheWritableCacheDirectoryByDefault(): void
49+
{
50+
$handler = new FileVarExportHandler();
51+
52+
$this->assertSame(WRITEPATH . 'cache', $this->getPrivateProperty($handler, 'path'));
53+
}
54+
3855
public function testSaveCreatesDirectoryAndFileWithCorrectPermissions(): void
3956
{
4057
$dir = WRITEPATH . 'cache_test_dir_' . uniqid('', true);
4158
$oldUmask = umask(0000);
4259

4360
try {
44-
$handler = new FileVarExportHandler();
45-
$this->setPrivateProperty($handler, 'path', $dir);
61+
$handler = new FileVarExportHandler($dir);
4662

4763
$handler->save('test_key', ['data']);
4864

‎tests/system/Commands/Cache/ClearCacheTest.php‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use CodeIgniter\Autoloader\FileLocator;
1717
use CodeIgniter\Autoloader\FileLocatorCached;
1818
use CodeIgniter\Cache\CacheFactory;
19-
use CodeIgniter\Cache\FactoriesCache\FileVarExportHandler;
2019
use CodeIgniter\Cache\Handlers\FileHandler;
2120
use CodeIgniter\CLI\CLI;
2221
use CodeIgniter\Config\Factories;
@@ -25,17 +24,33 @@
2524
use Config\Cache;
2625
use Config\Services;
2726
use PHPUnit\Framework\Attributes\Group;
27+
use Tests\Support\Cache\IsolatedCacheTrait;
2828

2929
/**
3030
* @internal
3131
*/
3232
#[Group('Others')]
3333
final class ClearCacheTest extends CIUnitTestCase
3434
{
35+
use IsolatedCacheTrait;
3536
use StreamFilterTrait;
3637

3738
private Cache $config;
3839

40+
public static function setUpBeforeClass(): void
41+
{
42+
parent::setUpBeforeClass();
43+
44+
self::setUpBeforeClassIsolatedCache();
45+
}
46+
47+
public static function tearDownAfterClass(): void
48+
{
49+
self::tearDownAfterClassIsolatedCache();
50+
51+
parent::tearDownAfterClass();
52+
}
53+
3954
protected function setUp(): void
4055
{
4156
parent::setUp();
@@ -101,7 +116,7 @@ public function testClearCacheWorks(): void
101116

102117
public function testClearCacheDiscardsSharedLocatorCache(): void
103118
{
104-
$handler = new FileVarExportHandler();
119+
$handler = $this->getIsolatedCacheHandler();
105120
$handler->delete('FileLocatorCache');
106121

107122
$locator = new FileLocatorCached(new FileLocator(service('autoloader')), $handler);
@@ -120,6 +135,10 @@ public function testClearCacheDiscardsSharedLocatorCache(): void
120135
$this->assertArrayHasKey('Config/Cache', $cached['search']);
121136

122137
$handler->delete('FileLocatorCache');
138+
139+
// Prevent the destructor from writing the cache file
140+
// when the locator is destroyed.
141+
$locator->deleteCache();
123142
}
124143

125144
public function testClearCacheFails(): void

0 commit comments

Comments
 (0)