Skip to content

Commit

Permalink
Allow for service providers to be added multiple times by giving them…
Browse files Browse the repository at this point in the history
… a signature
  • Loading branch information
philipobenito committed Mar 17, 2016
1 parent 76deb40 commit aae4af9
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 39 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

All Notable changes to `League\Container` will be documented in this file

## 2.2.0

### Changed
- Service providers can now be added multiple times by giving them a signature.

## 2.1.0

### Added
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
},
"extra": {
"branch-alias": {
"dev-master": "2.1-dev",
"dev-1.x": "1.5-dev"
"dev-master": "2.x-dev",
"dev-1.x": "1.x-dev"
}
}
}
31 changes: 31 additions & 0 deletions src/ServiceProvider/AbstractSignatureServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace League\Container\ServiceProvider;

abstract class AbstractSignatureServiceProvider
extends AbstractServiceProvider
implements SignatureServiceProviderInterface
{
/**
* @var string
*/
protected $signature;

/**
* {@inheritdoc}
*/
public function withSignature($signature)
{
$this->signature = $signature;

return $this;
}

/**
* {@inheritdoc}
*/
public function getSignature()
{
return (is_null($this->signature)) ? get_class($this) : $this->signature;
}
}
8 changes: 4 additions & 4 deletions src/ServiceProvider/ServiceProviderAggregate.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@ public function register($service)
);
}

$provider = $this->providers[$service];

$provider = $this->providers[$service];
$signature = get_class($provider);
if ($provider instanceof ServiceProviderSignatureInterface) {
$signature = $provider->signature();

if ($provider instanceof SignatureServiceProviderInterface) {
$signature = $provider->getSignature();
}

// ensure that the provider hasn't already been invoked by any other service request
Expand Down
17 changes: 0 additions & 17 deletions src/ServiceProvider/ServiceProviderSignatureInterface.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/ServiceProvider/SignatureServiceProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace League\Container\ServiceProvider;

use League\Container\ContainerAwareInterface;

interface SignatureServiceProviderInterface
{
/**
* Set a custom signature for the service provider. This enables
* registering the same service provider multiple times.
*
* @param string $signature
* @return self
*/
public function withSignature($signature);

/**
* The signature of the service provider uniquely identifies it, so
* that we can quickly determine if it has already been registered.
* Defaults to get_class($provider).
*
* @return string
*/
public function getSignature();
}
23 changes: 7 additions & 16 deletions tests/Asset/SharedServiceProviderWithSignatureFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,20 @@

namespace League\Container\Test\Asset;

use League\Container\ServiceProvider\AbstractServiceProvider;
use League\Container\ServiceProvider\ServiceProviderSignatureInterface;
use League\Container\ServiceProvider\AbstractSignatureServiceProvider;
use League\Container\ServiceProvider\SignatureServiceProviderInterface;

class SharedServiceProviderWithSignatureFake extends AbstractServiceProvider implements ServiceProviderSignatureInterface
class SharedServiceProviderWithSignatureFake extends AbstractSignatureServiceProvider implements SignatureServiceProviderInterface
{
/**
* @var string
*/
private $alias;
protected $alias;

/**
* @var mixed
*/
private $item;

/**
* @var string
*/
private $signature;
protected $item;

/**
* @param string $alias
Expand All @@ -32,7 +27,8 @@ public function __construct($alias, $item, $signature)
$this->item = $item;

$this->provides[] = $alias;
$this->signature = $signature;

$this->withSignature($signature);
}

public function register()
Expand All @@ -43,9 +39,4 @@ public function register()

return true;
}

public function signature()
{
return $this->signature;
}
}

0 comments on commit aae4af9

Please sign in to comment.