fix(ErrorBoundaries): use Environment class instead of direct $_ENV access
All checks were successful
Test Runner / test-basic (push) Successful in 9s
Test Runner / test-php (push) Successful in 8s
Deploy Application / deploy (push) Successful in 1m40s

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 <noreply@anthropic.com>
This commit is contained in:
2025-11-25 19:44:02 +01:00
parent 85e2360a90
commit 22fd89b013

View File

@@ -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);