Skip to content

Conversation

@gitauto-ai
Copy link
Contributor

@gitauto-ai gitauto-ai bot commented Oct 25, 2024

Resolves #237

What is the feature

This feature introduces auto-registration and automatic dependency resolution to the Dependency Injection (DI) Container in the Pancake project. When enabled, the DI Container will automatically register services that are not explicitly registered and resolve all their dependencies, including constructor dependencies, recursively.

Why we need the feature

  • Simplify Service Management: Manually registering every service can be tedious and error-prone, especially in large projects with numerous services and dependencies.
  • Enhance Developer Productivity: Auto-registration reduces boilerplate code, allowing developers to focus on business logic rather than configuration.
  • Improve Flexibility: The DI Container adapts to changes in the codebase without requiring additional registration code.
  • Facilitate Dependency Handling: Automatic resolution of dependencies ensures that complex dependency graphs are managed seamlessly.

How to implement and why

Implementation Steps:

  1. Modify the DIContainer Class:

    • Add a Property for Auto-Registration Flag:
      • Introduce a private property $autoRegisterEnabled to control the auto-registration feature.
      • Initialize it in the constructor with a default value of true, making the feature enabled by default.
    • Update the resolve Method:
      • Check if the service exists in $services. If not found and auto-registration is enabled, proceed to auto-register.
      • Invoke a new private method autoRegister($name) to handle the registration and resolution.
    • Implement the autoRegister Method:
      • Use PHP's Reflection API to inspect the class and its constructor.
      • Check if the class exists; if not, throw an exception.
      • Retrieve constructor parameters and resolve each dependency recursively by calling resolve on the parameter's class name.
      • Instantiate the class with resolved dependencies using newInstanceArgs.
    • Add Methods to Control Auto-Registration:
      • Provide setAutoRegisterEnabled(bool $enabled) to allow enabling or disabling the feature at runtime.
      • This adds flexibility for developers to opt-in or opt-out as needed.
  2. Handle Dependency Resolution:

    • Ensure that when resolving dependencies, the container can handle nested dependencies by recursively resolving constructor arguments.
    • If a dependency cannot be resolved (e.g., missing type hint or class does not exist), throw an exception with a clear message for easier debugging.
  3. Update Registration Methods:

    • Maintain existing methods like registerSingleton and registerTransient to ensure they work seamlessly with the new auto-registration feature.
    • Explicitly registered services should take precedence over auto-registered ones.
  4. Write Unit Tests:

    • Auto-Registration Tests:
      • Verify that services not explicitly registered can be auto-registered and resolved.
    • Dependency Resolution Tests:
      • Test services with multiple layers of dependencies to ensure recursive resolution works correctly.
    • Auto-Registration Disabled Tests:
      • Confirm that when auto-registration is disabled, resolving an unregistered service throws an exception.
    • Exception Handling Tests:
      • Ensure meaningful exceptions are thrown when services cannot be resolved.
  5. Write Integration Tests:

    • Create real-world scenarios where services depend on each other.
    • Validate that the DI Container correctly auto-registers and resolves all services and their dependencies in an application context.
  6. Update Documentation:

    • Provide clear examples showcasing how to use the DI Container with auto-registration enabled and disabled.
    • Explain the mechanisms of auto-registration and dependency resolution.
    • Highlight any potential caveats or considerations.

Why Implement This Way:

  • Maintain Backward Compatibility: By enabling auto-registration by default but allowing it to be disabled, existing codebases remain unaffected unless they choose to utilize the new feature.
  • Improve Developer Experience: Automating the tedious parts of service registration promotes a smoother development workflow.
  • Enhance Reliability: With thorough unit and integration tests, we ensure the new feature is robust and reliable.

About backward compatibility

  • Non-Intrusive Addition: The feature is added in a way that does not break existing functionality. All existing methods and behaviors remain unchanged unless auto-registration is used.
  • Enabled by Default, Configurable: Auto-registration is enabled by default for convenience but can be turned off for projects that require explicit service registration.
  • Explicit Registration Priority: Services explicitly registered in the container take precedence over auto-registered services, preventing unexpected behavior.
  • No Changes Required for Existing Code: Developers are not required to modify existing code to accommodate this feature unless they wish to leverage auto-registration.

