Skip to content

Add contextual bindings and PHP 8 attributes support#20

Merged
Chemaclass merged 3 commits into
mainfrom
feat/more-dx-improvements-2
Nov 8, 2025
Merged

Add contextual bindings and PHP 8 attributes support#20
Chemaclass merged 3 commits into
mainfrom
feat/more-dx-improvements-2

Conversation

@Chemaclass
Copy link
Copy Markdown
Member

📓 Summary

This PR adds two major developer experience improvements to the container:

  1. Contextual Bindings - Inject different implementations based on which class needs them
  2. PHP 8 Attributes - Declarative configuration using #[Inject], #[Singleton], and #[Factory]

🏗️ Contextual Bindings

Different classes can now receive different implementations of the same interface:

  $container->when(UserController::class)
      ->needs(LoggerInterface::class)
      ->give(FileLogger::class);

  $container->when(AdminController::class)
      ->needs(LoggerInterface::class)
      ->give(DatabaseLogger::class);

:atom: PHP 8 Attributes

#[Inject] - Override type hints

  class NotificationService {
      public function __construct(
          #[Inject(EmailLogger::class)]
          private LoggerInterface $logger,
      ) {}
  }

#[Singleton] - Single instance

  #[Singleton]
  class DatabaseConnection {
      public function __construct(private string $dsn) {}
  }

#[Factory] - New instances

  #[Factory]
  class RequestContext {
      public function __construct(private LoggerInterface $logger) {}
  }

@Chemaclass Chemaclass self-assigned this Nov 8, 2025
@Chemaclass Chemaclass added the enhancement New feature or request label Nov 8, 2025
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

public function resolveDependencies(string $className): array
{
if (!isset($this->cachedDependencies[$className])) {
$this->cachedDependencies[$className] = $this
->getDependencyResolver()
->resolveDependencies($className);
}
return $this->cachedDependencies[$className];

P1 Badge Cache contextual resolutions by requesting class

Contextual bindings are resolved based on the class currently on the build stack, yet resolveDependencies() caches the dependency list only by the class being instantiated. When a shared service is resolved in multiple contexts with different when()->needs()->give() rules, the first cached dependency set is reused and later contextual bindings are ignored (e.g. Service resolved for UserController caches a FileLogger and AdminController subsequently receives the same instance instead of its DatabaseLogger). The cache keys need to incorporate the requesting context or be disabled for contextual bindings to avoid injecting incorrect implementations.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@Chemaclass Chemaclass merged commit 02c73f2 into main Nov 8, 2025
12 checks passed
@Chemaclass Chemaclass deleted the feat/more-dx-improvements-2 branch November 8, 2025 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants