Skip to content

SirCameron/ServiceManager

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 

Repository files navigation

ServiceManager

A dependency and config manager for PHP

The manager uses config to define services. Services are keyed by a hash of the arguments given, which means you may have multiple instances of a service with different arguments. nice.

Usage

your config passed into the ServiceManager constructor will look like this:

$config = array(
    'myConfig'=>array(
        'foo'=>'bar'
    ),
    'ServiceManager' => array(
        'additionalConfigs' => array(
            __DIR__.'/template.config.php' // load in and combine more configs.
        ),
        'factories' => array(
            'myService' => function( $serviceManager, $someArg = null ) { // using this service with different someArg arguments will create different instances of this service
                if ($someArg == null) {
                    $someArg = __NAMESPACE__; // if you want to set some default...
                }
                $config = $serviceManager->getConfig('myService', $someArg);
                return new YourService($config);
            }
        )
    )
);

Pass that config into ServiceManager

$manager = new ServiceManager($config); // for factories, ServiceManager looks within the data at the 'ServiceManager' key of the config.

Get your service:

$service = $manager->get('myService');

The service manager also manages config, allowing you to easily get config like so:

$manager->getConfig('myConfig','foo'); // bar
$manager->getConfig('myConfig','foobooo'); // NULL

About

A dependency and config manager for PHP

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages