decodelabs / slingshot
Unified dependency injection invoker
Installs: 8 986
Dependents: 9
Suggesters: 1
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: ^8.1
- decodelabs/archetype: ^0.3
- decodelabs/exceptional: ^0.4.4
- psr/container: ^2.0
Requires (Dev)
- decodelabs/dovetail: ^0.2.1
- decodelabs/genesis: ^0.9
- decodelabs/pandora: ^0.2.12
- decodelabs/phpstan-decodelabs: ^0.6.7
README
Unified dependency injection invoker
Slingshot provides a simple interface for invoking methods on objects with dependency injection.
Get news and updates on the DecodeLabs blog.
Installation
Install via Composer:
composer require decodelabs/slingshot
Usage
Use a Slingshot instance to invoke a function with dependency injection:
use DecodeLabs\Slingshot; $slingshot = new Slingshot( container: $container, // Psr\Container\ContainerInterface parameters: [ 'param1' => 'hello' ] ); function test( string $param1, string $param2 ) { return $param1 . ' '. $param2; } $output = $slingshot->invoke(test(...), [ 'param2' => 'world' ]); // hello world
Or instantiate an object with dependency injection:
use DecodeLabs\Harvest\Context as HarvestContext; class Test { public function __construct( // Fetch or create a Harvest Context (example) from container HarvestContext $param1, string $param2 ) { // ... } } $testObect = $slingshot->instantiate(Test::class, [ 'param2' => 'value' ]);
Objects can be added to Slingshot by type for reference matching:
$object = new Test(...); $slingshot->addType($object); $slingshot->invoke(function(Test $test) { // ... });
Licensing
Slingshot is licensed under the MIT License. See LICENSE for the full license text.