\Charcoal\FactoryGenericFactory

The Generic Factory resolves the **class name** from an exact full qualifed name as **type**.

Class dependencies:

Name Type Description
base_class string Optional. A base class (or interface) to ensure a type of object.
default_class string Optional. A default class, as fallback when the requested object is not resolvable.
arguments array Optional. Constructor arguments that will be passed along to created instances.
callback Callable Optional. A callback function that will be called upon object creation.
resolver Callable Optional. A class resolver. If none is provided, a default will be used.
resolver_options array Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if resolver is provided.

Summary

Methods
Properties
Constants
__construct()
create()
get()
setBaseClass()
baseClass()
setDefaultClass()
defaultClass()
setArguments()
arguments()
setCallback()
callback()
resolve()
isResolvable()
No public properties found
No constants found
createClass()
resolver()
map()
addClassToMap()
$resolved
N/A
runCallbacks()
setResolver()
setMap()
$baseClass
$defaultClass
$arguments
$callback
$instances
$resolver
$map
N/A

Properties

$resolved

$resolved : array

Type

array

$baseClass

$baseClass : string

If a base class is set, then it must be ensured that the

Type

string

$defaultClass

$defaultClass : string

Type

string

$arguments

$arguments : array

Type

array

$callback

$callback : callable

Type

callable

$instances

$instances : array

Keeps loaded instances in memory, in `[$type => $instance]` format.

Used with the get() method only.

Type

array

$resolver

$resolver : Callable

Type

Callable

$map

$map : array<mixed,string>

The class map array holds available types, in `[$type => $className]` format.

Type

array<mixed,string>

Methods

__construct()

__construct(array  $data = null) 

Parameters

array $data

Constructor dependencies.

create()

create(string  $type, array  $args = null, callable  $cb = null) : mixed

Create a new instance of a class, by type.

Unlike get(), this method always return a new instance of the requested class.

Object callback

It is possible to pass a callback method that will be executed upon object instanciation. The callable should have a signature: function($obj); where $obj is the newly created object.

Parameters

string $type

The type (class ident).

array $args

Optional. Constructor arguments (will override the arguments set on the class from constructor).

callable $cb

Optional. Object callback, called at creation. Will run in addition to the default callback, if any.

Throws

\Exception

If the base class is set and the resulting instance is not of the base class.

\InvalidArgumentException

If type argument is not a string or is not an available type.

Returns

mixed —

The instance / object

get()

get(string  $type, array  $args = null) : mixed

Get (load or create) an instance of a class, by type.

Unlike create() (which always call a new instance), this function first tries to load / reuse an already created object of this type, from memory.

Parameters

string $type

The type (class ident).

array $args

The constructor arguments (optional).

Throws

\InvalidArgumentException

If type argument is not a string.

Returns

mixed —

The instance / object

setBaseClass()

setBaseClass(string  $type) : \Charcoal\Factory\FactoryInterface

If a base class is set, then it must be ensured that the created objects are `instanceof` this base class.

Parameters

string $type

The FQN of the class, or "type" of object, to set as base class.

Throws

\InvalidArgumentException

If the class is not a string or is not an existing class / interface.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

baseClass()

baseClass() : string

A base class name (or interface)

Returns

string —

The FQN of the base class

setDefaultClass()

setDefaultClass(string  $type) : \Charcoal\Factory\FactoryInterface

If a default class is set, then calling `get()` or `create()` an invalid type should return an object of this class instead of throwing an error.

Parameters

string $type

The FQN of the class, or "type" of object, to set as default class.

Throws

\InvalidArgumentException

If the class name is not a string or not a valid class.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

defaultClass()

defaultClass() : string

Returns

string —

The FQN of the default class

setArguments()

setArguments(array  $arguments) : \Charcoal\Factory\FactoryInterface

Parameters

array $arguments

The constructor arguments to be passed to the created object's initialization.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

arguments()

arguments() : array

Returns

array

setCallback()

setCallback(callable  $callback) : \Charcoal\Factory\FactoryInterface

Parameters

callable $callback

The object callback.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

callback()

callback() : callable|null

Returns

callable|null

resolve()

resolve(string  $type) : string

The Generic factory resolves the class name from an exact FQN.

Parameters

string $type

The "type" of object to resolve (the object ident).

Throws

\InvalidArgumentException

If the type parameter is not a string.

Returns

string —

The resolved class name (FQN).

isResolvable()

isResolvable(string  $type) : boolean

Wether a `type` is resolvable. The Generic Factory simply checks if the _FQN_ `type` class exists.

Parameters

string $type

The "type" of object to resolve (the object ident).

Throws

\InvalidArgumentException

If the type parameter is not a string.

Returns

boolean

createClass()

createClass(string  $classname, mixed  $args) : mixed

Create a class instance with given arguments.

How the constructor arguments are passed depends on its type:

  • if null, no arguments are passed at all.
  • if it's not an array, it's passed as a single argument.
  • if it's an associative array, it's passed as a sing argument.
  • if it's a sequential (numeric keys) array, it's

Parameters

string $classname

The FQN of the class to instanciate.

mixed $args

The constructor arguments.

Returns

mixed —

The created object.

resolver()

resolver() : callable

Returns

callable

map()

map() : array<mixed,string>

Get the map of all types in `[$type => $class]` format.

Returns

array<mixed,string>

addClassToMap()

addClassToMap(string  $type, string  $className) : \Charcoal\Factory\FactoryInterface

Add a class name to the available types _map_.

Parameters

string $type

The type (class ident).

string $className

The FQN of the class.

Throws

\InvalidArgumentException

If the $type parameter is not a striing or the $className class does not exist.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

runCallbacks()

runCallbacks(mixed  $obj, callable  $customCallback = null) : void

Run the callback(s) on the object, if applicable.

Parameters

mixed $obj

The object to pass to callback(s).

callable $customCallback

An optional additional custom callback.

setResolver()

setResolver(callable  $resolver) : \Charcoal\Factory\FactoryInterface

Parameters

callable $resolver

The class resolver instance to use.

Returns

\Charcoal\Factory\FactoryInterface

Chainable

setMap()

setMap(array<mixed,string>  $map) : \Charcoal\Factory\FactoryInterface

Add multiple types, in a an array of `type` => `className`.

Parameters

array<mixed,string> $map

The map (key=>classname) to use.

Returns

\Charcoal\Factory\FactoryInterface

Chainable