\Phabstractic\PatternsAbstractFactory

AbstractFactory Class - Creates and Defines OO-defined AbstractFactory Classes/Objects

The AbstractFactory class which when instantiated can define an abstract factory that can then be 'baked' into the appropriate namespace (or global namespace) and accessed, once baked, as an AbstractFactory class of itself.

A static 'registry' makes sure that the programmer doesn't try to instantiate a fully qualified AbstractFactory twice, and throws an error.

CHANGELOG

1.0 created AbstractFactory - May 27th, 2013 2.0: Integrated into Primus2 - August 30th, 2015 3.0: configuration information is now in one place, the constructor refactored createAbstractFactory to access object properties clarified all exceptions as coming from patterns/exception reformatted for inclusion into phabstractic - July 20th, 2016

Summary

Methods
Properties
Constants
configure()
saveSettings()
getSettings()
processSettings()
__construct()
bake()
setFactoryName()
getFactoryName()
isBaked()
setConstants()
getConstants()
addConstant()
addConstants()
removeConstant()
setMethods()
getMethods()
addMethod()
addMethods()
removeMethod()
setNamespace()
getNamespace()
__debugInfo()
buildAbstractFactory()
No public properties found
No constants found
No protected methods found
$conf
N/A
createAbstractFactory()
getFQN()
setUpFactories()
$factories
$factoryName
$baked
$constants
$methods
$namespace
N/A

Properties

$conf

$conf : \Phabstractic\Features\Zend\Config\Config

The objects configuration object

Should be an instance of Zend/Config

Type

\Phabstractic\Features\Zend\Config\Config — The object configuration info

$factories

$factories : \Phabstractic\Patterns\Phabstractic\Data\Types\Set

Keeps track of already defined AbstractFactory classes internally

Type

\Phabstractic\Patterns\Phabstractic\Data\Types\Set

$factoryName

$factoryName : string

The name of the factory to be generated

Type

string

$baked

$baked : boolean

Whether the class has defined itself

Type

boolean

$constants

$constants : array

The constants, name => value, of the enumeration

Type

array

$methods

$methods : \Phabstractic\Patterns\Phabstractic\Data\Types\Set

The abstract methods (turned into-> makeMethodName)

Type

\Phabstractic\Patterns\Phabstractic\Data\Types\Set

$namespace

$namespace : string

The namespace under which the abstract factory class is to be defined

Type

string

Methods

configure()

configure(array|string|\Phabstractic\Features\Zend\Config\Config  $configuration, string  $format = null, mixed  $context = null) : boolean

Configure An Object

Expects an array for configuration however, you can also pass it a filepath where it will read the information from a file automatically detecting the format using the file extension. You can also pass a Zend/Config object already made.

You can also pass a format specifier (forced format) for use in a if $configuration is a string formatted with such information. E.G. to load from a string in the format ini:

$this->configure($configString, 'ini');

The $context argument is used for any additional reader constructor information, such as the constructor for the 'yaml' format.

NOTE: You can override/extend the classes used for reading formats by identifying an additional array in the property $this->configReaders. This will merge with the standard formats array.

Parameters

array|string|\Phabstractic\Features\Zend\Config\Config $configuration

The objects configuration information.

string $format

The forced format, or format for configuration string

mixed $context

Any additional information for a reader constructor, such as needed for the YAML format.

Returns

boolean —

True if instantiated

saveSettings()

saveSettings(string  $file, \Phabstractic\Features\Zend\Config\Writer\WriterInterface  $writer = null, boolean  $exclusive = true, mixed  $context = null) 

Save an Object's Configuration to a File

Takes an objects $conf property and writes the information contained therein to a file with a format automatically specified by the filename.

It is possible to retrieve a string of a particular format from this method by specifying the filename '#string' with an extension indicating the desired format, such as '#string.json'.

The $context argument is used for any additional reader constructor information, such as the constructor for the 'yaml' format.

NOTE: You can override/extend the classes used for writing formats by identifying an additional array in the property $this->configWriters. This will merge with the standard formats array.

Parameters

string $file

The file path to write to, or '#string.ext'

\Phabstractic\Features\Zend\Config\Writer\WriterInterface $writer

The optional writer object supplied to use (such as a MySQL writer)

boolean $exclusive

Argument provided to toFile(), file exclusive lock when writing

mixed $context

Any additionla writer constructor information (YAML)

getSettings()

getSettings(string  $format,   $context = null) : string|boolean

Retrieve an Object's Configuration Information As String

This is a shortcut to ::saveSettings() which specifies a format and forces the return of a string, using the #string.ext filename -see documentation for ::saveSettings()-

Parameters

string $format

The format to return, must be supported by ::saveSettings(), use $this->configWriters to support additional formats.

$context

Returns

string|boolean —

The formatted string, or false otherwise

processSettings()

processSettings(\Phabstractic\Features\Zend\Config\Processor\ProcessorInterface  $processor) 

Process an Object's Configuration

This uses a Zend\Config\Processor implementation to process the configuration information, such as constants. The processor must be supplied and implement ProcessorInterface

NOTE: Edits the $conf object in place.

Parameters

\Phabstractic\Features\Zend\Config\Processor\ProcessorInterface $processor

The given processor object

__construct()

__construct(  $factoryName, array  $methods = array(), array  $constants = array(), array  $options = array()) 

AbstractFactory construction, Set Up abstract factory parameters

