Conversation
added 3 commits
June 30, 2026 00:09
Add setFactory() method to CommandDiscovery that allows providing a custom callable for instantiating auto-discovered commands. This enables dependency injection without requiring zero-argument constructors. When set, the factory receives the fully qualified class name and must return a Command instance. Default behavior (no factory) remains unchanged. Also change catch from \Exception to \Throwable so that PHP Errors like ArgumentCountError are properly handled during instantiation. Closes #48
Add test cases covering: - Factory used for instantiation with dependency injection - Default behavior (no factory) still works for simple commands - Factory exceptions handled gracefully (added to errors) - Factory exceptions in strict mode throw CommandDiscoveryException Add FactoryTestCommands directory with CommandWithDependency and SimpleCommand test fixtures.
Add examples/13-auto-discovery/ demonstrating factory-based command instantiation with dependency injection. Add 'Auto-Discovery' section to README under Advanced Features.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #57 +/- ##
============================================
+ Coverage 88.36% 88.48% +0.11%
- Complexity 1422 1424 +2
============================================
Files 37 37
Lines 3858 3863 +5
============================================
+ Hits 3409 3418 +9
+ Misses 449 445 -4
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Summary
Adds a
setFactory()method toCommandDiscoverythat allows providing a custom callable for instantiating auto-discovered commands, enabling dependency injection without requiring zero-argument constructors.Motivation
Auto-discovered commands must currently have zero-argument constructors because
instantiateCommands()hardcodesnew $className(). Commands requiring dependencies (DB connections, services, config) cannot use auto-discovery. Fixes #48.Changes
$factoryproperty andsetFactory(callable)method toCommandDiscoveryinstantiateCommands()to use the factory when set\Exceptionto\Throwablefor proper error handling ofArgumentCountErroretc.FactoryTestCommandsfixturesexamples/13-auto-discovery/demonstrating factory usageHow to Test / Verify
All 16 tests pass (12 existing + 4 new).
Breaking Changes and Migration Steps
None. Default behavior (no factory set) remains
new $className().Checklist
composer fix-cs)Related issues
Closes #48