Exemple #1
0
 public function testSetCustomHydrationModes()
 {
     $this->configuration->addCustomHydrationMode('HydrationModeName', __CLASS__);
     $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('HydrationModeName'));
     $this->configuration->setCustomHydrationModes(array('AnotherHydrationModeName' => __CLASS__));
     $this->assertNull($this->configuration->getCustomHydrationMode('HydrationModeName'));
     $this->assertSame(__CLASS__, $this->configuration->getCustomHydrationMode('AnotherHydrationModeName'));
 }
 /**
  * {@inheritdoc}
  */
 protected function createWithConfig(ContainerInterface $container, $configKey)
 {
     $config = $this->retrieveConfig($container, $configKey, 'configuration');
     $configuration = new Configuration();
     $configuration->setProxyDir($config['proxy_dir']);
     $configuration->setProxyNamespace($config['proxy_namespace']);
     $configuration->setAutoGenerateProxyClasses($config['auto_generate_proxy_classes']);
     $configuration->setEntityNamespaces($config['entity_namespaces']);
     $configuration->setCustomDatetimeFunctions($config['datetime_functions']);
     $configuration->setCustomStringFunctions($config['string_functions']);
     $configuration->setCustomNumericFunctions($config['numeric_functions']);
     $configuration->setCustomHydrationModes($config['custom_hydration_modes']);
     $configuration->setClassMetadataFactoryName($config['class_metadata_factory_name']);
     foreach ($config['named_queries'] as $name => $dql) {
         $configuration->addNamedQuery($name, $dql);
     }
     foreach ($config['named_native_queries'] as $name => $query) {
         $configuration->addNamedNativeQuery($name, $query['sql'], $query['rsm']);
     }
     foreach ($config['filters'] as $name => $className) {
         $configuration->addFilter($name, $className);
     }
     $configuration->setMetadataCacheImpl($this->retrieveDependency($container, $config['metadata_cache'], 'cache', CacheFactory::class));
     $configuration->setQueryCacheImpl($this->retrieveDependency($container, $config['query_cache'], 'cache', CacheFactory::class));
     $configuration->setResultCacheImpl($this->retrieveDependency($container, $config['result_cache'], 'cache', CacheFactory::class));
     $configuration->setHydrationCacheImpl($this->retrieveDependency($container, $config['hydration_cache'], 'cache', CacheFactory::class));
     $configuration->setMetadataDriverImpl($this->retrieveDependency($container, $config['driver'], 'driver', DriverFactory::class));
     if (is_string($config['naming_strategy'])) {
         $configuration->setNamingStrategy($container->get($config['naming_strategy']));
     } elseif (null !== $config['naming_strategy']) {
         $configuration->setNamingStrategy($config['naming_strategy']);
     }
     if (is_string($config['repository_factory'])) {
         $configuration->setRepositoryFactory($container->get($config['repository_factory']));
     } elseif (null !== $config['repository_factory']) {
         $configuration->setRepositoryFactory($config['repository_factory']);
     }
     if (is_string($config['entity_listener_resolver'])) {
         $configuration->setEntityListenerResolver($container->get($config['entity_listener_resolver']));
     } elseif (null !== $config['entity_listener_resolver']) {
         $configuration->setEntityListenerResolver($config['entity_listener_resolver']);
     }
     if (null !== $config['default_repository_class_name']) {
         $configuration->setDefaultRepositoryClassName($config['default_repository_class_name']);
     }
     if ($config['second_level_cache']['enabled']) {
         $regionsConfig = new RegionsConfiguration($config['second_level_cache']['default_lifetime'], $config['second_level_cache']['default_lock_lifetime']);
         foreach ($config['second_level_cache']['regions'] as $regionName => $regionConfig) {
             if (array_key_exists('lifetime', $regionConfig)) {
                 $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
             }
             if (array_key_exists('lock_lifetime', $regionConfig)) {
                 $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
             }
         }
         $cacheFactory = new DefaultCacheFactory($regionsConfig, $configuration->getResultCacheImpl());
         $cacheFactory->setFileLockRegionDirectory($config['second_level_cache']['file_lock_region_directory']);
         $cacheConfiguration = new CacheConfiguration();
         $cacheConfiguration->setCacheFactory($cacheFactory);
         $cacheConfiguration->setRegionsConfiguration($regionsConfig);
         $configuration->setSecondLevelCacheEnabled(true);
         $configuration->setSecondLevelCacheConfiguration($cacheConfiguration);
     }
     if (is_string($config['sql_logger'])) {
         $configuration->setSQLLogger($container->get($config['sql_logger']));
     } elseif (null !== $config['sql_logger']) {
         $configuration->setSQLLogger($config['sql_logger']);
     }
     return $configuration;
 }
