- What is service locator pattern?
- Why use service locator?
- Usage of php-servicemanager
- How to install
The service locator pattern is a design pattern used in software development to encapsulate the processes involved in obtaining a service with a strong abstraction layer. This pattern uses a central registry known as the "service locator", which on request returns the information necessary to perform a certain task. -- Service locator pattern - Wikipedia, The Free Encyclopedia
Because it is easier than not to used it. Don't trust me? Let see at this code:
<?php // classes.php
class MyDatabase
{
public function __construct($dsn, $user, $password) {/* ... */}
}
class MyWeb
{
public function __construct(MyDatabase $database) {/* ... */}
}
class MyBlog
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
class MyAdmin
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
<?php // index.php
require_once("classes.php");
$config = require("config.php");
$database = new MyDatabase($config["dsn"], $config["username"], $config["password"]);
$web = new MyWeb($database);
$blog = new MyBlog($web);
$blog->show("homepage");
<?php // admin.php
require_once("classes.php");
$config = require("config.php");
$database = new MyDatabase($config["dsn"], $config["username"], $config["password"]);
$web = new MyWeb($database);
$admin = new MyAdmin($web);
$admin->show("dashboard");
And now the same code with service locator:
<?php // classes.php
use PetrKnap\Php\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;
class MyDatabase
{
public function __construct($dsn, $user, $password) {/* ... */}
}
class MyWeb
{
public function __construct(MyDatabase $database) {/* ... */}
}
class MyBlog
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
class MyAdmin
{
public function __construct(MyWeb $web) {/* ... */}
public function show($page) {/* ... */}
}
ServiceManager::setConfig([
"factories" => [
"MyDatabase" => function() {
$config = require("config.php");
return new MyDatabase($config["dsn"], $config["username"], $config["password"]);
},
"MyWeb" => function(ContainerInterface $container) {
return new MyWeb($container->get("MyDatabase"));
},
"MyBlog" => function(ContainerInterface $container) {
return new MyBlog($container->get("MyWeb"));
},
"MyAdmin" => function(ContainerInterface $container) {
return new MyBlog($container->get("MyWeb"));
}
]
]);
<?php // index.php
use PetrKnap\Php\ServiceManager\ServiceManager;
require_once("classes.php");
ServiceManager::getInstance()->get("MyBlog")->show("homepage");
<?php // admin.php
use PetrKnap\Php\ServiceManager\ServiceManager;
require_once("classes.php");
ServiceManager::getInstance()->get("MyAdmin")->show("dashboard");
<?php
use PetrKnap\Php\ServiceManager\ConfigurationBuilder;
use PetrKnap\Php\ServiceManager\ServiceManager;
use Psr\Container\ContainerInterface;
class MyCoreClass
{
/* ... */
}
class MyClass
{
private $core;
public function __construct(MyCoreClass $core)
{
$this->core = $core;
}
}
$configBuilder = new ConfigurationBuilder();
$configBuilder->addInvokable("MyCoreClass", "MyCoreClass");
$configBuilder->setShared("MyCoreClass", true);
$configBuilder->addFactory("MyClass", function(ContainerInterface $container) {
return new MyClass($container->get("MyCoreClass"));
});
ServiceManager::setConfig($configBuilder);
<?php
use PetrKnap\Php\ServiceManager\ServiceManager;
$serviceManager = ServiceManager::getInstance();
$myClass = $serviceManager->get("MyClass");
Run composer require petrknap/php-servicemanager
or merge this JSON code with your project composer.json
file manually and run composer install
. Instead of dev-master
you can use one of released versions.
{
"require": {
"petrknap/php-servicemanager": "dev-master"
}
}
Or manually clone this repository via git clone https://github.com/petrknap/php-servicemanager.git
or download this repository as ZIP and extract files into your project.