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', [$this->processCache($config['resultCache'], $name . '.dbalResult')])->addSetup('setSQLLogger', [new Statement('Doctrine\\DBAL\\Logging\\LoggerChain')])->addSetup('setFilterSchemaAssetsExpression', [$config['schemaFilter']])->setAutowired(FALSE)->setInject(FALSE);
// types
Validators::assertField($config, 'types', 'array');
$schemaTypes = $dbalTypes = [];
foreach ($config['types'] as $dbType => $className) {
$typeInst = Code\Helpers::createObject($className, []);
/** @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(['types', 'resultCache', 'connection', 'logging']));
$connection = $builder->addDefinition($connectionServiceId = $this->prefix($name . '.connection'))->setClass('Kdyby\\Doctrine\\Connection')->setFactory('Kdyby\\Doctrine\\Connection::create', [$options, $this->prefix('@' . $name . '.dbalConfiguration'), $this->prefix('@' . $name . '.evm')])->addSetup('setSchemaTypes', [$schemaTypes])->addSetup('setDbalTypes', [$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', [$builder->expand($config['logging'])]);
$configuration->addSetup('$service->getSQLLogger()->addLogger(?)', [$fileLogger]);
} elseif ($config['logging']) {
$connection->addSetup('?->enableLogging()', [new Code\PhpLiteral('$panel')]);
}
return $this->prefix('@' . $name . '.connection');
}