Exemple #3
0
 public function register(Container $c)
 {
     parent::register($c);
     $c['orm.options'] = array();
     $c['orm.types'] = array();
     $c['orm.proxies_dir'] = sys_get_temp_dir();
     $c['orm.proxies_namespace'] = 'DoctrineProxy';
     $c['orm.auto_generate_proxies'] = $c->factory(function ($c) {
         return $c['debug'];
     });
     $c['orm.custom.functions.string'] = array();
     $c['orm.custom.functions.numeric'] = array();
     $c['orm.custom.functions.datetime'] = array();
     $c['orm.custom.hydration_modes'] = array();
     $c['orm.class_metadata_factory_name'] = 'Doctrine\\ORM\\Mapping\\ClassMetadataFactory';
     $c['orm.default_repository_class'] = 'Doctrine\\ORM\\EntityRepository';
     $c['orm.initialize'] = $c->protect(function () use($c) {
         static $initialized = false;
         if ($initialized) {
             return;
         }
         $initialized = true;
         if (!isset($c['orm.options'])) {
             $c['orm.options'] = array('default' => array());
         } elseif (!is_array(reset($c['orm.options']))) {
             // Se o primeiro elemento do array de opções não for um array,
             // muito provavelmente o usuário está colocando as options de
             // uma única conexão, então usar como default
             $c['orm.options'] = array('default' => $c['orm.options']);
         }
         $tmp = $c['orm.options'];
         foreach ($tmp as $name => &$options) {
             if (!isset($options['connection'])) {
                 throw new \LogicException("Missing 'connection' param in Doctrine ORM in {$name}.");
             }
             if (is_string($options['connection'])) {
                 $options['connection'] = $c['dbal.options'][$options['connection']];
             } elseif (!is_array($options['connection']) && !$options['connection'] instanceof Connection) {
                 throw new \LogicException("Param 'connection' in {$name} must be a string, array or a Connection instance.");
             }
         }
         $c['orm.options'] = $tmp;
         $c['orm.defaultName'] = key($c['orm.options']);
     });
     $c['orm.ems'] = function ($c) {
         $c['orm.initialize']();
         $ems = new Container();
         foreach ($c['orm.options'] as $name => $options) {
             $config = $c['orm.configs'][$name];
             $connection = $options['connection'];
             $ems[$name] = function ($ems) use($connection, $config) {
                 return EntityManager::create($connection, $config);
             };
         }
         return $ems;
     };
     $c['orm.configs'] = function ($c) {
         $c['orm.initialize']();
         $configs = new Container();
         foreach ($c['orm.options'] as $name => $options) {
             $configs[$name] = $config = new Configuration();
             $config->setProxyDir($c['orm.proxies_dir']);
             $config->setProxyNamespace($c['orm.proxies_namespace']);
             $config->setAutoGenerateProxyClasses($c['orm.auto_generate_proxies']);
             $config->setCustomStringFunctions($c['orm.custom.functions.string']);
             $config->setCustomNumericFunctions($c['orm.custom.functions.numeric']);
             $config->setCustomDatetimeFunctions($c['orm.custom.functions.datetime']);
             $config->setCustomHydrationModes($c['orm.custom.hydration_modes']);
             $config->setClassMetadataFactoryName($c['orm.class_metadata_factory_name']);
             $config->setDefaultRepositoryClassName($c['orm.default_repository_class']);
             $config->setEntityListenerResolver($c['orm.entity_listener_resolver']);
             $config->setRepositoryFactory($c['orm.repository_factory']);
             $config->setNamingStrategy($c['orm.strategy.naming']);
             $config->setQuoteStrategy($c['orm.strategy.quote']);
             $chain = new MappingDriverChain();
             $config->setMetadataDriverImpl($chain);
             foreach ((array) $options['mappings'] as $entity) {
                 if (!is_array($entity)) {
                     throw new \InvalidArgumentException("The 'orm.options' option 'mappings' should be an array of arrays.");
                 }
                 if (isset($entity['alias'])) {
                     $config->addEntityNamespace($entity['alias'], $entity['namespace']);
                 }
                 switch ($entity['type']) {
                     case 'annotation':
                         if (isset($c['annotation.reader'])) {
                             $driver = new AnnotationDriver($c['annotation.reader'], (array) $entity['path']);
                         } else {
                             $simple = isset($entity['use_simple_annotation_reader']) ? $entity['use_simple_annotation_reader'] : true;
                             $driver = $config->newDefaultAnnotationDriver((array) $entity['path'], $simple);
                         }
                         break;
                     case 'yml':
                         $driver = new YamlDriver($entity['path']);
                         break;
                     case 'simple_yml':
                         $driver = new SimplifiedYamlDriver(array($entity['path'] => $entity['namespace']));
                         break;
                     case 'xml':
                         $driver = new XmlDriver($entity['path']);
                         break;
                     case 'simple_xml':
                         $driver = new SimplifiedXmlDriver(array($entity['path'] => $entity['namespace']));
                         break;
                     case 'php':
                         $driver = new StaticPHPDriver($entity['path']);
                         break;
                     default:
                         throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
                 }
                 $chain->addDriver($driver, $entity['namespace']);
             }
         }
         foreach ((array) $c['orm.types'] as $typeName => $typeClass) {
             if (Type::hasType($typeName)) {
                 Type::overrideType($typeName, $typeClass);
             } else {
                 Type::addType($typeName, $typeClass);
             }
         }
         return $configs;
     };
     $c['orm.strategy.naming'] = function ($c) {
         return new DefaultNamingStrategy();
     };
     $c['orm.strategy.quote'] = function ($c) {
         return new DefaultQuoteStrategy();
     };
     $c['orm.entity_listener_resolver'] = function ($c) {
         return new DefaultEntityListenerResolver();
     };
     $c['orm.repository_factory'] = function ($c) {
         return new DefaultRepositoryFactory();
     };
     $c->extend('doctrine.registry', function ($registry, $c) {
         $c['orm.initialize']();
         return new OrmRegistry($c['dbal.conns'], $c['orm.ems'], 'Doctrine\\Common\\Persistence\\Proxy', array_keys($c['dbal.options']), array_keys($c['orm.options']), $c['dbal.defaultName'], $c['orm.defaultName']);
     });
     // TODO passar pro Container\ValidatorExtensionableProviderInterface
     if (isset($c['validator.object_initializers'])) {
         $c->extend('validator.object_initializers', function ($initializers, $c) {
             $initializers[] = new DoctrineInitializer($c['doctrine.registry']);
             $c['doctrine.orm.validator.unique'] = new UniqueEntityValidator($c['doctrine.registry']);
             return $initializers;
         });
     }
 }