clippy / container
Dependency-injection container inspired by Pimple and PHP-DI Invoker
Installs: 59 373
Dependents: 1
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 1
Open Issues: 0
Requires
- php: >=7.2
- php-di/invoker: ~2.0
- pimple/pimple: ~3.0
- psr/container: ~1.1||~2.0
README
This is a derivative of pimple with a few notable changes.
Function parameter injection
In standard Pimple, the service factories all receive Container $c
as input, e.g.
$c['descendent'] = function($c) { return $c['parent']->generateDescendent()->withWhizBang(); };
In Clippy's container:
$c['descendent'] = function(MyParent $parent) { return $parent->generateDescendent()->withWhizBang(); };
This allows the consumers of services to (progressively) use type-hinting.
Service methods
A "service method" is a function which supports both service-injection and runtime data-passing. For example:
$c['getPassword()'] = function ($domain, SymfonyStyle $io) { if (getenv('PASSWORD')) return getenv('PASSWORD'); return $io->askHidden("What is the password for <comment>$domain</comment>?"); } $c['app']->main('', function($getPassword) { $pass = $getPassword('example.com'); });
The first parameter to getPassword
is given at runtime ($getPassword('example.com')
); the second parameter ($io
)
is injected automatically.
The service-method is denoted by including ()
in the declaration. Compare:
// Standard service $c['foo'] = function($injectedService, ...) { ... } // Service method $c['foo()'] = function($inputtedData, ... , $injectedService, ...) { ... } };
Autowired objects / anonymous service classes
The following allows for using injection with improvised service classes.
$c['basicService'] = 'something'; $c['newService'] = $c->autowiredObject(new class() { protected $basicService; public function double() { return $this->basicService . $this->basicService; } });
Properties (eg $basicService
) will be pre-populated with the corresponding services.
In the default strict
mode, unmatched properties will produce exceptions. This can be disabled, e.g.
$c['newService'] = $c->autowiredObject(['strict' => FALSE], new class() { ..});
Similarly, you may define a regular service function and use autowiring as part of the logic, e.g.
$c['basicService'] = 'something'; $c['newService'] = function() use ($c) { return $c->autowire(new MyClass()); };
Sigils
In standard Pimple, you may define alternative handling for a callback by using a wrapper method. Clippy supports wrappers as well as a sigil notation.
// Run a function every time one reads `$c['theJoneses]`, with mix of inputs and services $c['getPassword'] = $c->method(function ($domain, SymfonyStyle $io) { ... }); $c['getPassword()'] = function ($domain, SymfonyStyle $io) { ... }; $c['getPassword']('localhost'); $c['getPassword']('example.com'); // Create a new instance every time one reads `$c['theJonses']`: $c['theJoneses'] = $c->factory(function() { return new CoolThing(microtime(1)); }); $c['theJoneses++'] = function() { return new CoolThing(microtime(1)); }; print_r($c['theJoneses']); print_r($c['theJoneses']);