Methods summary
public
|
#
__construct( array $data = null )
Parameters
- $data
- Constructor dependencies.
|
final public
mixed
|
#
create( string $type, array $args = null, callable $cb = null )
Create a new instance of a class, by type.
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
- $type
- The type (class ident).
- $args
- Optional. Constructor arguments (will override the arguments set on the class from constructor).
- $cb
- Optional. Object callback, called at creation. Will run in addition to the default callback, if any.
Returns
mixed The instance / object
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.
Implementation of
|
protected
mixed
|
#
createClass( string $classname, mixed $args )
Create a class instance with given arguments.
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
- $classname
- The FQN of the class to instanciate.
- $args
- The constructor arguments.
Returns
mixed The created object.
|
final public
mixed
|
#
get( string $type, array $args = null )
Get (load or create) an instance of a class, by type.
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
- $type
- The type (class ident).
- $args
- The constructor arguments (optional).
Returns
mixed The instance / object
Throws
InvalidArgumentException If type argument is not a string.
|
protected
callable
|
|
protected
string[]
|
#
map( )
Get the map of all types in [$type => $class] format.
Get the map of all types in [$type => $class] format.
Returns
string[]
|
protected
Charcoal\Factory\FactoryInterface
|
#
addClassToMap( string $type, string $className )
Add a class name to the available types map.
Add a class name to the available types map.
Parameters
- $type
- The type (class ident).
- $className
- The FQN of the class.
Returns
Throws
InvalidArgumentException If the $type parameter is not a striing or the $className class does not exist.
|
public
Charcoal\Factory\FactoryInterface
|
#
setBaseClass( string $type )
If a base class is set, then it must be ensured that the created objects
are instanceof this base class.
If a base class is set, then it must be ensured that the created objects
are instanceof this base class.
Parameters
- $type
- The FQN of the class, or "type" of object, to set as base class.
Returns
Throws
InvalidArgumentException If the class is not a string or is not an existing class / interface.
|
public
string
|
#
baseClass( )
Returns
string The FQN of the base class
Implementation of
|
public
Charcoal\Factory\FactoryInterface
|
#
setDefaultClass( string $type )
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.
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
- $type
- The FQN of the class, or "type" of object, to set as default class.
Returns
Throws
InvalidArgumentException If the class name is not a string or not a valid class.
|
public
string
|
#
defaultClass( )
Returns
string The FQN of the default class
Implementation of
|
public
Charcoal\Factory\FactoryInterface
|
#
setArguments( array $arguments )
Parameters
- $arguments
- The constructor arguments to be passed to the created object's initialization.
Returns
|
public
array
|
|
public
Charcoal\Factory\FactoryInterface
|
#
setCallback( callable $callback )
Parameters
- $callback
- The object callback.
Returns
|
public
callable|null
|
#
callback( )
Returns
callable|null
Implementation of
|
public
string
|
#
resolve( string $type )
The Generic factory resolves the class name from an exact FQN.
The Generic factory resolves the class name from an exact FQN.
Parameters
- $type
- The "type" of object to resolve (the object ident).
Returns
string The resolved class name (FQN).
Throws
InvalidArgumentException If the type parameter is not a string.
|
public
boolean
|
#
isResolvable( string $type )
Wether a type is resolvable. The Generic Factory simply checks if the FQN type class exists.
Wether a type is resolvable. The Generic Factory simply checks if the FQN type class exists.
Parameters
- $type
- The "type" of object to resolve (the object ident).
Returns
boolean
Throws
InvalidArgumentException If the type parameter is not a string.
|