Charcoal Factory
Factories create (or build) dynamic PHP objects. Factories can resolve a type to a FQN and create instance of this class with an optional given set of arguments, while ensuring a default base class., (*1)
, (*2)
Table of contents
- How to install
- Factories
- Usage
- The resolver
- Class map / aliases
- Ensuring a type of object
- Setting a default type of object
- Constructor arguments
- Executing an object callback
- Development
- Development dependencies
- Continus integration
- Coding style
- Authors
- Changelog
- License
How to install
The preferred (and only supported) way of installing charcoal-factory is with composer:, (*3)
โ
composer require locomotivemtl/charcoal-factory
Dependencies
-
PHP 5.5+
- Older versions of PHP are deprecated, therefore not supported for charcoal-factory.
๐ Development dependencies, which are optional when using charcoal-factory, are described in the Development section of this README file., (*4)
Factories
Usage
Factories have only one purpose: to create / instanciate new PHP objects. Factory options should be set directly from the constructor:, (*5)
$factory = new \Charcoal\Factory\GenericFactory([
// Ensure the created object is a Charcoal Model
'base_class' => '\Charcoal\Model\ModelInterface',
// An associative array of class map (aliases)
'map' => [
'foo' => '\My\Foo',
'bar' => '\My\Bar'
],
// Constructor arguments
'arguments' => [
$dep1,
$dep2
],
// Object callback
'callback' => function (&obj) {
$obj->do('foo');
}
]);
// Create a "\My\Custom\Baz" object with the given arguments + callbck
$factory->create('\My\Custom\Baz');
// Create a "\My\Foo" object (using the map of aliases)
$factory->create('foo');
// Create a "\My\Custom\FooBar" object with the default resolver
$factory->create('my/custom/foo-bar');
Constructor options (class dependencies) are:, (*6)
Name |
Type |
Default |
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 |
null |
Optional. A callback function that will be called upon object creation. |
resolver |
Callable |
null [1]
|
Optional. A class resolver. If none is provided, a default will be used. |
resolver_options |
array |
null |
Optional. Resolver options (prefix, suffix, capitals and replacements). This is ignored / unused if resolver is provided. |
[1] If no resolver is provided, a default \Charcoal\Factory\GenericResolver
will be used., (*7)
The resolver
The _type_ (class identifier) sent to the create()
method can be parsed / resolved with a custom Callable
resolver., (*8)
If no resolver
is passed to the constructor, a default \Charcoal\Factory\GenericResolver
is used. This default resolver transforms, for example, my/custom/foo-bar
into \My\Custom\FooBar
., (*9)
Class map / aliases
Class aliases can be added by setting them in the Factory constructor:, (*10)
$factory = new GenericFactory([
'map' => [
'foo' => '\My\Foo',
'bar' => '\My\Bar'
]
]);
// Create a `\My\Foo` instance
$obj = $factory->create('foo');
Ensuring a type of object
Ensuring a type of object can be done by setting the base_class
property., (*11)
The recommended way of setting the base class is by setting it in the constructor:, (*12)
$factory = new GenericFactory([
'base_class' => '\My\Foo\BaseClassInterface'
]);
๐ Note that Interfaces can also be used as a factory's base class., (*13)
Setting a default type of object
It is possible to set a default type of object (default class) by setting the default_class
property., (*14)
The recommended way of setting the default class is by setting it in the constructor:, (*15)
$factory = new GenericFactory([
'default_class' => '\My\Foo\DefaultClassInterface'
]);
โ Setting a default class name changes the standard Factory behavior. When an invalid class name is used, instead of throwing an Exception
, an object of the default class type will always be returned., (*16)
Constructor arguments
It is possible to set "automatic" constructor arguments that will be passed to every created object., (*17)
The recommended way of setting constructor arguments is by passing an array of arguments to the constructor:, (*18)
$factory = new GenericFactory([
'arguments' => [
[
'logger' => $container['logger']
],
$secondArgument
]
]);
Executing an object callback
It is possible to execute an object callback upon object instanciation. A callback is any Callable
that takes the newly created object by reference as its function parameter., (*19)
// $obj is the newly created object
function callback(&$obj);
The recommended way of adding an object callback is by passing a Callable
to the constructor:, (*20)
$factory = new GenericFactory([
'arguments' => [[
'logger' => $container['logger']
]],
'callback' => function (&$obj) {
$obj->foo('bar');
$obj->logger->debug('Objet instanciated from factory.');
}
]);
Development
To install the development environment:, (*21)
โ
composer install --prefer-source
To run the scripts (phplint, phpcs and phpunit):, (*22)
โ
composer test
Development dependencies
phpunit/phpunit
squizlabs/php_codesniffer
satooshi/php-coveralls
Continuous Integration
Service |
Badge |
Description |
Travis |
 |
Runs code sniff check and unit tests. Auto-generates API documentation. |
Scrutinizer |
 |
Code quality checker. Also validates API documentation quality. |
Coveralls |
 |
Unit Tests code coverage. |
Sensiolabs |
 |
Another code quality checker, focused on PHP. |
Coding Style
All Charcoal modules follow the same coding style and charcoal-factory
is no exception. For PHP:, (*23)
Coding style validation / enforcement can be performed with composer phpcs
. An auto-fixer is also available with composer phpcbf
., (*24)
Authors
License
Charcoal is licensed under the MIT license. See LICENSE for details., (*25)
Changelog
0.3.2
- Split resolved classname "cache" by factory class.
0.3.1
Released 2016-03-22, (*26)
- Keep resolved classname in memory. Can greatly speed things up if instancing many objects.
0.3
Released 2016-01-28, (*27)
- Add the
setArguments()
method to factories.
- Add the
setCallback()
method to factories.
- Execute the callback when using the default class too.
0.2
Released 2016-01-26, (*28)
Minor (but BC-breaking) changes to Charcoal-Factory, (*29)
- Full PSR1 compliancy (All methods are now camel-case).
- Add a callback argument to the
create()
method.
-
create()
and get()
are now final
in the base abstract factory class.
- Internal code, docs and tool improvements.
0.1
Released 2015-11-25, (*30)