The most important pre-requisite needed to use objective-php/services-factory is PHP7.
If you don't have it installed yet, please take a look at Official PHP website and read instruction about PHP7 installation on your development machine.
The easiest way to include a project with Objective PHP is to use composer's "require" feature.
The following command assumes composer is available in your current PATH:
composer require objective-php/services-factory
The documentation is being worked on... here are some examples. When used together with objective-php/application, services are usually registered by declaring them in the application configuration. Please refer to the config directives documentation to learn more about it.
<?php
use ObjectivePHP\Primitives\Collection\Collection;
use ObjectivePHP\ServicesFactory\ServicesFactory;
use ObjectivePHP\ServicesFactory\Specification\AbstractServiceSpecification;
use ObjectivePHP\ServicesFactory\Specification\ClassServiceSpecification;
use ObjectivePHP\ServicesFactory\Specification\PrefabServiceSpecification;
require 'vendor/autoload.php';
// no dependency is required to instantiate the Factory
$factory = new ServicesFactory();
// the most simple way to get a service is to call get() with an existing class name as id
// autowiring : if Some\Class::__construct() expects dependencies as arguments, the ServicesFactory will try to resolve the dependencies and instantiate the class with expected arguments
$service = $factory->get('Some\Class');
// storing an existing object in the service factory
$config = new Collection(['directive' => 'value']);
$serviceSpecs = new PrefabServiceSpecification('config', $config);
$factory->registerService($serviceSpecs);
$configService = $factory->get('config');
// same as above but using the ClassServiceSpecs
$serviceSpecs = (new ClassServiceSpecification('config', Collection::class))
->setParams(['directive' => 'value']);
$factory->registerService($serviceSpecs);
$configService = $factory->get('config');
// same again, using specs factory
$serviceSpecs = AbstractServiceSpecification::factory([
'id' => 'config',
'class' => Collection::class,
'params' => [['directive' => 'value']]
]
);
// or with
$serviceSpecs = AbstractServiceSpecification::factory([
'id' => 'config',
'instance' => $config,
'type' => PrefabServiceSpecification::class
]
);
$factory->registerService($serviceSpecs);
$configService = $factory->get('config');
// injecting dependency using setter
$serviceSpecs = (new ClassServiceSpecification('config', Collection::class))
->setSetters(['setDirective' => ['directive','value']]);
$factory->registerService($serviceSpecs);
$configService = $factory->get('config');
// using references
// create a first service
$directiveServiceSpecs = new PrefabServiceSpecification('config.directive.value', 'value');
$factory->registerService($directiveServiceSpecs);
// use it as reference for the second service
$serviceSpecs = (new ClassServiceSpecification('config', Collection::class))
->setSetters(['setDirective' => ['directive', 'service(config.directive.value)']]);
$factory->registerService($serviceSpecs);
$configService = $factory->get('config');