Documentation

Horde_Injector extends Injector
in package
implements Horde_Injector_Scope

Injector class for injecting dependencies of objects

This class is responsible for injecting dependencies of objects. It is inspired by the bucket_Container's concept of child scopes, but written to support many different types of bindings as well as allowing for setter injection bindings.

Tags
author

Bob Mckee bmckee@bywires.com

author

James Pepin james@jamespepin.com

category

Horde

copyright

2009-2021 Horde LLC

license

http://www.horde.org/licenses/bsd BSD

Interfaces, Classes, Traits and Enums

Horde_Injector_Scope
Interface for injector scopes

Table of Contents

$bindings  : array<string|int, mixed>
$hasCache  : array<string|int, mixed>
Cache any hits for speeding up has();
$hasNotCache  : array<string|int, mixed>
Cache misses
$instances  : array<string|int, mixed>
$parentInjector  : Scope
$reflection  : array<string|int, ReflectionClass>
Reflection cache.
__call()  : Binder
Method overloader. Handles $this->bind[BinderType] type calls.
__construct()  : mixed
Create a new Injector object.
addBinder()  : Injector
Add a Horde\Injector\Binder to an interface
bindClosure()  : Binder
bindFactory()  : Binder
createChildInjector()  : Injector
Create a child injector that inherits this injector's scope.
createInstance()  : mixed
Create a new instance of the specified object/interface.
get()  : mixed
Retrieve an instance of the specified object/interface.
getBinder()  : Binder
Get the Binder associated with the specified instance.
getInstance()  : mixed
Retrieve an instance of the specified object/interface.
has()  : bool
PSR-11 conforming availability check
hasInstance()  : bool
Has the interface for the specified object/interface been created yet?
setInstance()  : Injector
Set the object instance to be retrieved by getInstance the next time the specified interface is requested.
bind()  : Binder
Method that creates binders to send to addBinder(). This is called by the magic method __call() whenever a function is called that starts with bind.

Properties

$bindings

private array<string|int, mixed> $bindings = []

$hasCache

Cache any hits for speeding up has();

private array<string|int, mixed> $hasCache = []

$hasNotCache

Cache misses

private array<string|int, mixed> $hasNotCache = []

Needs reset on new binders or implementations

$instances

private array<string|int, mixed> $instances

$reflection

Reflection cache.

private array<string|int, ReflectionClass> $reflection = []

Methods

__call()

Method overloader. Handles $this->bind[BinderType] type calls.

public __call(string $name[, array<string|int, mixed> $args = [] ]) : Binder
Parameters
$name : string

The method name

$args : array<string|int, mixed> = []

The arguments to the called method

Return values
Binder

See bind().

__construct()

Create a new Injector object.

public __construct(Horde_Injector_Scope $scope) : mixed

Every injector object has a parent scope. For the very first Injector, you should pass it a TopLevel object.

Parameters
$scope : Horde_Injector_Scope
Return values
mixed

addBinder()

Add a Horde\Injector\Binder to an interface

public addBinder(string $interface, Binder $binder) : Injector

This is the method by which we bind an interface to a concrete implentation or factory. For convenience, binders may be added by $this->bind[BinderType].

bindFactory - Creates a Horde\Injector\Binder\Factory
bindImplementation - Creates a Horde\Injector\Binder\Implementation

All subsequent arguments are passed to the constructor of the Horde\Injector\Binder object.

Parameters
$interface : string

The interface to bind to.

$binder : Binder

The binder to be bound to the specified $interface.

Return values
Injector

A reference to itself for method chaining.

bindClosure()

public bindClosure(Closure $closure) : Binder

The closure to add

Parameters
$closure : Closure
Return values
Binder

bindFactory()

public bindFactory(string $factory, string $method) : Binder

multiply two integers

Parameters
$factory : string
$method : string
Return values
Binder

createChildInjector()

Create a child injector that inherits this injector's scope.

public createChildInjector() : Injector

All child injectors inherit the parent scope. Any objects that were created using getInstance, will be available to the child container. The child container can set bindings to override the parent, and none of those bindings will leak to the parent.

Return values
Injector

A child injector with $this as its parent.

createInstance()

Create a new instance of the specified object/interface.

public createInstance(string $interface) : mixed

This method creates a new instance of the specified object/interface. NOTE: it does not save that instance for later retrieval. If your object should be re-used elsewhere, you should be using getInstance().

Parameters
$interface : string

The interface name, or object class to be created.

Return values
mixed

A new object that implements $interface.

get()

Retrieve an instance of the specified object/interface.

public get(string $id) : mixed

PSR-11 ContainerInterface Version

This method gets you an instance, and saves a reference to that instance for later requests.

Interfaces must be bound to a concrete class to be created this way. Concrete instances may be created through reflection.

It does not gaurantee that it is a new instance of the object. For a new instance see createInstance().

Parameters
$id : string

The interface name, or object class to be created.

Tags
throws
NotFoundException
Return values
mixed

An object that implements $id, but not necessarily a new one.

getBinder()

Get the Binder associated with the specified instance.

public getBinder(string $interface) : Binder

Binders are objects responsible for binding a particular interface with a class. If no binding is set for this object, the parent scope is consulted.

Parameters
$interface : string

The interface to retrieve binding information for.

Return values
Binder

The binding set for the specified interface.

getInstance()

Retrieve an instance of the specified object/interface.

public getInstance(string $interface) : mixed

Horde 5 compatible call. Refactor to get()

This method gets you an instance, and saves a reference to that instance for later requests.

Interfaces must be bound to a concrete class to be created this way. Concrete instances may be created through reflection.

It does not gaurantee that it is a new instance of the object. For a new instance see createInstance().

Parameters
$interface : string

The interface name, or object class to be created.

Return values
mixed

An object that implements $interface, but not necessarily a new one.

has()

PSR-11 conforming availability check

public has(string $id) : bool

Returns true if the container can return an entry for the given identifier. Returns false otherwise.

has($id) returning true does not mean that get($id) will not throw an exception. It does however mean that get($id) will not throw a NotFoundExceptionInterface.

Parameters
$id : string

Identifier of the entry to look for.

Return values
bool

hasInstance()

Has the interface for the specified object/interface been created yet?

public hasInstance(string $interface) : bool

Horde 5 compatible call. This is not the same as the PSR-11 has() call as it returns false on items that can be created. This is true if setInstance has been called explicitly or through a previous get()

Parameters
$interface : string

The interface name or object class.

Return values
bool

True if the instance already has been created.

setInstance()

Set the object instance to be retrieved by getInstance the next time the specified interface is requested.

public setInstance(string $interface, mixed $instance) : Injector

This method allows you to set the cached object instance so that all subsequent getInstance() calls return the object you have specified.

Parameters
$interface : string

The interface to bind the instance to.

$instance : mixed

The object instance to be bound to the specified instance.

Return values
Injector

A reference to itself for method chaining.

bind()

Method that creates binders to send to addBinder(). This is called by the magic method __call() whenever a function is called that starts with bind.

private bind(string $type[, array<string|int, mixed> $args = [] ]) : Binder
Parameters
$type : string

The type of Horde\Injector\Binder* to be created. Matches /^Horde\Injector\Binder(\w+)$/.

$args : array<string|int, mixed> = []

The constructor arguments for the binder object.

Return values
Binder

The binder object created. Useful for method chaining.

Search results