2017 © Pedro Peláez
 

library object-factory

Construct objects from configuration instructions

image

wikimedia/object-factory

Construct objects from configuration instructions

  • Saturday, July 28, 2018
  • by bd808
  • Repository
  • 11 Watchers
  • 0 Stars
  • 17,223 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 56 % Grown

The README.md

Wikimedia ObjectFactory

Construct objects from configuration instructions., (*1)

It can be used statically, or as a service wrapping a PSR-11 service container for lazy instantiation of objects with dependency injection., (*2)

Specification array

Contents of the specification array are as follows:, (*3)

'factory' => callable,
'class' => string,

The specification array must contain either a 'class' key with string value that specifies the class name to instantiate or a 'factory' key with a callable (is_callable() === true). If both are passed, 'factory' takes precedence but an InvalidArgumentException will be thrown if the resulting object is not an instance of the named class., (*4)

'args' => array,
'closure_expansion' => bool, // default true
'spec_is_arg' => bool, // default false
'services' => (string|null)[], // default empty
'optional_services' => (string|null)[], // default empty

The 'args' key, if provided, specifies arguments to pass to the constructor/callable. Values in 'args' which are Closure instances will be expanded by invoking them with no arguments before passing the resulting value on to the constructor/callable. This can be used to pass live objects to the constructor/callable. This behavior can be suppressed by adding closure_expansion => false to the specification., (*5)

If 'spec_is_arg' => true is in the specification, 'args' is ignored. The entire spec array is passed to the constructor/callable instead., (*6)

If 'services' is supplied and non-empty (and a service container is available), the named services are requested from the PSR-11 service container and prepended before 'args'. null values in 'services' are passed to the constructor unchanged., (*7)

Optional services declared via 'optional_services' are handled the same, except that if the service is not available from the service container null is passed as a parameter instead. Optional services are appended directly after the normal required services, (*8)

If any extra arguments are passed in the options to getObjectFromSpec() or createObject(), these are prepended before the 'services' and 'args'. These are passed using the extraArgs key, and any Closure instances that are included this way are never expanded, regardless of whether the main argument list is expanded., (*9)

'calls' => array

The specification may also contain a 'calls' key that describes method calls to make on the newly created object before returning it. This pattern is often known as "setter injection". The value of this key is expected to be an associative array with method names as keys and argument lists as values. The argument list will be expanded (or not) in the same way as the 'args' key for the main object., (*10)

Note these calls are not passed the extra arguments., (*11)

Installation

$ composer require wikimedia/object-factory

Usage

<?php

$specs = [
    // Simple constructor based injection
    'testDB' => [
        'class' => PDO::class,
            'args' => [
                'mysql:dbname=testdb;host=127.0.0.1',
                'dbuser',
                'dbpass',
            ],
    ],
];

$db = ObjectFactory::getObjectFromSpec( $specs['testDB'] ):

License

Wikimedia ObjectFactory is licensed under the GNU General Public License, version 2 and any later version (GPL-2.0-or-later). See the COPYING file for more details., (*12)

The Versions