PHP code example of blackbonjour / service-manager
1. Go to this page and download the library: Download blackbonjour/service-manager library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
blackbonjour / service-manager example snippets
use BlackBonjour\ServiceManager\ServiceManager;
// Create a new service manager
$serviceManager = new ServiceManager();
// Add a service directly
$serviceManager->addService('config', [
'db' => [
'host' => 'localhost',
'user' => 'root',
'password' => 'secret',
],
]);
// Retrieve a service
$config = $serviceManager->get('config');
$serviceManager = new ServiceManager(
services: [], // Pre-created service instances
factories: [], // Factories for creating services
abstractFactories: [], // Abstract factories for dynamic service creation
invokables: [], // Classes that can be instantiated directly
aliases: [], // Alternative names for services
);
// Via constructor
$serviceManager = new ServiceManager([
'config' => ['debug' => true],
'logger' => new Logger(),
]);
// Via method
$serviceManager->addService('config', ['debug' => true]);
$serviceManager->addService('logger', new Logger());
// Using a factory class
$serviceManager->addFactory(Database::class, DatabaseFactory::class);
// Using a callable
$serviceManager->addFactory('logger', function($container, $requestedName) {
return new Logger();
});
// Via constructor
$serviceManager = new ServiceManager(
factories: [
Database::class => DatabaseFactory::class,
'logger' => function($container, $requestedName) {
return new Logger();
},
]
);
// Add an abstract factory
$serviceManager->addAbstractFactory(new DynamicFactory());
// Or via constructor
$serviceManager = new ServiceManager(
abstractFactories: [
new DynamicFactory(),
ReflectionFactory::class,
]
);
// Add an invokable
$serviceManager->addInvokable(stdClass::class);
// Via constructor
$serviceManager = new ServiceManager(
invokables: [
stdClass::class,
SomeClass::class,
]
);
// Add an alias
$serviceManager->addAlias('configuration', 'config');
// Via constructor
$serviceManager = new ServiceManager(
aliases: [
'configuration' => 'config',
'db' => Database::class,
]
);
// Define a factory that accepts options
$serviceManager->addFactory('database', function($container, $requestedName, $options = null) {
return new Database(
$options['host'] ?? 'localhost',
$options['user'] ?? 'root',
$options['password'] ?? 'secret'
);
});
// Create the service with options
$db = $serviceManager->createService('database', [
'host' => 'db.example.com',
'user' => 'admin',
'password' => 'password123'
]);
// Add a service
$serviceManager['config'] = ['debug' => true];
// Check if a service exists
if (isset($serviceManager['config'])) {
// Service exists
}
// Get a service
$config = $serviceManager['config'];
// Remove a service
unset($serviceManager['config']);
$serviceManager->removeService('config');
// or
unset($serviceManager['config']);
// Add the DynamicFactory
$serviceManager->addAbstractFactory(new DynamicFactory());
// Now if you request MyService, it will look for MyServiceFactory
$service = $serviceManager->get(MyService::class);
// Add the ReflectionFactory
$serviceManager->addAbstractFactory(new ReflectionFactory());
// Now you can get any class that has constructor dependencies registered in the container
$service = $serviceManager->get(MyService::class);
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.