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 <noreply@anthropic.com>
This commit is contained in:
@@ -4,6 +4,8 @@ declare(strict_types=1);
|
|||||||
|
|
||||||
namespace App\Framework\ErrorBoundaries\Middleware;
|
namespace App\Framework\ErrorBoundaries\Middleware;
|
||||||
|
|
||||||
|
use App\Framework\Config\Environment;
|
||||||
|
use App\Framework\Config\EnvKey;
|
||||||
use App\Framework\DI\Container;
|
use App\Framework\DI\Container;
|
||||||
use App\Framework\DI\Initializer;
|
use App\Framework\DI\Initializer;
|
||||||
use App\Framework\ErrorBoundaries\ErrorBoundaryFactory;
|
use App\Framework\ErrorBoundaries\ErrorBoundaryFactory;
|
||||||
@@ -12,6 +14,11 @@ use App\Framework\Logging\Logger;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Service provider for error boundary middleware
|
* 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
|
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]
|
#[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->registerMiddlewareConfiguration($container);
|
||||||
$this->registerMiddlewareComponents($container);
|
$this->registerMiddlewareComponents($container);
|
||||||
|
|||||||
Reference in New Issue
Block a user