Overview

Namespaces

  • Charcoal
    • Factory

Classes

  • AbstractFactory
  • GenericFactory
  • GenericResolver
  • MapFactory
  • ResolverFactory

Interfaces

  • FactoryInterface
  • Overview
  • Namespace
  • Class

Class AbstractFactory

Full implementation, as Abstract class, of the FactoryInterface.

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.
Charcoal\Factory\AbstractFactory implements Charcoal\Factory\FactoryInterface

Direct known subclasses

Charcoal\Factory\GenericFactory, Charcoal\Factory\MapFactory, Charcoal\Factory\ResolverFactory
Abstract
Namespace: Charcoal\Factory
Located at Charcoal/Factory/AbstractFactory.php
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

Charcoal\Factory\FactoryInterface::create()
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
# resolver( )

Returns

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

Charcoal\Factory\FactoryInterface
Chainable

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

Charcoal\Factory\FactoryInterface
Chainable

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

Charcoal\Factory\FactoryInterface::baseClass()
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

Charcoal\Factory\FactoryInterface
Chainable

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

Charcoal\Factory\FactoryInterface::defaultClass()
public Charcoal\Factory\FactoryInterface
# setArguments( array $arguments )

Parameters

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

Returns

Charcoal\Factory\FactoryInterface
Chainable
public array
# arguments( )

Returns

array

Implementation of

Charcoal\Factory\FactoryInterface::arguments()
public Charcoal\Factory\FactoryInterface
# setCallback( callable $callback )

Parameters

$callback
The object callback.

Returns

Charcoal\Factory\FactoryInterface
Chainable
public callable|null
# callback( )

Returns

callable|null

Implementation of

Charcoal\Factory\FactoryInterface::callback()
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.
Properties summary
protected array $resolved

$resolved

$resolved

# []
API documentation generated by ApiGen