setDefinitionCache() public method

Enables the use of a cache for the definitions.
public setDefinitionCache ( Doctrine\Common\Cache\Cache $cache ) : ContainerBuilder
$cache Doctrine\Common\Cache\Cache Cache backend to use
return ContainerBuilder
Exemplo n.º 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;
 }
Exemplo n.º 2
0
 public function withCache(CacheProvider $cacheProvider = null)
 {
     if ($cacheProvider != null) {
         $this->builder->setDefinitionCache($cacheProvider);
     }
     return $this;
 }
Exemplo n.º 3
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;
 }
Exemplo n.º 4
0
 /**
  * @link http://php-di.org/doc/container-configuration.html
  * @throws \Exception
  * @return Container
  */
 public function create()
 {
     $builder = new ContainerBuilder();
     $builder->useAnnotations(false);
     $builder->setDefinitionCache(new ArrayCache());
     // INI config
     $builder->addDefinitions(new IniConfigDefinitionSource(Config::getInstance()));
     // Global config
     $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php');
     // Plugin configs
     $this->addPluginConfigs($builder);
     // Development config
     if (Development::isEnabled()) {
         $builder->addDefinitions(PIWIK_USER_PATH . '/config/environment/dev.php');
     }
     // User config
     if (file_exists(PIWIK_USER_PATH . '/config/config.php')) {
         $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php');
     }
     // Environment config
     $this->addEnvironmentConfig($builder);
     if (!empty($this->definitions)) {
         $builder->addDefinitions($this->definitions);
     }
     return $builder->build();
 }
Exemplo n.º 5
0
 protected function createContainer()
 {
     $builder = new ContainerBuilder();
     $builder->setDefinitionCache(new \Doctrine\Common\Cache\ArrayCache());
     $builder->addDefinitions(dirname(__DIR__) . '/DI/config.php');
     $this->container = $builder->build();
 }
 /**
  * @link http://php-di.org/doc/container-configuration.html
  * @throws \Exception
  * @return Container
  */
 public function create()
 {
     $builder = new ContainerBuilder();
     $builder->useAnnotations(false);
     $builder->setDefinitionCache(new ArrayCache());
     // INI config
     $builder->addDefinitions(new IniConfigDefinitionSource($this->settings));
     // Global config
     $builder->addDefinitions(PIWIK_USER_PATH . '/config/global.php');
     // Plugin configs
     $this->addPluginConfigs($builder);
     // Development config
     if ($this->isDevelopmentModeEnabled()) {
         $builder->addDefinitions(PIWIK_USER_PATH . '/config/environment/dev.php');
     }
     // User config
     if (file_exists(PIWIK_USER_PATH . '/config/config.php')) {
         $builder->addDefinitions(PIWIK_USER_PATH . '/config/config.php');
     }
     // Environment config
     foreach ($this->environments as $environment) {
         $this->addEnvironmentConfig($builder, $environment);
     }
     if (!empty($this->definitions)) {
         foreach ($this->definitions as $definitionArray) {
             $builder->addDefinitions($definitionArray);
         }
     }
     $container = $builder->build();
     $container->set('Piwik\\Application\\Kernel\\PluginList', $this->pluginList);
     $container->set('Piwik\\Application\\Kernel\\GlobalSettingsProvider', $this->settings);
     return $container;
 }
Exemplo n.º 7
0
 /**
  * Configure and create a container using all configuration files registered under
  * the `php-di/configuration` binding type in Puli.
  *
  * @return Container
  */
 public function createContainer()
 {
     if (!self::$puliFactoryClass && !defined('PULI_FACTORY_CLASS')) {
         throw new \RuntimeException('Puli is not installed');
     }
     // Create Puli objects
     $factoryClass = self::$puliFactoryClass ?: PULI_FACTORY_CLASS;
     $puli = new $factoryClass();
     /** @var ResourceRepository $repository */
     $repository = $puli->createRepository();
     $containerBuilder = new ContainerBuilder();
     $cache = $this->getContainerCache();
     if ($cache) {
         $containerBuilder->setDefinitionCache($cache);
     }
     // Puli objects
     $containerBuilder->addDefinitions(['puli.factory' => $puli, ResourceRepository::class => $repository, Discovery::class => function (ContainerInterface $c) {
         $puli = $c->get('puli.factory');
         return $puli->createDiscovery($c->get(ResourceRepository::class));
     }]);
     foreach ($this->modules as $module) {
         $this->loadModule($containerBuilder, $repository, $module);
     }
     if (!empty($this->config)) {
         $containerBuilder->addDefinitions($this->config);
     }
     $this->configureContainerBuilder($containerBuilder);
     return $containerBuilder->build();
 }
Exemplo n.º 8
0
 private function createOptimizedBuilder()
 {
     $cache = new ArrayCache($this->cacheDir);
     $builder = new ContainerBuilder();
     $builder->setDefinitionCache($cache);
     $builder->addDefinitions(array('bicycle_factory' => \DI\object('PhpBench\\Benchmarks\\Container\\Acme\\BicycleFactory')));
     return $builder;
 }
 public function setUp($useReflection = true, $useAnnotations = true, Cache $cache = null)
 {
     parent::setUp();
     $builder = new ContainerBuilder();
     $builder->useReflection($useReflection)->useAnnotations($useAnnotations);
     if (!is_null($cache)) {
         $builder->setDefinitionCache($cache);
     }
     $this->DIContainer = $builder->build();
     $this->registerMocks();
 }
Exemplo n.º 10
0
 /**
  * Returns the dependency injection container
  *
  * @return \DI\Container
  */
 public function getDiContainer()
 {
     if (!$this->diContainer) {
         $builder = new ContainerBuilder();
         //			$builder->setDefinitionCache(new ArrayCache());
         $builder->setDefinitionCache(new FilesystemCache(ConfigurationManager::getSharedInstance()->getConfigurationForKeyPath('cachePath')));
         $this->diContainer = $builder->build();
         $builder->addDefinitions(__DIR__ . '/Configuration/dependencyInjectionConfiguration.php');
         $this->diContainer = $builder->build();
         //			$this->diContainer = ContainerBuilder::buildDevContainer();
     }
     return $this->diContainer;
 }
Exemplo n.º 11
0
 /**
  * Returns the dependency injection container
  *
  * @return \DI\Container
  */
 public function getDiContainer()
 {
     if (!$this->diContainer) {
         $builder = new ContainerBuilder();
         //			$builder->setDefinitionCache(new \Doctrine\Common\Cache\ArrayCache());
         $builder->setDefinitionCache(new FilesystemCache(__DIR__ . '/../../var/Cache/'));
         $builder->addDefinitions(__DIR__ . '/../../Classes/Configuration/dependencyInjectionConfiguration.php');
         $this->diContainer = $builder->build();
         //			$this->diContainer = ContainerBuilder::buildDevContainer();
         $this->diContainer->get('Cundd\\PersistentObjectStore\\Event\\SharedEventEmitter');
     }
     return $this->diContainer;
 }
 /**
  * {@inheritdoc}
  */
 public function init()
 {
     parent::init();
     $environment = Config::getSystemConfig()->get("general")->get("environment");
     // Set up dependency injection container
     $builder = new ContainerBuilder();
     $builder->useAnnotations(true);
     // Configure the container
     $builder->addDefinitions(PIMCORE_WEBSITE_PATH . "/config/container.php");
     if ($environment !== "local") {
         // Use whatever cache Pimcore has configured (if it has one configured) to cache object
         // definitions in the container
         $builder->setDefinitionCache(new PimcoreCache());
     } else {
         // For development, or debugging we don't want to cache the container
         $builder->setDefinitionCache(new ArrayCache());
     }
     $environmentConfigFile = DefinitionFileLocator::getPath($environment);
     $parametersFile = ParametersFileLocator::getPath();
     if (file_exists($environmentConfigFile)) {
         $builder->addDefinitions($environmentConfigFile);
     }
     if (file_exists($parametersFile)) {
         $builder->addDefinitions($parametersFile);
     }
     $container = $builder->build();
     /** @var \Zend_Controller_Dispatcher_Interface $currentDispatcher */
     $currentDispatcher = \Zend_Controller_Front::getInstance()->getDispatcher();
     $dispatcher = new Dispatcher();
     $dispatcher->setContainer($container);
     $dispatcher->setControllerDirectory($currentDispatcher->getControllerDirectory());
     $dispatcher->setDefaultModule($currentDispatcher->getDefaultModule());
     $dispatcher->setDefaultAction($currentDispatcher->getDefaultAction());
     $dispatcher->setDefaultControllerName($currentDispatcher->getDefaultControllerName());
     \Zend_Controller_Front::getInstance()->setDispatcher($dispatcher);
 }
