Skip to content

Improve DI container fallback: specific exception handling and logging#2289

Merged
ArnaudLigny merged 4 commits intophp-difrom
copilot/sub-pr-2285
Jan 19, 2026
Merged

Improve DI container fallback: specific exception handling and logging#2289
ArnaudLigny merged 4 commits intophp-difrom
copilot/sub-pr-2285

Conversation

Copy link
Contributor

Copilot AI commented Jan 19, 2026

Changes proposed in this pull request:

The fallback mechanism in Builder.php silently caught all exceptions when resolving steps from the DI container, potentially hiding configuration errors.

Exception Handling

  • Catch only DI\NotFoundException instead of \Exception
  • Import DI\NotFoundException class

Observability

  • Log warning with step class name when fallback triggers
  • Helps identify misconfigured or missing DI registrations

Documentation

  • Document expected behavior: all steps should resolve from container
  • Clarify fallback is exceptional, not normal flow

DI Configuration Fix

  • Explicitly inject Builder parameter into Parsedown class configuration
// Before: catches everything, no visibility
try {
    $stepObject = $this->container->get($step);
} catch (\Exception $e) {
    $stepObject = new $step($this);
}

// After: specific exception, logged warning
try {
    $stepObject = $this->container->get($step);
} catch (NotFoundException $e) {
    $this->getLogger()->warning(sprintf(
        'Step %s not found in DI container, using direct instantiation as fallback',
        $step
    ));
    $stepObject = new $step($this);
}

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits January 19, 2026 15:39
Co-authored-by: ArnaudLigny <80580+ArnaudLigny@users.noreply.github.com>
Co-authored-by: ArnaudLigny <80580+ArnaudLigny@users.noreply.github.com>
… namespace prefix

Co-authored-by: ArnaudLigny <80580+ArnaudLigny@users.noreply.github.com>
Copilot AI changed the title [WIP] Address feedback on PHP-DI integration for dependency injection Improve DI container fallback: specific exception handling and logging Jan 19, 2026
@ArnaudLigny ArnaudLigny marked this pull request as ready for review January 19, 2026 15:44
Copilot AI requested a review from ArnaudLigny January 19, 2026 15:44
Copilot AI review requested due to automatic review settings January 19, 2026 15:44
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request improves the DI container fallback mechanism in the Builder class by making exception handling more specific and adding observability through logging. The changes ensure that only genuine DI container lookup failures trigger the fallback, while providing visibility when this exceptional path is taken.

Changes:

  • Refined exception handling to catch only DI\NotFoundException instead of all exceptions
  • Added warning log when fallback to direct instantiation occurs
  • Fixed Parsedown DI configuration to explicitly inject Builder parameter

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/Builder.php Imported NotFoundException, changed exception handling to be more specific, added logging for fallback scenarios, and improved documentation comments
config/dependencies.php Explicitly configured Builder parameter injection for Parsedown class to ensure proper DI container resolution

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ArnaudLigny ArnaudLigny merged commit 25ce403 into php-di Jan 19, 2026
26 checks passed
@ArnaudLigny ArnaudLigny deleted the copilot/sub-pr-2285 branch January 19, 2026 15:53
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