marcojanssen/silex-service-register-provider

Silex provider for registering other providers

Installs: 3 411

Dependents: 1

Stars: 4

Watchers: 1

Forks: 2

Language: PHP

1.1.0 2013-09-27 12:33 UTC

README

Build Status Scrutinizer Quality Score Code Coverage

ServiceRegisterProvider is a provider for registering other providers.

Features

  • Register providers through configuration
  • Register multiple providers with the provider
  • Register a single provider with the provider

Installing

  • Install Composer

  • Add marcojanssen/silex-service-register-provider to your composer.json:

{
    "require": {
        "marcojanssen/silex-service-register-provider": "1.1.*"
    }
}
  • Install/update your dependencies

Usage

Registering a single provider

index.php

use Silex\Application;
use MJanssen\Provider\ServiceRegisterProvider;

$app = new Application();

$provider = array(
    'class' => 'MJanssen\Provider\ServiceProviderFoo',
    'values' => array(
        'foo' => 'baz'
    )
);

$serviceRegisterProvider = new ServiceRegisterProvider();
$serviceRegisterProvider->registerServiceProvider($app, $provider);

Registering multiple providers

index.php

use Silex\Application;
use MJanssen\Provider\ServiceRegisterProvider;

$app = new Application();
$serviceRegisterProvider = new ServiceRegisterProvider();

$providers = array(
    array(
        'class' => 'MJanssen\Provider\ServiceProviderFoo'
    ),
    array(
        'class' => 'MJanssen\Provider\ServiceProviderBaz'
    )
);

$serviceRegisterProvider->registerServiceProviders($app, $providers);

Registering providers with configuration

For this example the ConfigServiceProvider is used to read the yml file. The ServiceRegisterProvider picks the stored configuration through the node config.providers in $app['config.providers'] by default. If you want to set a different key, add it as parameter when instantiating the ServiceRegisterProvider

services.yml

custom.providers.key:
  validator:
    class: Silex\Provider\ValidatorServiceProvider

  controller.service:
    class: Silex\Provider\ServiceControllerServiceProvider

index.php

use Silex\Application;
use Igorw\Silex\ConfigServiceProvider;
use MJanssen\Provider\ServiceRegisterProvider;

//Set all service providers
$app->register(
    new ConfigServiceProvider(__DIR__."/../app/config/services.yml")
);

//Register all providers
$app->register(
    new ServiceRegisterProvider('custom.providers.key')
);

Note: It's recommended to use php files instead of yml/xml/etc.