public function createService(ServiceLocatorInterface $serviceLocator)
 {
     /** @var $options \DoctrineORMModule\Options\Configuration */
     $options = $this->getOptions($serviceLocator);
     $config = new Configuration();
     $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
     $config->setProxyDir($options->getProxyDir());
     $config->setProxyNamespace($options->getProxyNamespace());
     $config->setEntityNamespaces($options->getEntityNamespaces());
     $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
     $config->setCustomStringFunctions($options->getStringFunctions());
     $config->setCustomNumericFunctions($options->getNumericFunctions());
     $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
     foreach ($options->getNamedQueries() as $name => $query) {
         $config->addNamedQuery($name, $query);
     }
     foreach ($options->getNamedNativeQueries() as $name => $query) {
         $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']());
     }
     foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
         $config->addCustomHydrationMode($modeName, $hydrator);
     }
     foreach ($options->getFilters() as $name => $class) {
         $config->addFilter($name, $class);
     }
     $config->setMetadataCacheImpl($serviceLocator->get($options->getMetadataCache()));
     $config->setQueryCacheImpl($serviceLocator->get($options->getQueryCache()));
     $config->setResultCacheImpl($serviceLocator->get($options->getResultCache()));
     $config->setHydrationCacheImpl($serviceLocator->get($options->getHydrationCache()));
     $config->setMetadataDriverImpl($serviceLocator->get($options->getDriver()));
     if ($namingStrategy = $options->getNamingStrategy()) {
         if (is_string($namingStrategy)) {
             if (!$serviceLocator->has($namingStrategy)) {
                 throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
             }
             $config->setNamingStrategy($serviceLocator->get($namingStrategy));
         } else {
             $config->setNamingStrategy($namingStrategy);
         }
     }
     if ($repositoryFactory = $options->getRepositoryFactory()) {
         if (is_string($repositoryFactory)) {
             if (!$serviceLocator->has($repositoryFactory)) {
                 throw new InvalidArgumentException(sprintf('Repository factory "%s" not found', $repositoryFactory));
             }
             $config->setRepositoryFactory($serviceLocator->get($repositoryFactory));
         } else {
             $config->setRepositoryFactory($repositoryFactory);
         }
     }
     if ($entityListenerResolver = $options->getEntityListenerResolver()) {
         if ($entityListenerResolver instanceof EntityListenerResolver) {
             $config->setEntityListenerResolver($entityListenerResolver);
         } else {
             $config->setEntityListenerResolver($serviceLocator->get($entityListenerResolver));
         }
     }
     $this->setupDBALConfiguration($serviceLocator, $config);
     return $config;
 }
