Skip to content

Commit f69c5a0

Browse files
committed
feat: Adiciona script de teste para funcionalidade de limpeza de cache do EnvironmentHelper e melhora o gerenciamento de cache
1 parent 72e4fb0 commit f69c5a0

File tree

2 files changed

+77
-26
lines changed

2 files changed

+77
-26
lines changed

scripts/test-cache-clearing.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* Test script to demonstrate EnvironmentHelper cache clearing functionality
4+
*/
5+
6+
require_once __DIR__ . '/../vendor/autoload.php';
7+
8+
use PivotPHP\CycleORM\Helpers\EnvironmentHelper;
9+
10+
echo "Testing EnvironmentHelper cache clearing...\n\n";
11+
12+
// First calls - populate cache
13+
echo "1. First calls (populating cache):\n";
14+
$result1 = EnvironmentHelper::isTesting();
15+
$result2 = EnvironmentHelper::isProduction();
16+
$result3 = EnvironmentHelper::getEnvironment();
17+
18+
echo " isTesting(): " . ($result1 ? 'true' : 'false') . "\n";
19+
echo " isProduction(): " . ($result2 ? 'true' : 'false') . "\n";
20+
echo " getEnvironment(): {$result3}\n\n";
21+
22+
// Verify cache is populated by checking performance
23+
$start = microtime(true);
24+
for ($i = 0; $i < 1000; $i++) {
25+
EnvironmentHelper::isTesting();
26+
}
27+
$cachedTime = microtime(true) - $start;
28+
echo "2. Cache performance (1000 calls): " . number_format($cachedTime * 1000, 2) . " ms\n\n";
29+
30+
// Clear cache
31+
echo "3. Clearing cache...\n";
32+
EnvironmentHelper::clearCache();
33+
echo " Cache cleared!\n\n";
34+
35+
// Verify same results after cache clear
36+
echo "4. After cache clear (should rebuild cache):\n";
37+
$result4 = EnvironmentHelper::isTesting();
38+
$result5 = EnvironmentHelper::isProduction();
39+
$result6 = EnvironmentHelper::getEnvironment();
40+
41+
echo " isTesting(): " . ($result4 ? 'true' : 'false') . "\n";
42+
echo " isProduction(): " . ($result5 ? 'true' : 'false') . "\n";
43+
echo " getEnvironment(): {$result6}\n\n";
44+
45+
// Verify results are consistent
46+
$consistent = ($result1 === $result4) && ($result2 === $result5) && ($result3 === $result6);
47+
echo "5. Consistency check: " . ($consistent ? "✅ PASS" : "❌ FAIL") . "\n";
48+
echo " Results are " . ($consistent ? "identical" : "different") . " before and after cache clear\n\n";
49+
50+
echo "✅ Cache clearing functionality works correctly!\n";

src/Helpers/EnvironmentHelper.php

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,25 @@
44

55
class EnvironmentHelper
66
{
7+
/**
8+
* Cache centralizado para todos os métodos de ambiente.
9+
*
10+
* @var array<string, bool|string>
11+
*/
12+
private static array $cache = [];
713
/**
814
* Verifica se o ambiente é produção.
915
* Resultado é cached para melhor performance.
1016
*/
1117
public static function isProduction(): bool
1218
{
13-
static $cachedResult = null;
14-
if ($cachedResult !== null) {
15-
return $cachedResult;
19+
if (isset(self::$cache['isProduction'])) {
20+
return (bool) self::$cache['isProduction'];
1621
}
1722

1823
$env = function_exists('env') ? env('APP_ENV', 'production') : ($_ENV['APP_ENV'] ?? 'production');
19-
$cachedResult = in_array($env, ['production', 'prod'], true);
20-
return $cachedResult;
24+
self::$cache['isProduction'] = in_array($env, ['production', 'prod'], true);
25+
return (bool) self::$cache['isProduction'];
2126
}
2227

2328
/**
@@ -26,14 +31,13 @@ public static function isProduction(): bool
2631
*/
2732
public static function isDevelopment(): bool
2833
{
29-
static $cachedResult = null;
30-
if ($cachedResult !== null) {
31-
return $cachedResult;
34+
if (isset(self::$cache['isDevelopment'])) {
35+
return (bool) self::$cache['isDevelopment'];
3236
}
3337

3438
$env = function_exists('env') ? env('APP_ENV', 'development') : ($_ENV['APP_ENV'] ?? 'development');
35-
$cachedResult = in_array($env, ['development', 'dev', 'local'], true);
36-
return $cachedResult;
39+
self::$cache['isDevelopment'] = in_array($env, ['development', 'dev', 'local'], true);
40+
return (bool) self::$cache['isDevelopment'];
3741
}
3842

3943
/**
@@ -43,9 +47,8 @@ public static function isDevelopment(): bool
4347
*/
4448
public static function isTesting(): bool
4549
{
46-
static $cachedResult = null;
47-
if ($cachedResult !== null) {
48-
return $cachedResult;
50+
if (isset(self::$cache['isTesting'])) {
51+
return (bool) self::$cache['isTesting'];
4952
}
5053

5154
// Check APP_ENV from multiple sources
@@ -61,17 +64,17 @@ public static function isTesting(): bool
6164

6265
foreach ($envSources as $envValue) {
6366
if (in_array($envValue, ['testing', 'test'], true)) {
64-
$cachedResult = true;
65-
return $cachedResult;
67+
self::$cache['isTesting'] = true;
68+
return (bool) self::$cache['isTesting'];
6669
}
6770
}
6871

6972
// Check if running under PHPUnit
70-
$cachedResult = defined('PHPUNIT_RUNNING') ||
71-
(isset($_ENV['PHPUNIT_RUNNING']) && $_ENV['PHPUNIT_RUNNING']) ||
72-
(isset($_SERVER['PHPUNIT_RUNNING']) && $_SERVER['PHPUNIT_RUNNING']);
73+
self::$cache['isTesting'] = defined('PHPUNIT_RUNNING') ||
74+
(isset($_ENV['PHPUNIT_RUNNING']) && $_ENV['PHPUNIT_RUNNING']) ||
75+
(isset($_SERVER['PHPUNIT_RUNNING']) && $_SERVER['PHPUNIT_RUNNING']);
7376

74-
return $cachedResult;
77+
return (bool) self::$cache['isTesting'];
7578
}
7679

7780
/**
@@ -80,14 +83,13 @@ public static function isTesting(): bool
8083
*/
8184
public static function getEnvironment(): string
8285
{
83-
static $cachedResult = null;
84-
if ($cachedResult !== null) {
85-
return $cachedResult;
86+
if (isset(self::$cache['getEnvironment'])) {
87+
return (string) self::$cache['getEnvironment'];
8688
}
8789

8890
$env = function_exists('env') ? env('APP_ENV', 'production') : ($_ENV['APP_ENV'] ?? 'production');
89-
$cachedResult = is_string($env) ? $env : 'production';
90-
return $cachedResult;
91+
self::$cache['getEnvironment'] = is_string($env) ? $env : 'production';
92+
return (string) self::$cache['getEnvironment'];
9193
}
9294

9395
/**
@@ -96,7 +98,6 @@ public static function getEnvironment(): string
9698
*/
9799
public static function clearCache(): void
98100
{
99-
// Reset all static caches by calling each method with a flag
100-
// This is a simple approach - in production the cache should rarely need clearing
101+
self::$cache = [];
101102
}
102103
}

0 commit comments

Comments
 (0)