Download the PHP package xou816/silex-autowiring without Composer
On this page you can find all versions of the php package xou816/silex-autowiring. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download xou816/silex-autowiring
More information about xou816/silex-autowiring
Files in xou816/silex-autowiring
Package silex-autowiring
Short Description A service to autowire your services in Silex.
License MIT
Homepage https://github.com/xou816/silex-autowiring
Informations about the package silex-autowiring
silex-autowire
A service to autowire them all! For Silex.
Requirements
PHP 5.5+
Installation
Through Composer: composer require xou816/silex-autowiring
.
The service is then available as .
Usage
Basics
Constructor injection
Call the wire
method to let the autowiring service create your instances.
The resulting instance of Bar
can be found using $app['autowiring']->provider(Bar::class);
. If you only need the service name, use the name
method.
Every dependency of Bar
has to be known to the AutowiringService!
You can pass an array of arguments to wire
: these arguments will be passed in order wherever an argument could not be resolved to a service.
Controller injection
You can entirely avoid ever calling provider
or name
, as your wired services can be directly injected in controllers, alongside the usual Request
or Application
objects.
Closure injection
You can also inject services in a closure, using invoke
:
If you want to delay execution of a closure (that is, obtain a closure), use partial
instead:
Interfaces
The autowiring service can also inject services based on interface rather than class. If multiple services implement the same interface, the last service to be wired is used.
It is very useful to painlessly switch implementations of an interface.
Injecting other services
Exposing built-in services
If you wish to use a service shipped with Silex or a provider, you can use the expose
method. For instance:
The AutowiringService
itself is exposed, and can therefore be injected!
Custom service providers
You can control how an instance will be created using provide
.
Injecting any service
You may also inject any other service, even plain PHP objects (for which type hinting cannot be used) such as arrays or integers, as long as they are available in the Pimple container.
In order to do that, add a dependency to a SilexAutowiring\Injectable\Injectable
:
The autowiring service knows how to inject the correct service as it infers the service's name from the constructor argument's name (fooOptions
being converted from camel case to snake case).
Since an instance of Injectable
has to be passed to the constructor, you can retrieve the real service by calling get
.
If you intend to inject a configuration array, you can instead use SilexAutowiring\Injectable\Configuration
, which works the exactly like Injectable
(both implement the SilexAutowiring\Injectable\InjectableInterface
), but has array access.
Property injection
It is not possible to rely on type hinting to inject services on properties. Therefore, this feature is intended for configuration instead.
It resolves names much like with Injectable
s, but injects plain values instead.
Warning: injected values on properties are only available after construction.
Injection resolver
You might dislike the way Injectable
and configure
handle names (from snake case to camel case). It is possible to tweak this behaviour by providing (using wire
for instance!) a custom implementation of SilexAutowiring\Injectable\InjectableResolver
. This task is made easier by extending the SilexAutowiring\Injectable\AbstractCompositeKeyResolver
, which allows handling identifiers such as foo_options.baz
above.
You may also just wire
the SilexAutowiring\Injectable\IdentityResolver
class into your app to use a simpler resolution mechanism (no casing style alteration).
Experimental
You may also use the SilexAutowiring\Traits\Autowire
and SilexAutowiring\Traits\Autoconfigure
traits instead of calling wire
and configure
.
This is however not completely equivalent and likely worse in terms of performance.