public function register(Application $app)
 {
     $app['di.definitions'] = array();
     $app['di.options'] = array();
     $app['di.default_options'] = array('cache' => null, 'container_class' => 'DI\\Container', 'useAnnotations' => true, 'useAutowiring' => true, 'writeProxiesToFile' => false, 'proxyDirectory' => null, 'silexAliases' => true, 'injectOnControllers' => true);
     $app['di'] = $app->share(function () use($app) {
         $acclimator = new ContainerAcclimator();
         $container = new CompositeContainer();
         $builder = $app['di.builder'];
         $builder->wrapContainer($container);
         $phpdi = $builder->build();
         $phpdi->set('Silex\\Application', $app);
         $container->addContainer($acclimator->acclimate($phpdi));
         $container->addContainer($acclimator->acclimate($app));
         return $container;
     });
     $app['di.builder'] = $app->share(function () use($app) {
         $options = $app['di.options_merged'];
         $builder = new ContainerBuilder($options['container_class']);
         $builder->useAnnotations((bool) $options['useAnnotations']);
         $builder->useAutowiring((bool) $options['useAutowiring']);
         $builder->writeProxiesToFile($options['writeProxiesToFile'], $options['proxyDirectory']);
         if ($options['cache']) {
             $builder->setDefinitionCache($options['cache']);
         }
         $definitions = (array) $app['di.definitions'];
         if ($options['silexAliases']) {
             $definitions[] = __DIR__ . '/config.php';
         }
         foreach ($definitions as $file) {
             $builder->addDefinitions($file);
         }
         return $builder;
     });
     $app['di.raw'] = $app->share(function () use($app) {
         return $app['di']->get($app['di.options_merged']['container_class']);
     });
     $app['di.options_merged'] = $app->share(function () use($app) {
         return array_merge($app['di.default_options'], $app['di.options']);
     });
     $app['resolver'] = $app->share($app->extend('resolver', function ($resolver, $app) {
         if ($app['di.options_merged']['injectOnControllers']) {
             return new PhpDiControllerResolver($app['di.raw'], $resolver);
         }
         return $resolver;
     }));
 }
Exemple #2
0
 /**
  * @param object|null $container
  */
 public function setContainer($container = null)
 {
     // Setup container
     $delegates = [$this->container, new ReflectionContainer()];
     $parentContainer = new Container();
     $parentContainer->share(ContainerInterface::class, $parentContainer);
     if ($container) {
         // Unify container to PSR11
         $acclimator = new ContainerAcclimator();
         $container = $acclimator->acclimate($container);
         array_unshift($delegates, $container);
     }
     // Bind delegates
     $delegates = array_filter($delegates);
     foreach ($delegates as $delegate) {
         $parentContainer->delegate($delegate);
     }
     $this->container = $parentContainer;
 }
 /**
  * Create service
  *
  * @param ServiceLocatorInterface $serviceLocator
  * @return Container
  */
 public function createService(ServiceLocatorInterface $serviceLocator)
 {
     if ($this->container !== null) {
         return $this->container;
     }
     $builder = new ContainerBuilder();
     $config = $this->getConfig($serviceLocator);
     $configFile = $this->getDefinitionsFilePath($config);
     $builder->addDefinitions($configFile);
     $useAnnotations = $this->shouldUseAnnotations($config);
     $builder->useAnnotations($useAnnotations);
     $acclimator = new ContainerAcclimator();
     $zfContainer = $acclimator->acclimate($serviceLocator);
     $builder->wrapContainer($zfContainer);
     /**
      * @var $cache Cache
      */
     $cache = $this->getCache($serviceLocator, $config);
     if ($cache) {
         $builder->setDefinitionCache($cache);
     }
     $this->container = $builder->build();
     return $this->container;
 }
 public function testThrowsExceptionOnContainersThatCannotBeAdpated()
 {
     $acclimator = new ContainerAcclimator();
     $this->setExpectedException('Acclimate\\Container\\Exception\\InvalidAdapterException');
     $container = $acclimator->acclimate('foo');
 }
 /**
  * @return StartableEndpoint
  */
 public function build()
 {
     $externalContainer = $this->settings->tryGet(KnownSettingsEnum::CONTAINER);
     $container = new Container();
     $acclimator = new ContainerAcclimator();
     $adaptedExternalContainer = $externalContainer ? $acclimator->acclimate($externalContainer) : null;
     $builder = new Builder($container, $adaptedExternalContainer);
     $container[BuilderInterface::class] = $builder;
     $this->ensureTransportConfigured();
     $this->ensureOutboxPersistenceConfigured();
     $this->ensureSerializationConfigured();
     $this->ensureUuidGenerationConfigured();
     $this->registerKnownFeatures();
     $this->settings->setDefault(KnownSettingsEnum::ENDPOINT_NAME, $this->endpointName);
     $this->settings->setDefault(KnownSettingsEnum::SEND_ONLY, false);
     $this->settings->set(QueueBindings::class, new QueueBindings());
     $persistenceDefinitionApplier = new PersistenceDefinitionApplier();
     $this->registerBaseContainerServices($container);
     return new StartableEndpoint($this->settings, $persistenceDefinitionApplier, $builder, $builder->build(PipelineModifications::class), $builder->build(BusContext::class));
 }