writeProxiesToFile() public method

For dev environment, use writeProxiesToFile(false) (default configuration) For production environment, use writeProxiesToFile(true, 'tmp/proxies')
public writeProxiesToFile ( boolean $writeToFile, string | null $proxyDirectory = null ) : ContainerBuilder
$writeToFile boolean If true, write the proxies to disk to improve performances
$proxyDirectory string | null Directory where to write the proxies
return ContainerBuilder
Example #1
1
 /**
  * @param $env
  * @return Container
  */
 public function getContainer($env)
 {
     $container = null;
     $builder = new ContainerBuilder(Container::class);
     //        $builder = new ContainerBuilder();
     foreach (glob(APP_HOME . '/conf/conf.d/*.conf.php') as $filename) {
         $builder->addDefinitions($filename);
     }
     $definitionsEnumFile = APP_HOME . '/conf/config.' . $env . '.php';
     if (file_exists($definitionsEnumFile)) {
         $builder->addDefinitions($definitionsEnumFile);
     }
     $builder->useAnnotations(false);
     $builder->useAutowiring(true);
     switch ($env) {
         case EnvironmentEnum::DEV:
             $builder->setDefinitionCache(new ArrayCache());
             break;
         case EnvironmentEnum::PROD:
         default:
             $cache = $this->getCache();
             $builder->setDefinitionCache($cache);
             $builder->writeProxiesToFile(true, APP_HOME . '/var/cache/proxies');
             break;
     }
     $container = $builder->build();
     return $container;
 }
Example #2
0
 /**
  * @param $env
  * @return \DI\Container
  */
 public function getContainer($env)
 {
     $builder = new ContainerBuilder();
     foreach (glob(APP_HOME . '/conf/conf.d/*.conf.php') as $filename) {
         $builder->addDefinitions($filename);
     }
     $builder->addDefinitions(APP_HOME . '/conf/config.' . $env . '.php');
     switch ($env) {
         case EnvironmentEnum::DEV:
             $builder->setDefinitionCache(new ArrayCache());
             break;
         case EnvironmentEnum::PROD:
             $redis = new Redis();
             $redis->pconnect('localhost');
             $redis->setOption(Redis::OPT_PREFIX, 'homeservice:cache:di:');
             $cache = new RedisCache();
             $cache->setRedis($redis);
             $builder->setDefinitionCache($cache);
             $builder->writeProxiesToFile(true, APP_HOME . '/var/cache/proxies');
             break;
     }
     $builder->useAnnotations(true);
     $container = $builder->build();
     return $container;
 }
Example #3
0
 /**
  * Настраиваем контейнер.
  */
 protected function setContainer()
 {
     $builder = new ContainerBuilder();
     $builder->useAnnotations(false);
     if ($this->options['dependencies']) {
         $builder->addDefinitions($this->options['dependencies']);
     }
     $builder->writeProxiesToFile((bool) $this->options['containerProxy'], is_string($this->options['containerProxy']) ? $this->options['containerProxy'] : sys_get_temp_dir());
     $this->container = $builder->build();
 }
 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;
     }));
 }
 /**
  * Configure container's proxies.
  *
  * @param \DI\ContainerBuilder $containerBuilder
  * @param array                $settings
  *
  * @throws \InvalidArgumentException
  *
  * @return \DI\ContainerBuilder
  */
 private static function configureContainerProxies(DIContainerBuilder $containerBuilder, array $settings)
 {
     if (array_key_exists('proxy_path', $settings) && !empty($settings['proxy_path'])) {
         $containerBuilder->writeProxiesToFile(true, $settings['proxy_path']);
     }
     return $containerBuilder;
 }
Example #6
0
    $builder = new ContainerBuilder();
    // We will enable @Inject annotation support. Between autowiring &
    // annotations I am hoping we won't need to have much in the way of
    // custom definitions in the ```container.php``` file.
    // http://php-di.org/doc/annotations.html
    $builder->useAnnotations(true);
    // Add our definitions from ```container.php```.
    $definitions = import(__DIR__ . '/container.php');
    $builder->addDefinitions($definitions);
    // Grab the config object so we can use it to build the container.
    $config = $definitions['config']->__invoke();
    // Add definitions from a child theme that might exist.
    $parentThemePath = $config->paths->theme->parent->root;
    $childThemePath = $config->paths->theme->child->root;
    if ($parentThemePath != $childThemePath) {
        $childContainer = $childThemePath . '/container.php';
        if (file_exists($childContainer)) {
            $builder->addDefinitions(import($childContainer));
        }
    }
    // If running on staging or production we will make
    // sure the container is cached for maximum performance.
    if (s::create($config->hosting->env)->containsAny(['staging', 'production'])) {
        $builder->setDefinitionCache($config->cache->container->driver->__invoke($config));
        // NOTE: This would only be used in the case there are lazy injections.
        // see: http://php-di.org/doc/lazy-injection.html
        $builder->writeProxiesToFile(true, $config->paths->cache . '/proxies');
    }
    // Boot our theme kernel.
    $builder->build()->get(IKernel::class)->boot();
});