4
4
5
5
class EnvironmentHelper
6
6
{
7
+ /**
8
+ * Cache centralizado para todos os métodos de ambiente.
9
+ *
10
+ * @var array<string, bool|string>
11
+ */
12
+ private static array $ cache = [];
7
13
/**
8
14
* Verifica se o ambiente é produção.
9
15
* Resultado é cached para melhor performance.
10
16
*/
11
17
public static function isProduction (): bool
12
18
{
13
- static $ cachedResult = null ;
14
- if ($ cachedResult !== null ) {
15
- return $ cachedResult ;
19
+ if (isset (self ::$ cache ['isProduction ' ])) {
20
+ return (bool ) self ::$ cache ['isProduction ' ];
16
21
}
17
22
18
23
$ 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 ' ] ;
21
26
}
22
27
23
28
/**
@@ -26,14 +31,13 @@ public static function isProduction(): bool
26
31
*/
27
32
public static function isDevelopment (): bool
28
33
{
29
- static $ cachedResult = null ;
30
- if ($ cachedResult !== null ) {
31
- return $ cachedResult ;
34
+ if (isset (self ::$ cache ['isDevelopment ' ])) {
35
+ return (bool ) self ::$ cache ['isDevelopment ' ];
32
36
}
33
37
34
38
$ 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 ' ] ;
37
41
}
38
42
39
43
/**
@@ -43,9 +47,8 @@ public static function isDevelopment(): bool
43
47
*/
44
48
public static function isTesting (): bool
45
49
{
46
- static $ cachedResult = null ;
47
- if ($ cachedResult !== null ) {
48
- return $ cachedResult ;
50
+ if (isset (self ::$ cache ['isTesting ' ])) {
51
+ return (bool ) self ::$ cache ['isTesting ' ];
49
52
}
50
53
51
54
// Check APP_ENV from multiple sources
@@ -61,17 +64,17 @@ public static function isTesting(): bool
61
64
62
65
foreach ($ envSources as $ envValue ) {
63
66
if (in_array ($ envValue , ['testing ' , 'test ' ], true )) {
64
- $ cachedResult = true ;
65
- return $ cachedResult ;
67
+ self :: $ cache [ ' isTesting ' ] = true ;
68
+ return ( bool ) self :: $ cache [ ' isTesting ' ] ;
66
69
}
67
70
}
68
71
69
72
// 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 ' ]);
73
76
74
- return $ cachedResult ;
77
+ return ( bool ) self :: $ cache [ ' isTesting ' ] ;
75
78
}
76
79
77
80
/**
@@ -80,14 +83,13 @@ public static function isTesting(): bool
80
83
*/
81
84
public static function getEnvironment (): string
82
85
{
83
- static $ cachedResult = null ;
84
- if ($ cachedResult !== null ) {
85
- return $ cachedResult ;
86
+ if (isset (self ::$ cache ['getEnvironment ' ])) {
87
+ return (string ) self ::$ cache ['getEnvironment ' ];
86
88
}
87
89
88
90
$ 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 ' ] ;
91
93
}
92
94
93
95
/**
@@ -96,7 +98,6 @@ public static function getEnvironment(): string
96
98
*/
97
99
public static function clearCache (): void
98
100
{
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 = [];
101
102
}
102
103
}
0 commit comments