Skip to content

feat(discovery): add factory callable for auto-discovery - #57

Merged
usernane merged 3 commits into
mainfrom
dev
Jun 29, 2026
Merged

feat(discovery): add factory callable for auto-discovery#57
usernane merged 3 commits into
mainfrom
dev

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Adds a setFactory() method to CommandDiscovery that 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() hardcodes new $className(). Commands requiring dependencies (DB connections, services, config) cannot use auto-discovery. Fixes #48.

Changes

  • Added $factory property and setFactory(callable) method to CommandDiscovery
  • Modified instantiateCommands() to use the factory when set
  • Changed catch from \Exception to \Throwable for proper error handling of ArgumentCountError etc.
  • Added 4 test cases with FactoryTestCommands fixtures
  • Added examples/13-auto-discovery/ demonstrating factory usage
  • Added Auto-Discovery section to README

How to Test / Verify

php vendor/bin/phpunit tests/WebFiori/Tests/Cli/Discovery/CommandDiscoveryTest.php

All 16 tests pass (12 existing + 4 new).

Breaking Changes and Migration Steps

None. Default behavior (no factory set) remains new $className().

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not)
  • I updated docs (if needed) Docs Repo
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility
  • I considered security

Related issues

Closes #48

Ibrahim BinAlshikh 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

codecov Bot commented Jun 29, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 89.09091% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.48%. Comparing base (4912537) to head (845c568).
⚠️ Report is 10 commits behind head on main.

Files with missing lines Patch % Lines
WebFiori/Cli/Discovery/CommandDiscovery.php 89.09% 12 Missing ⚠️
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     
Flag Coverage Δ
php-8.1 88.48% <89.09%> (+0.11%) ⬆️
php-8.2 88.48% <89.09%> (+0.11%) ⬆️
php-8.3 88.48% <89.09%> (+0.11%) ⬆️
php-8.4 88.48% <89.09%> (+0.11%) ⬆️
php-8.5 88.45% <89.09%> (+0.11%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@usernane
usernane merged commit 8537fbe into main Jun 29, 2026
19 checks passed
@sonarqubecloud

Copy link
Copy Markdown

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.

feat(discovery): Add command factory callable for auto-discovery

1 participant