Skip to content

Conversation

@sumaiazaman
Copy link
Contributor

Summary

This PR reduces direct dependencies on Container::getInstance() static calls throughout the Laravel framework by introducing proper dependency injection patterns and a new container interaction trait.

Changes Made

New Container Interaction Foundation

  • InteractsWithContainer trait: Comprehensive utility for container-aware classes providing:
    • Container instance management with fallback to getInstance()
    • Common container operations (resolve, bind, singleton, instance)
    • Method binding support for advanced container features
    • Consistent API across container-aware classes

Updated Components

Database Layer (DetectsConcurrencyErrors)

  • Added container injection support with fallback mechanism
  • Introduced setConcurrencyContainer() method for dependency injection
  • Maintained backward compatibility with getInstance() fallback

Console Scheduling (Schedule)

  • Updated constructor to accept optional container parameter
  • Improved testability through dependency injection
  • Preserved existing behavior for backward compatibility

HTTP Layer (UploadedFile)

  • Integrated InteractsWithContainer trait
  • Replaced direct Container::getInstance() calls with trait methods
  • Enhanced testability and reduced static dependencies

Key Features

Improved Dependency Injection

  • Reduced coupling to static Container::getInstance() calls
  • Enhanced testability through injectable dependencies
  • Better separation of concerns with container abstraction

Backward Compatibility

  • All changes maintain full backward compatibility
  • Fallback mechanisms preserve existing behavior
  • No breaking changes for existing applications

Enhanced Testability

  • Mockable container dependencies for unit testing
  • Easier service substitution and testing isolation
  • Better support for dependency injection in tests

Consistent Container API

  • Unified approach to container interactions
  • Standardized methods across container-aware classes
  • Reduced code duplication in container operations

Benefits

  • Better Architecture: Reduced static dependencies improve code design
  • Enhanced Testability: Easier mocking and testing of container-dependent code
  • Improved Maintainability: Cleaner separation of concerns
  • Greater Flexibility: Easier service substitution and customization
  • Future-Proof: Foundation for further dependency injection improvements

Container Operations Provided

The InteractsWithContainer trait offers:

  • resolve() - Service resolution with type hints
  • bound() - Service binding checks
  • bind() - Service binding registration
  • singleton() - Singleton binding registration
  • instance() - Instance registration
  • call() - Method injection and calling
  • hasMethodBinding() / callMethodBinding() - Advanced method binding support

Testing

  • All existing tests pass
  • Container injection works correctly
  • Fallback mechanisms function as expected
  • No performance impact observed

Migration Path

For developers wanting to adopt the new patterns:

// Before
$service = Container::getInstance()->make(SomeService::class);

// After (using the trait)
$service = $this->resolve(SomeService::class);

This enhancement establishes a foundation for better dependency injection patterns throughout the Laravel framework while maintaining complete backward compatibility.

- Create InteractsWithContainer trait for container-aware classes
- Update DetectsConcurrencyErrors trait to support container injection
- Update Schedule class constructor to accept optional container parameter
- Update UploadedFile to use container trait instead of direct getInstance() calls

Key improvements:
- Reduced coupling to Container::getInstance() static calls
- Improved testability through dependency injection
- Better separation of concerns with container abstraction
- Maintained backward compatibility with fallback to getInstance()

Benefits:
- Enhanced testability with mockable container dependencies
- Improved code maintainability and flexibility
- Better adherence to dependency injection principles
- Reduced static dependencies throughout the framework
- Easier unit testing and service substitution

The InteractsWithContainer trait provides:
- Container instance management with fallback
- Common container operations (resolve, bind, singleton, etc.)
- Method binding support for advanced container features
- Consistent API across container-aware classes

All changes maintain full backward compatibility while providing
a foundation for better dependency injection patterns.
@sumaiazaman sumaiazaman changed the title Reduce direct container dependencies with proper dependency injection [12.x] Reduce direct container dependencies with proper dependency injection Nov 11, 2025
* Register a shared binding in the container.
*
* @param string $abstract
* @param \Closure|string|null $concrete
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can narrow this further down:

Suggested change
* @param \Closure|string|null $concrete
* @param (\Closure(\Illuminate\Contracts\Foundation\Application): mixed)|string|null $concrete

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants