Skip to content

5.next: Option B - Simpler opt-in CakePHP container#19366

Open
dereuromark wants to merge 5 commits intocakephp:5.nextfrom
dereuromark:5.next-container-optinb
Open

5.next: Option B - Simpler opt-in CakePHP container#19366
dereuromark wants to merge 5 commits intocakephp:5.nextfrom
dereuromark:5.next-container-optinb

Conversation

@dereuromark
Copy link
Copy Markdown
Member

Alternative to #19358

This PR implements Option B from my comment on #19358 - a simpler approach to making the built-in CakePHP container available as opt-in for 5.4.

Summary

  • Adds ContainerFactory to select container based on Configure::read('App.container')
  • Updates type hints throughout to accept both ContainerInterface and CakeContainer
  • Removes League-specific traits from ComponentRegistry (uses simpler inline logic)
  • Uses PSR interfaces for exception catching

Usage

To opt-in to the CakePHP container:

Configure::write('App.container', 'cake');

Comparison

Aspect PR #19358 (Adapters) This PR (Option B)
Lines of code ~450 adapter code ~100 lines total
BC for non-switchers 100% 100%
BC for switchers ~100% Minor adjustments may be needed
Runtime overhead Adapter wrapping None
Complexity Higher Lower

Trade-offs

Advantages:

  • Much less code (~100 vs ~450 lines)
  • No runtime adapter overhead
  • Easier to maintain and reason about
  • Clear message: "if you want the new container, update any League-specific code"

Disadvantages:

  • Apps using League internals directly need code changes when switching
  • Not a strict drop-in replacement for League

Why this is pragmatic

  • It's opt-in - only affects users who actively choose to switch
  • Apps coupled to League internals will need changes in 6.x anyway
  • Most apps only use the basic API (add, addShared, get, has) which works identically
  • Config-based opt-in means only users who actively choose are affected

Refs: #19358, #18090

This adds a simpler alternative approach (Option B) for making the built-in
CakePHP container available as an opt-in feature for 5.4.

Key changes:
- Add ContainerFactory to select container based on Configure setting
- Update type hints to accept both ContainerInterface and CakeContainer
- Remove League-specific traits from ComponentRegistry
- Use PSR interface for exception catching

To opt-in to the CakePHP container:
  Configure::write('App.container', 'cake');

This approach:
- Keeps ~100 lines of code vs ~450 lines in the adapter approach
- Zero BC impact for users who don't switch containers
- Accepts minor app changes for those who do switch
- No adapter/wrapper complexity

Refs: cakephp#19358
@dereuromark dereuromark added this to the 5.4.0 milestone Mar 26, 2026
dereuromark and others added 4 commits March 26, 2026 01:24
Uses a bridge pattern that wraps CakePHP's Container to implement
ContainerInterface, maintaining 100% backwards compatibility:

- CakeContainerBridge wraps Cake\Container\Container
- CakeDefinitionBridge wraps definitions for League interface compat
- CakeInflectorBridge wraps inflectors for League interface compat

No interface changes required - existing implementations continue to
work unchanged. Users can opt into the CakePHP container via config.
- Revert interface union types to maintain BC
- Add getTags() method for league/container 5.2 compatibility
Required for compatibility with league/container 5.2 which added
this method to its DefinitionInterface.
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.

2 participants