drinky78 / silex-service-register-provider
Silex provider for registering other providers
Installs: 20
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 4
pkg:composer/drinky78/silex-service-register-provider
Requires
- silex/silex: 1.*
Suggests
- igorw/config-service-provider: Allows registering providers with configuration
This package is not auto-updated.
Last update: 2025-10-21 12:52:34 UTC
README
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-providerto yourcomposer.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.