Skip to content

Commit

Permalink
Adding support for a service prefix
Browse files Browse the repository at this point in the history
This allows users to register multiple instances of this ServiceProvider to use multiple instances of Spot.
  • Loading branch information
Dijky committed Aug 27, 2015
1 parent e03f59b commit 9a6f169
Showing 1 changed file with 24 additions and 6 deletions.
30 changes: 24 additions & 6 deletions src/Dijky/Silex/Provider/SpotServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,35 @@

class SpotServiceProvider implements ServiceProviderInterface
{
protected $servicePrefix;

public function __construct($servicePrefix = null)
{
$servicePrefix = (string)$servicePrefix;

if (strlen($servicePrefix)) {
$servicePrefix .= '.';
} else {
$servicePrefix = '';
}

$this->servicePrefix = $servicePrefix;
}

public function register(Application $app)
{
$app['spot.connections.default'] = null;
$servicePrefix = $this->servicePrefix;

$app[$servicePrefix . 'spot.connections.default'] = null;

$app['spot'] = $app->share(function() use ($app) {
return new Spot\Locator($app['spot.config']);
$app[$servicePrefix . 'spot'] = $app->share(function() use ($app, $servicePrefix) {
return new Spot\Locator($app[$servicePrefix . 'spot.config']);
});

$app['spot.config'] = $app->share(function() use ($app) {
$app[$servicePrefix . 'spot.config'] = $app->share(function() use ($app, $servicePrefix) {
$config = new Spot\Config();

$connections = $app['spot.connections'];
$connections = $app[$servicePrefix . 'spot.connections'];

// foreach does not work with a Pimple container
// like the one exposed by DoctrineServiceProvider
Expand All @@ -31,7 +48,8 @@ public function register(Application $app)
}

foreach($keys as $key) {
if(isset($app['spot.connections.default']) && $key === $app['spot.connections.default']) {
if(isset($app[$servicePrefix . 'spot.connections.default'])
&& $key === $app[$servicePrefix . 'spot.connections.default']) {
$default = true;
} else {
$default = false;
Expand Down

0 comments on commit 9a6f169

Please sign in to comment.