Download the PHP package spiffy/spiffy-inject without Composer
On this page you can find all versions of the php package spiffy/spiffy-inject. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download spiffy/spiffy-inject
More information about spiffy/spiffy-inject
Files in spiffy/spiffy-inject
Package spiffy-inject
Short Description Spiffy\Inject is a light-weight, HHVM compatible, and dependency lite dependency injection container.
License BSD-3-Clause
Informations about the package spiffy-inject
Spiffy\Inject
Installation
Spiffy\Inject can be installed using composer which will setup any autoloading for you.
composer require spiffy/spiffy-inject
Additionally, you can download or clone the repository and setup your own autoloading.
Introduction
Spiffy\Inject is a light-weight, HHVM compatible, and dependency lite dependency injection (DI) container. You can read more about DI on Wikipedia or Martin Fowler's website. Spiffy\Inject aims to help you manage your parameters and services.
Parameters
Services
The primary purpose of Spiffy\Inject is for managing your services. You can create services in one of three ways:
- Setting using a string class name
- Setting the service directly
- Creating the service through a factory closure
- Using the array configuration
- Using an object that implements ServiceFactory
- Using annotations combined with a generator.
All services are set through the nject
method regardless of which style you choose. Each style has it's own advantages and disadvantages. It's you to you to decide which is the best approach to take for your application.
Setting Services
Testing if a service exists
Getting Services
Array Configuration
The array configuration has some additional options available to make it extremely flexible.
Constructor injection
Setter injection
Referencing other services and parameters
Spiffy\Inject's array configuration includes the ability to reference other services when the string is prepended with
special characters. By default you reference services with the @
symbol and parameters with the $
symbol. These can
be modified using the setServiceIdentifier
and setParamIdentifier
methods respectively.
ServiceFactories
If you return an instance of Spiffy\Inject\ServiceFactory
from an array configuration it will automatically create
the instance for you and return that instead. This let's you inject parameters into Service Factories easily and reuse
factories while still returning the instance you want.
Annotations combined with a generator
SpiffyInject provides annotations that you can use to assist in creating configurations for services.
Now that you have an annotated class use the metadata factory to create metadata for the class and a generator to generate the Injector configuration.
Reading files, creating metadata, and generating configuration is a heavy process and is not intended for production. You should cache the results of the generation and use the cache in production.
Decorating your services
Sometimes you want to over-ride the services set your DI container without modifying the original configuration. Spiffy\Inject handles this by providing you with two types of decorators.
Decorate
The decorate
method allows you to take the service created and apply any modifications to it prior to having it returned. The decorate closure receives the injector and service as arguments.
Alternatively, you can provide an instance of a class implementing the Spiffy\Inject\ServiceDecorator
interface:
Wrap
The wrap
method is much more powerful than decorate
. Wrapping let's you completely change the object that's created or completely bypass the original configuration. The wrap closure receives three arguments: the injector, the name of the service, and the callable that creates the service.
Alternatively, you can provide an instance of a class implementing the Spiffy\Inject\ServiceWrapper
interface:
Why nvoke and nject?
Because it's damn cute, that's why! If you prefer, though, you can use set()
instead as nject()
and get()
instead of nvoke()
.