yiisoft/yii2

Migrate core components resolution to DI-first approach and PSR compliance

Aperta

#20.574 aperta il 2 ott 2025

 (1 commento) (2 reazioni) (1 assegnatario)PHP (6784 fork)batch import
22dependencieshelp wanted

Metriche repository

Star
 (14.302 stelle)
Metriche merge PR
 (Merge medio 2g 15h) (47 PR mergiate in 30 g)

Descrizione

Problem

Currently, core application components such as log, view, formatter, i18n, urlManager, assetManager, and security are resolved via a legacy coreComponents array in framework/base/Application.php. This tightly couples the framework to specific implementations and limits interoperability with Yii3 and PSR-compliant components.

Proposal

  • Refactor the resolution of these core components to use the Dependency Injection (DI) container as the primary mechanism.
  • Where possible, define interfaces (preferably from the relevant PSRs) for these components.
  • For components covered by PSRs, implement compatibility layers and/or adapters so Yii2 can work with PSR-compliant service contracts.
  • During component resolution, attempt to retrieve the component from DI first. If not found, fall back to the existing coreComponents (legacy) approach for backward compatibility.
  • This enables developers to override core components via DI and facilitates integration with Yii3 and external (e.g. PSR) components.

Benefits

  • Moves Yii2 closer to Yii3's architecture and easier migration path.
  • Enables use of Yii3 (and other PSR-compatible) components in Yii2 apps.
  • Reduces coupling and increases developer flexibility.

Example Migration Path

  1. Identify all core components in coreComponents().
  2. For each, check for suitable PSR interface (e.g., PSR-3 for logging, PSR-16 for caching, PSR-7 for HTTP, etc). Create interfaces if none exist.
  3. Refactor component resolution logic to:
    • Query the DI container first.
    • If not found, fall back to coreComponents array as currently implemented.
  4. Add documentation and migration notes for developers.

Guida contributor