teewurst / psr4-advanced-wildcard-composer-plugin
Adds a parser to enable composer, to be used with wildcards
Installs: 22 589
Dependents: 0
Suggesters: 0
Security: 0
Stars: 10
Watchers: 2
Forks: 2
Open Issues: 2
Type:composer-plugin
Requires
- php: ^7.1|^8.0
- composer-plugin-api: ^1.1|^2.0
- ext-json: *
Requires (Dev)
- composer/composer: ^2.0
- phpspec/prophecy-phpunit: ^2.0
- phpstan/phpstan: ^0.12.52
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2025-04-29 01:32:54 UTC
README
Adds a parser to enable composer, to be used with wildcards
How to install
Now we are able to require
composer require teewurst/psr4-advanced-wildcard-composer-plugin
How to use
Both glob and sprintf are used to dynamically replace content of the generated autoload file:
- Use GLOB Braces to define folder dynamically in your composer.json (Epx.:
"/modules/{*Domain,*Module}/{*}/src"
) - Use %s of sprintf to match findings of GLOB to your file path (Exp.:
"My\\Namespace\\%s\\%s\\"
) - GLOB is case in/sensitive on linux/windows
- Also you may use argument switching, but that is not recommended (Exp.:
"My\\Namespace\\%2$s\\%1$s\\"
) - IDEs cant handle Advanced Wildcards in composer.json (File creation, namespace auto-complete etc.)
- if you run in --dev mode, it will generate a composer.development.json at the same location
- it is a exact copy of composer.json, but resolved wildcards
- There are two ways of configuration in your composer.json (equivalent to autoload-dev)
- (Recommended) You add your wildcards to
extra.teewurst/psr4-advanced-wildcard-composer-plugin.autoload.psr-4
(see example) - You set
extra.teewurst/psr4-advanced-wildcard-composer-plugin
to a truthy value and set your namespaces in your defaultautoload.psr-4
- (Recommended) You add your wildcards to
Example
composer.json:
{ "extra": { "teewurst/psr4-advanced-wildcard-composer-plugin": { "autoload": { "psr-4": { "My\\Namespace\\%s\\%s\\": "modules/{*Domain,*Module}/{*}/src" } }, "autoload-dev": { "psr-4": { "My\\Namespace\\test\\%s\\": "tests/{*}/src" } } } } }
FileSystem:
|- composer.json
|- modules
|- BusinessDomain
|- Calculation
|- src
|- Listener
|- src
|- DataModule
|- AWS
|- src
|- Mysql
|- src
|- SomethingElse
AdvancedWildcards + FileSystem is equivalent:
{ "autoload": { "psr-4": { "My\\Namespace\\BusinessDomain\\Calculation\\": "modules/BusinessDomain/Calculation/src", "My\\Namespace\\BusinessDomain\\Listener\\": "modules/BusinessDomain/Listener/src", "My\\Namespace\\DataModule\\AWS\\": "modules/DataModule/AWS/src", "My\\Namespace\\DataModule\\Mysql\\": "modules/DataModule/Mysql/src" } } }
Limitations and Performance
Be aware that...
- Glob/IO and performance? No, No, No... dump-autoload will take a bit longer
- This plugin is limited to one folder level per namespace replacement (Oh boy, it would escalate quickly)
- You will get weired results, if folders do not exist
Contribute
- Create any dummy repository locally, setup composer and add to that composer.json:
"repositories": [ { "type": "path", "version": "dev-[branch_name]", "url": "[path_to_local_wildcard_plugin]/psr4-advanced-wildcard-composer-plugin" }, ],
- in dummy repository, fire
composer require teewurst/psr4-advanced-wildcard-composer-plugin
- in dummy repository, you can execute the code by
composer dump-autoload
- to enable xDebug, you have to fire
export COMPOSER_ALLOW_XDEBUG=1
(session env variable = execute in every terminal) - addition needs to pass
composer test
andcomposer analyse