3 namespace League\Container\ServiceProvider;
5 use League\Container\ContainerAwareInterface;
6 use League\Container\ContainerAwareTrait;
8 class ServiceProviderAggregate implements ServiceProviderAggregateInterface
10 use ContainerAwareTrait;
15 protected $providers = [];
20 protected $registered = [];
25 public function add($provider)
27 if (is_string($provider) && class_exists($provider)) {
28 $provider = new $provider;
31 if ($provider instanceof ContainerAwareInterface) {
32 $provider->setContainer($this->getContainer());
35 if ($provider instanceof BootableServiceProviderInterface) {
39 if ($provider instanceof ServiceProviderInterface) {
40 foreach ($provider->provides() as $service) {
41 $this->providers[$service] = $provider;
47 throw new \InvalidArgumentException(
48 'A service provider must be a fully qualified class name or instance ' .
49 'of (\League\Container\ServiceProvider\ServiceProviderInterface)'
56 public function provides($service)
58 return array_key_exists($service, $this->providers);
64 public function register($service)
66 if (! array_key_exists($service, $this->providers)) {
67 throw new \InvalidArgumentException(
68 sprintf('(%s) is not provided by a service provider', $service)
72 $provider = $this->providers[$service];
73 $signature = get_class($provider);
75 if ($provider instanceof SignatureServiceProviderInterface) {
76 $signature = $provider->getSignature();
79 // ensure that the provider hasn't already been invoked by any other service request
80 if (in_array($signature, $this->registered)) {
84 $provider->register();
86 $this->registered[] = $signature;