Exemplo n.º 13
0
 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;
     }));
 }
Exemplo n.º 14
0
 /**
  * Boots the dependency injection container.
  *
  * @param \Domynation\Config\ConfigInterface $config
  * @param \Domynation\Session\SessionInterface $session
  *
  * @return \DI\Container
  */
 private function bootContainer(ConfigInterface $config, SessionInterface $session)
 {
     $builder = new ContainerBuilder();
     $builder->useAnnotations(false);
     // Cache the definitions in production
     if (IS_PRODUCTION) {
         $builder->setDefinitionCache(new ApcuCache());
     }
     // Load all global services
     $builder->addDefinitions(array_merge(require_once __DIR__ . '/Files/container.php', [ConfigInterface::class => $config, SessionInterface::class => $session]));
     // First pass: Instanciate each providers and load their container definitions
     $providers = array_reduce($config->get('providers'), function ($accumulator, $name) use($builder) {
         if (!class_exists($name)) {
             return $accumulator;
         }
         // Instanciate the provider
         $reflection = new \ReflectionClass($name);
         $provider = $reflection->newInstanceArgs();
         // Load the provider's container definitions
         $builder->addDefinitions($provider->registerContainerDefinitions());
         $accumulator[] = $provider;
         return $accumulator;
     });
     // Build the container
     $container = $builder->build();
     // Second pass: Boot each service
     foreach ($providers as $provider) {
         // Start the provider
         $provider->start($container->get(Router::class), $container->get(ViewFactoryInterface::class), $container->get(EventDispatcherInterface::class));
         // Use reflection to get a closure copy of the `boot` method
         $reflection = new \ReflectionClass(get_class($provider));
         $closure = $reflection->getMethod('boot')->getClosure($provider);
         // Let the container inject the dependency needed by the closure
         $container->call($closure);
     }
     return $container;
 }
Exemplo n.º 15
0
 /**
  * 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;
 }
Exemplo n.º 16
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();
});
Exemplo n.º 17
0
<?php

use DI\ContainerBuilder;
use Doctrine\Common\Cache\ArrayCache;
require __DIR__ . '/../vendor/autoload.php';
$containerBuilder = new ContainerBuilder();
$containerBuilder->setDefinitionCache(new ArrayCache());
$containerBuilder->addDefinitions(__DIR__ . '/di-config.php');
$containerBuilder->useAutowiring(true);
$container = $containerBuilder->build();
return $container;
Exemplo n.º 18
0
 /**
  * Configure container's cache system.
  *
  * @param \DI\ContainerBuilder $containerBuilder
  * @param array                $settings
  *
  * @return \DI\ContainerBuilder
  */
 private static function configureContainerCache(DIContainerBuilder $containerBuilder, array $settings)
 {
     if (array_key_exists('definitions_cache', $settings)) {
         $containerBuilder->setDefinitionCache($settings['definitions_cache']);
     }
     return $containerBuilder;
 }