Skip to content

Resolve decorated class through the container in initDecorated() - #8

Merged
trm42 merged 1 commit into
masterfrom
di-in-initdecorated
Jun 11, 2026
Merged

trm42 merged 1 commit into
masterfrom
di-in-initdecorated

Conversation

@trm42

@trm42 trm42 commented Jun 11, 2026

Copy link
Copy Markdown
Owner

What

CacheDecorator::initDecorated() now resolves the decorated class through Laravel's service container (resolve($class)) instead of new $class when no instance is passed to the constructor.

Why

The package is already Laravel-coupled (it uses the Cache, Config, and Log facades), and direct instantiation only worked for classes with a zero-argument constructor — it ignored the container entirely. Resolving through the container is a strictly-more-capable, low-risk upgrade:

  • Decorated classes may now declare auto-wired constructor dependencies.
  • decoratedClass() may return an interface bound in the container.
  • Existing no-dependency stubs resolve identically, so the no-arg path is behaviorally unchanged.

Changes

  • src/CacheDecorator.php$decorated = new $class;$decorated = resolve($class);. The LogicException null-class guard is unchanged (it fires before the container is touched). Updated the decoratedClass() PHPDoc.
  • README.md / CLAUDE.md — documented container-based resolution per the README-sync convention.
  • Tests — added StubCollaborator, StubServiceWithDependency (requires the collaborator in its constructor), and CachedStubServiceWithDependency, plus test_decorated_class_is_resolved_through_container_with_dependencies. The service has a required constructor argument, so the old new $class path would have thrown ArgumentCountError — the test only passes because resolution auto-wires the dependency.

Verification

composer test — 29 tests, 43 assertions, all green.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@trm42
trm42 merged commit f340628 into master Jun 11, 2026
19 checks passed
@trm42
trm42 deleted the di-in-initdecorated branch June 11, 2026 22:09
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.

1 participant