@@ -6,26 +6,48 @@ class EnvironmentHelper
66{
77 /**
88 * Verifica se o ambiente é produção.
9+ * Resultado é cached para melhor performance.
910 */
1011 public static function isProduction (): bool
1112 {
12- return in_array (env ('APP_ENV ' , 'production ' ), ['production ' , 'prod ' ], true );
13+ static $ cachedResult = null ;
14+ if ($ cachedResult !== null ) {
15+ return $ cachedResult ;
16+ }
17+
18+ $ env = function_exists ('env ' ) ? env ('APP_ENV ' , 'production ' ) : ($ _ENV ['APP_ENV ' ] ?? 'production ' );
19+ $ cachedResult = in_array ($ env , ['production ' , 'prod ' ], true );
20+ return $ cachedResult ;
1321 }
1422
1523 /**
1624 * Verifica se o ambiente é desenvolvimento.
25+ * Resultado é cached para melhor performance.
1726 */
1827 public static function isDevelopment (): bool
1928 {
20- return in_array (env ('APP_ENV ' , 'development ' ), ['development ' , 'dev ' , 'local ' ], true );
29+ static $ cachedResult = null ;
30+ if ($ cachedResult !== null ) {
31+ return $ cachedResult ;
32+ }
33+
34+ $ env = function_exists ('env ' ) ? env ('APP_ENV ' , 'development ' ) : ($ _ENV ['APP_ENV ' ] ?? 'development ' );
35+ $ cachedResult = in_array ($ env , ['development ' , 'dev ' , 'local ' ], true );
36+ return $ cachedResult ;
2137 }
2238
2339 /**
2440 * Verifica se o ambiente é de testes.
2541 * Considera múltiplas fontes: APP_ENV, PHPUnit e variáveis de servidor.
42+ * Resultado é cached para melhor performance.
2643 */
2744 public static function isTesting (): bool
2845 {
46+ static $ cachedResult = null ;
47+ if ($ cachedResult !== null ) {
48+ return $ cachedResult ;
49+ }
50+
2951 // Check APP_ENV from multiple sources
3052 $ envSources = [
3153 $ _ENV ['APP_ENV ' ] ?? '' ,
@@ -39,23 +61,42 @@ public static function isTesting(): bool
3961
4062 foreach ($ envSources as $ envValue ) {
4163 if (in_array ($ envValue , ['testing ' , 'test ' ], true )) {
42- return true ;
64+ $ cachedResult = true ;
65+ return $ cachedResult ;
4366 }
4467 }
4568
4669 // Check if running under PHPUnit
47- return defined ('PHPUNIT_RUNNING ' ) ||
48- (isset ($ _ENV ['PHPUNIT_RUNNING ' ]) && $ _ENV ['PHPUNIT_RUNNING ' ]) ||
49- (isset ($ _SERVER ['PHPUNIT_RUNNING ' ]) && $ _SERVER ['PHPUNIT_RUNNING ' ]);
70+ $ cachedResult = defined ('PHPUNIT_RUNNING ' ) ||
71+ (isset ($ _ENV ['PHPUNIT_RUNNING ' ]) && $ _ENV ['PHPUNIT_RUNNING ' ]) ||
72+ (isset ($ _SERVER ['PHPUNIT_RUNNING ' ]) && $ _SERVER ['PHPUNIT_RUNNING ' ]);
73+
74+ return $ cachedResult ;
5075 }
5176
5277 /**
5378 * Retorna o nome do ambiente atual.
79+ * Resultado é cached para melhor performance.
5480 */
5581 public static function getEnvironment (): string
5682 {
57- $ env = env ('APP_ENV ' , 'production ' );
83+ static $ cachedResult = null ;
84+ if ($ cachedResult !== null ) {
85+ return $ cachedResult ;
86+ }
87+
88+ $ env = function_exists ('env ' ) ? env ('APP_ENV ' , 'production ' ) : ($ _ENV ['APP_ENV ' ] ?? 'production ' );
89+ $ cachedResult = is_string ($ env ) ? $ env : 'production ' ;
90+ return $ cachedResult ;
91+ }
5892
59- return is_string ($ env ) ? $ env : 'production ' ;
93+ /**
94+ * Limpa o cache dos métodos de ambiente.
95+ * Útil para testes unitários ou quando variáveis de ambiente mudam.
96+ */
97+ public static function clearCache (): void
98+ {
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
60101 }
61102}
0 commit comments