This sets up the parameters for the abstractfactory to be generated It only generas the class when the option 'bake' is set to true

Options: strict => throw errors namespace => namespace to define this enumerator in bake => define the class immediately with the given parameters

Parameters

$factoryName
array $methods

The methods of the class, turned into makeMethod

array $constants

The constants if any defined for the class ($key = $value)

array $options

See above options

bake()

bake() : boolean

Generates the abstract factory class in the desired namespace

Generates the abstract factory class in the desired namespace using the given parameters, in essence solidifying them in place. Once an abstract factory class is 'baked' it cannot be changed.

This function also pushes the qualified name onto a static set so that future abstract factories defined through this method don't clash in name, allowing the class ro raise a RangeException

Returns

boolean —

Success or failure?

setFactoryName()

setFactoryName(string  $factoryName) 

Sets the name of the class to be generated

Checks to make sure the proposed fully qualified name doesn't clash with an already created enumerator object, raising a RangeException error

Remember, factoryName becomes Abstract{FactoryName}Factory

Parameters

string $factoryName

The name of the class to be generated

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RangeException

when the qualified name clashes with an already created enumerator class

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

when the class has already been baked

getFactoryName()

getFactoryName() : string

Retrieve factory name

Returns

string —

The factory name of the typed object

isBaked()

isBaked() : boolean

Check to see if class has been defined or 'baked'

Returns

boolean —

Whether the abstract factory has been baked.

setConstants()

setConstants(array  $constants) 

Sets any desired constants to be defined in the abstract factory

Parameters

array $constants

An array containing the constant names => values

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

when the class has already been generated (baked)

getConstants()

getConstants() : array

Gets the abstract factory elements (constants)

Returns

array —

Array of abstract factory constants

addConstant()

addConstant(string  $name, mixed  $value) 

Add a single constant to the constant list

This method overrides any constant names already defined with the new proposed values

Parameters

string $name

The name of the constant

mixed $value

May be anything a constant can be defined as

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

addConstants()

addConstants(array  $constants) 

Add multiple constants to the constant list

This method employs the array structure identifier => value. Overrides any constant names already defined with the new proposed values

Parameters

array $constants

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

removeConstant()

removeConstant(  $name) 

Remove a constant from the constant list

This method removes a constant from the constant list only if the class has not been generated, otherwise it throws an error

Parameters

$name

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

setMethods()

setMethods(array  $methods) 

Sets the abstract factory methods

Remember, method names are transformed into makeMethod

Parameters

array $methods

An array containing the method names

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

when the class has already been generated (baked)

getMethods()

getMethods() : array

Gets the abstract factory methods

Returns

array —

Array of abstract factory methods

addMethod()

addMethod(string  $method) 

Add a single method to the method list

This method overrides any method names already defined with the new proposed values

Remember: method becomes makeMethod

Parameters

string $method

The name of the method

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

addMethods()

addMethods(array  $methods) 

Add multiple methods to the method list

This method employs the array structure identifier => value. Overrides any method names already defined with the new proposed values

Parameters

array $methods

The array of methods to add to the list

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

removeMethod()

removeMethod(string  $method) 

Remove a method from the method list

This method removes a method from the method list only if the class has not been generated, otherwise it throws an error

Remember: NOT makeMethod, just method

Parameters

string $method

Name of the method

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been baked

setNamespace()

setNamespace(string  $namespace) 

Sets up the namespace to be used when generating the abstract factory class

This sets the namespace that will be used when the enumerator class is generated How? Eval statements operate in the global namespace, or basically a clean slate enabling us to put a namespace statement at the beginning of our generated code Any namespace will do, if there is no namespace, you must access your abstract factory using the global namespace. ex: \Months::January, \Colors::Red, ...

Parameters

string $namespace

The namespace specified

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RangeException

when the class/namespace has already been generated via Enum

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\RuntimeException

if the class has already been generated/baked.

getNamespace()

getNamespace() : string

Retrieves the namespace for the generated abstract factory

Returns

string —

The namespace for the generated abstract factory

__debugInfo()

__debugInfo() 

Debug Info (var_dump)

Display debug info

Requires PHP 5.6+

buildAbstractFactory()

buildAbstractFactory(string  $factoryName, array  $methods, array  $constants = array(), array  $options = array()) 

Straight out define the abstract factory class without instantiating

Takes care of the instantiation and defines the abstract factory according to parameters

Parameters

string $factoryName

The name of the abstract factory class (remember Abstract{FactoryName}Factory)

array $methods

The methods to be defined in the factory

array $constants

The constants to be defined in the factory

array $options

See (__construct)

createAbstractFactory()

createAbstractFactory() : boolean

Generates the required class code and evaluates it

The function that pieces the generated code together and evaluates it

Evaluations: $this->factoryName The name of the generate class (is turned into AbstractFactoryNameFactory) $this->methods The methods to put in the factory (are turned into makeMethodName) $this->constants The constants to put in the factory (are turned uppercase)

Throws

\Phabstractic\Patterns\Phabstractic\Patterns\Exception\CodeGenerationException

if class couldn't be generated

Returns

boolean —

if abstract factory class is generated without errors

getFQN()

getFQN(  $factoryName = '',   $namespace = '') : string

Construct FQN for Factory based on properties

Parameters

$factoryName
$namespace

Returns

string

setUpFactories()

setUpFactories() 

Make sure self::factories exists