protected function processConnection($name, array $defaults, $isDefault = FALSE) { $builder = $this->getContainerBuilder(); $config = $this->resolveConfig($defaults, $this->connectionDefaults, $this->managerDefaults); if ($isDefault) { $builder->parameters[$this->name]['dbal']['defaultConnection'] = $name; } if (isset($defaults['connection'])) { return $this->prefix('@' . $defaults['connection'] . '.connection'); } // config $configuration = $builder->addDefinition($this->prefix($name . '.dbalConfiguration'))->setClass('Doctrine\\DBAL\\Configuration')->addSetup('setResultCacheImpl', array($this->processCache($config['resultCache'], $name . '.dbalResult')))->addSetup('setSQLLogger', array(new Statement('Doctrine\\DBAL\\Logging\\LoggerChain')))->addSetup('setFilterSchemaAssetsExpression', array($config['schemaFilter']))->setAutowired(FALSE)->setInject(FALSE); // types Validators::assertField($config, 'types', 'array'); $schemaTypes = $dbalTypes = array(); foreach ($config['types'] as $dbType => $className) { $typeInst = Code\Helpers::createObject($className, array()); /** @var Doctrine\DBAL\Types\Type $typeInst */ $dbalTypes[$typeInst->getName()] = $className; $schemaTypes[$dbType] = $typeInst->getName(); } // tracy panel if ($this->isTracyPresent()) { $builder->addDefinition($this->prefix($name . '.diagnosticsPanel'))->setClass('Kdyby\\Doctrine\\Diagnostics\\Panel')->setAutowired(FALSE); } // connection $options = array_diff_key($config, array_flip(array('types', 'resultCache', 'connection', 'logging'))); $connection = $builder->addDefinition($connectionServiceId = $this->prefix($name . '.connection'))->setClass('Kdyby\\Doctrine\\Connection')->setFactory('Kdyby\\Doctrine\\Connection::create', array($options, $this->prefix('@' . $name . '.dbalConfiguration'), $this->prefix('@' . $name . '.evm')))->addSetup('setSchemaTypes', array($schemaTypes))->addSetup('setDbalTypes', array($dbalTypes))->addTag(self::TAG_CONNECTION)->addTag('kdyby.doctrine.connection')->setAutowired($isDefault)->setInject(FALSE); if ($this->isTracyPresent()) { $connection->addSetup('$panel = ?->bindConnection(?)', [$this->prefix('@' . $name . '.diagnosticsPanel'), '@self']); } /** @var Nette\DI\ServiceDefinition $connection */ $this->configuredConnections[$name] = $connectionServiceId; if (!is_bool($config['logging'])) { $fileLogger = new Statement('Kdyby\\Doctrine\\Diagnostics\\FileLogger', array($builder->expand($config['logging']))); $configuration->addSetup('$service->getSQLLogger()->addLogger(?)', array($fileLogger)); } elseif ($config['logging']) { $connection->addSetup('?->enableLogging()', array(new Code\PhpLiteral('$panel'))); } return $this->prefix('@' . $name . '.connection'); }