/**
  * Iterates through all services of a service provider, seeding the container.
  *
  * For each service it:
  *
  * - adds the specified factory, if the service does not already exist.
  * - adds a delegator factory otherwise.
  *
  * @param ServiceProvider $provider
  * @param ServiceManager $container
  * @return ServiceManager
  * @throws RuntimeException if any factory listed is not callable.
  */
 private function marshalServiceProvider(ServiceProvider $provider, ServiceManager $container)
 {
     foreach ($provider->getServices() as $service => $factory) {
         $callable = $this->marshalCallable($provider, $factory, $service);
         if ($container->has($service)) {
             $container->addDelegator($service, $this->createDelegator($callable));
             continue;
         }
         $container->setFactory($service, $this->createFactory($callable));
     }
     return $container;
 }
Exemplo n.º 2
0
 /**
  * Registers a service provider.
  *
  * @param ServiceProvider $provider the service provider to register
  * @param array $values An array of values that customizes the provider
  *
  * @return static
  */
 public function register(ServiceProvider $provider, array $values = array())
 {
     $entries = $provider->getServices();
     foreach ($entries as $key => $callable) {
         if (isset($this->keys[$key])) {
             // Extend a previous entry
             $this[$key] = $this->extend($key, function ($previous, ContainerInterface $c) use($callable) {
                 $getPrevious = function () use($previous) {
                     return $previous;
                 };
                 return call_user_func($callable, $c, $getPrevious);
             });
         } else {
             $this[$key] = function (ContainerInterface $c) use($callable) {
                 return call_user_func($callable, $c, null);
             };
         }
     }
     foreach ($values as $key => $value) {
         $this[$key] = $value;
     }
     return $this;
 }
Exemplo n.º 3
0
 /**
  * {@inheritdoc}
  */
 public function register(ServiceProvider $provider, array $parameters = []) : ContainerContract
 {
     $entries = $provider->getServices();
     foreach ($entries as $key => $callable) {
         if ($this->has($key)) {
             // Extend a previous entry
             $this->extend($key, function ($previous, ContainerInterface $container) use($callable) {
                 $getPrevious = function () use($previous) {
                     return $previous;
                 };
                 return $callable($container, $getPrevious);
             });
         } else {
             $this->singleton($key, function (ContainerInterface $container) use($callable) {
                 return $callable($container, null);
             });
         }
     }
     foreach ($parameters as $key => $value) {
         $this->instance($key, $value);
     }
     return $this;
 }