From 22fd89b013a29bcb51fb89739301d55c8bf5e0fc Mon Sep 17 00:00:00 2001 From: Michael Schiemer Date: Tue, 25 Nov 2025 19:44:02 +0100 Subject: [PATCH] fix(ErrorBoundaries): use Environment class instead of direct $_ENV access Replace direct $_ENV/$_SERVER access with framework's Environment class to follow proper framework patterns and enable Docker Secrets support. Changes: - Add Environment and EnvKey imports - Use $container->get(Environment::class) for environment access - Replace $_ENV['APP_ENV'] with $env->getString(EnvKey::APP_ENV, ...) - Rename internal method to registerServices for clarity - Add documentation explaining the pattern Co-Authored-By: Claude --- .../Middleware/MiddlewareServiceProvider.php | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Framework/ErrorBoundaries/Middleware/MiddlewareServiceProvider.php b/src/Framework/ErrorBoundaries/Middleware/MiddlewareServiceProvider.php index e1bac835..6430f2b7 100644 --- a/src/Framework/ErrorBoundaries/Middleware/MiddlewareServiceProvider.php +++ b/src/Framework/ErrorBoundaries/Middleware/MiddlewareServiceProvider.php @@ -4,6 +4,8 @@ declare(strict_types=1); namespace App\Framework\ErrorBoundaries\Middleware; +use App\Framework\Config\Environment; +use App\Framework\Config\EnvKey; use App\Framework\DI\Container; use App\Framework\DI\Initializer; use App\Framework\ErrorBoundaries\ErrorBoundaryFactory; @@ -12,6 +14,11 @@ use App\Framework\Logging\Logger; /** * Service provider for error boundary middleware + * + * Note: The #[Initializer] method uses the Container to get Environment, + * following framework patterns for environment-aware configuration. + * For manual/custom configuration, use the factory methods: + * development(), production(), apiOnly(), or custom(). */ final readonly class MiddlewareServiceProvider { @@ -20,8 +27,34 @@ final readonly class MiddlewareServiceProvider ) { } + /** + * Initializer for automatic DI registration + * + * Uses environment-aware configuration (development or production) + * based on APP_ENV from Environment service. + */ #[Initializer] - public function initialize(Container $container): void + public static function initialize(Container $container): void + { + // Get Environment from container (proper framework pattern) + $env = $container->get(Environment::class); + $appEnv = $env->getString(EnvKey::APP_ENV, 'production'); + + // Determine configuration based on environment + $configuration = match ($appEnv) { + 'development', 'local', 'dev' => MiddlewareConfiguration::development(), + 'staging' => MiddlewareConfiguration::production(), // Staging uses production config + default => MiddlewareConfiguration::production(), + }; + + $provider = new self($configuration); + $provider->registerServices($container); + } + + /** + * Register all middleware services in the container + */ + private function registerServices(Container $container): void { $this->registerMiddlewareConfiguration($container); $this->registerMiddlewareComponents($container);