By carefully implementing the feature with these considerations, we ensure that current users of the Pancake DI Container can adopt the new functionality without disruption.

Test these changes locally

git checkout -b gitauto/issue-237-89757ae2-5736-4b2e-9a82-20e0e588b009
git pull origin gitauto/issue-237-89757ae2-5736-4b2e-9a82-20e0e588b009

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Oct 25, 2024

Important

Review skipped

Bot user detected.

To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Comment @coderabbitai help to get the list of available commands and usage tips.

This commit fixes the style issues introduced in 5e8725c according to the output
from PHP CS Fixer.

Details: #253
@github-actions github-actions bot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Oct 25, 2024
@deepsource-io
Copy link

deepsource-io bot commented Oct 25, 2024

Here's the code health analysis summary for commits d72f64d..d40498f. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Test coverage LogoTest coverage⚠️ Artifact not reportedTimed out: Artifact was never reportedView Check ↗
DeepSource SQL LogoSQL✅ SuccessView Check ↗
DeepSource Secrets LogoSecrets✅ SuccessView Check ↗
DeepSource PHP LogoPHP❌ Failure
❗ 5 occurences introduced
View Check ↗
DeepSource Docker LogoDocker✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.


For more details, refer to the [DIContainer Documentation](docs/DIContainer.md).


Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 1; Actual: 3 Warning

Expected: 1; Actual: 3
For more details, refer to the [DIContainer Documentation](docs/DIContainer.md).



Check warning

Code scanning / Markdownlint (reported by Codacy)

Expected: 1; Actual: 2 Warning

Expected: 1; Actual: 2

class ServiceWithUnresolvableDependency
{
public function __construct($unresolvable)

Check notice

Code scanning / Phpmd (reported by Codacy)

This rule prohibits the definition of unused parameters in methods or constructors. Note test

Avoid unused parameters such as '$unresolvable'.
Copy link

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

Phpcs (reported by Codacy) found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@gstraccini gstraccini bot added enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. ♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation 🕔 high effort A task that can be completed in a few days 🧑‍💻 tech-debit Technical debt that needs to be addressed 🧠 backlog Items that are in the backlog for future work 🧪 tests Tasks related to testing labels Oct 25, 2024
@gstraccini gstraccini bot requested a review from guibranco October 25, 2024 00:30
@gstraccini gstraccini bot added 🚦 awaiting triage Items that are awaiting triage or categorization 🤖 bot Automated processes or integrations labels Oct 25, 2024
@guibranco guibranco added 🚷 blocked Task or issue is currently blocked and removed 🚦 awaiting triage Items that are awaiting triage or categorization labels Jun 24, 2025
@gstraccini gstraccini bot added the 🚦 awaiting triage Items that are awaiting triage or categorization label Jul 11, 2025
@guibranco guibranco removed the 🚦 awaiting triage Items that are awaiting triage or categorization label Jul 14, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Dec 4, 2025

Infisical secrets check: ✅ No secrets leaked!

💻 Scan logs
2025-12-04T17:13:05Z INF scanning for exposed secrets...
5:13PM INF 429 commits scanned.
2025-12-04T17:13:05Z INF scan completed in 680ms
2025-12-04T17:13:05Z INF no leaks found

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🧠 backlog Items that are in the backlog for future work 🚷 blocked Task or issue is currently blocked 🤖 bot Automated processes or integrations ♻️ code quality Code quality-related tasks or issues 📝 documentation Tasks related to writing or updating documentation enhancement New feature or request gitauto GitAuto label to trigger the app in a issue. 🕔 high effort A task that can be completed in a few days size/L Denotes a PR that changes 100-499 lines, ignoring generated files. 🧑‍💻 tech-debit Technical debt that needs to be addressed 🧪 tests Tasks related to testing

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add Auto-Registration and Dependency Resolution to DI Container in Pancake

2 participants