Esempio n. 2
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'));
 }
 public function loadDoctrineConfiguration(Application $app)
 {
     $app['db.orm.config'] = $app->share(function () use($app) {
         $cache = $app['db.orm.cache'];
         $config = new ORMConfiguration();
         $config->setMetadataCacheImpl($cache);
         $config->setQueryCacheImpl($cache);
         $chain = new MappingDriverChain();
         foreach ((array) $app['db.orm.entities'] as $entity) {
             switch ($entity['type']) {
                 case 'default':
                 case 'annotation':
                     $driver = $config->newDefaultAnnotationDriver((array) $entity['path'], false);
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 case 'yml':
                     $driver = new YamlDriver((array) $entity['path']);
                     $driver->setFileExtension('.yml');
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 case 'xml':
                     $driver = new XmlDriver((array) $entity['path'], $entity['namespace']);
                     $driver->setFileExtension('.xml');
                     $chain->addDriver($driver, $entity['namespace']);
                     break;
                 default:
                     throw new \InvalidArgumentException(sprintf('"%s" is not a recognized driver', $entity['type']));
                     break;
             }
         }
         $config->setMetadataDriverImpl($chain);
         $config->setProxyDir($app['db.orm.proxies_dir']);
         $config->setProxyNamespace($app['db.orm.proxies_namespace']);
         $config->setAutoGenerateProxyClasses($app['db.orm.auto_generate_proxies']);
         $config->addCustomHydrationMode('SimpleArrayHydrator', 'App\\Provider\\SimpleArrayHydrator');
         // автолоад прокси
         $proxyDir = $app['db.orm.proxies_dir'];
         $proxyNamespace = $app['db.orm.proxies_namespace'];
         Autoloader::register($proxyDir, $proxyNamespace);
         return $config;
     });
 }
 /**
  * {@inheritDoc}
  *
  * @return Configuration
  */
 public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
 {
     /** @var $options \DoctrineORMModule\Options\Configuration */
     $options = $this->getOptions($container);
     $config = new Configuration();
     $config->setAutoGenerateProxyClasses($options->getGenerateProxies());
     $config->setProxyDir($options->getProxyDir());
     $config->setProxyNamespace($options->getProxyNamespace());
     $config->setEntityNamespaces($options->getEntityNamespaces());
     $config->setCustomDatetimeFunctions($options->getDatetimeFunctions());
     $config->setCustomStringFunctions($options->getStringFunctions());
     $config->setCustomNumericFunctions($options->getNumericFunctions());
     $config->setClassMetadataFactoryName($options->getClassMetadataFactoryName());
     foreach ($options->getNamedQueries() as $name => $query) {
         $config->addNamedQuery($name, $query);
     }
     foreach ($options->getNamedNativeQueries() as $name => $query) {
         $config->addNamedNativeQuery($name, $query['sql'], new $query['rsm']());
     }
     foreach ($options->getCustomHydrationModes() as $modeName => $hydrator) {
         $config->addCustomHydrationMode($modeName, $hydrator);
     }
     foreach ($options->getFilters() as $name => $class) {
         $config->addFilter($name, $class);
     }
     $config->setMetadataCacheImpl($container->get($options->getMetadataCache()));
     $config->setQueryCacheImpl($container->get($options->getQueryCache()));
     $config->setResultCacheImpl($container->get($options->getResultCache()));
     $config->setHydrationCacheImpl($container->get($options->getHydrationCache()));
     $config->setMetadataDriverImpl($container->get($options->getDriver()));
     if ($namingStrategy = $options->getNamingStrategy()) {
         if (is_string($namingStrategy)) {
             if (!$container->has($namingStrategy)) {
                 throw new InvalidArgumentException(sprintf('Naming strategy "%s" not found', $namingStrategy));
             }
             $config->setNamingStrategy($container->get($namingStrategy));
         } else {
             $config->setNamingStrategy($namingStrategy);
         }
     }
     if ($quoteStrategy = $options->getQuoteStrategy()) {
         if (is_string($quoteStrategy)) {
             if (!$container->has($quoteStrategy)) {
                 throw new InvalidArgumentException(sprintf('Quote strategy "%s" not found', $quoteStrategy));
             }
             $config->setQuoteStrategy($container->get($quoteStrategy));
         } else {
             $config->setQuoteStrategy($quoteStrategy);
         }
     }
     if ($repositoryFactory = $options->getRepositoryFactory()) {
         if (is_string($repositoryFactory)) {
             if (!$container->has($repositoryFactory)) {
                 throw new InvalidArgumentException(sprintf('Repository factory "%s" not found', $repositoryFactory));
             }
             $config->setRepositoryFactory($container->get($repositoryFactory));
         } else {
             $config->setRepositoryFactory($repositoryFactory);
         }
     }
     if ($entityListenerResolver = $options->getEntityListenerResolver()) {
         if ($entityListenerResolver instanceof EntityListenerResolver) {
             $config->setEntityListenerResolver($entityListenerResolver);
         } else {
             $config->setEntityListenerResolver($container->get($entityListenerResolver));
         }
     }
     $secondLevelCache = $options->getSecondLevelCache();
     if ($secondLevelCache->isEnabled()) {
         $regionsConfig = new RegionsConfiguration($secondLevelCache->getDefaultLifetime(), $secondLevelCache->getDefaultLockLifetime());
         foreach ($secondLevelCache->getRegions() as $regionName => $regionConfig) {
             if (isset($regionConfig['lifetime'])) {
                 $regionsConfig->setLifetime($regionName, $regionConfig['lifetime']);
             }
             if (isset($regionConfig['lock_lifetime'])) {
                 $regionsConfig->setLockLifetime($regionName, $regionConfig['lock_lifetime']);
             }
         }
         // As Second Level Cache caches queries results, we reuse the result cache impl
         $cacheFactory = new DefaultCacheFactory($regionsConfig, $config->getResultCacheImpl());
         $cacheFactory->setFileLockRegionDirectory($secondLevelCache->getFileLockRegionDirectory());
         $cacheConfiguration = new CacheConfiguration();
         $cacheConfiguration->setCacheFactory($cacheFactory);
         $cacheConfiguration->setRegionsConfiguration($regionsConfig);
         $config->setSecondLevelCacheEnabled();
         $config->setSecondLevelCacheConfiguration($cacheConfiguration);
     }
     if ($className = $options->getDefaultRepositoryClassName()) {
         $config->setDefaultRepositoryClassName($className);
     }
     $this->setupDBALConfiguration($container, $config);
     return $config;
 }
Esempio n. 5
0
 /**
  * @param array         $settings
  * @param Configuration $configuration
  */
 protected function setCustomHydrators(array $settings = [], Configuration $configuration)
 {
     foreach (array_get($settings, 'hydrators', []) as $mode => $hydrator) {
         $configuration->addCustomHydrationMode($mode, $hydrator);
     }
 }
 protected function setupCustomFunctions(Configuration $config, $spec)
 {
     foreach ($spec['custom_hydration_modes'] as $name => $classname) {
         $config->addCustomHydrationMode($name, $classname);
     }
     foreach ($spec['custom_datetime_functions'] as $name => $classname) {
         $config->addCustomDatetimeFunction($name, $classname);
     }
     foreach ($spec['custom_numeric_functions'] as $name => $classname) {
         $config->addCustomNumericFunction($name, $classname);
     }
     foreach ($spec['custom_string_functions'] as $name => $classname) {
         $config->addCustomStringFunction($name, $classname);
     }
     foreach ($spec['filters'] as $name => $classname) {
         $config->addFilter($name, $classname);
     }
 }