44
55class 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