Skip to content
Permalink
Browse files

DbalExtension: requires cache configured

  • Loading branch information
mabar authored and f3l1x committed Nov 26, 2019
1 parent 59af5ec commit 4c41e696d329503cf0edf14fdad64863e03e86fb
@@ -54,7 +54,7 @@ dbal:
sourcePaths: [%appDir%]
configuration:
sqlLogger: NULL
resultCacheImpl: NULL
resultCache: NULL
filterSchemaAssetsExpression: NULL
autoCommit: TRUE
@@ -21,12 +21,13 @@
],
"require": {
"php": "^7.2",
"nette/di": "~3.0.0",
"contributte/di": "^0.4.0",
"doctrine/dbal": "^2.9.2"
},
"require-dev": {
"contributte/console": "^0.5.0",
"mockery/mockery": "^1.2.2",
"nettrine/cache": "^0.1.0",
"ninjify/qa": "^0.9.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan-deprecation-rules": "^0.11.0",
@@ -2,12 +2,15 @@

namespace Nettrine\DBAL\DI;

use Contributte\DI\Helper\ExtensionDefinitionsHelper;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\Common\EventSubscriber;
use Doctrine\DBAL\Configuration;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Logging\LoggerChain;
use Nette\DI\CompilerExtension;
use Nette\DI\Definitions\Definition;
use Nette\DI\Definitions\ServiceDefinition;
use Nette\DI\Definitions\Statement;
use Nette\PhpGenerator\PhpLiteral;
@@ -37,7 +40,7 @@ public function getConfigSchema(): Schema
]),
'configuration' => Expect::structure([
'sqlLogger' => Expect::type('string|' . Statement::class),
'resultCacheImpl' => Expect::type('string|' . Statement::class),
'resultCache' => Expect::anyOf(Expect::string(), Expect::array(), Expect::type(Statement::class)),
'filterSchemaAssetsExpression' => Expect::string()->nullable(),
'autoCommit' => Expect::bool(true),
]),
@@ -65,6 +68,7 @@ public function loadDoctrineConfiguration(): void
{
$builder = $this->getContainerBuilder();
$config = $this->config->configuration;
$definitionsHelper = new ExtensionDefinitionsHelper($this->compiler);

$logger = $builder->addDefinition($this->prefix('logger'))
->setType(LoggerChain::class)
@@ -80,11 +84,23 @@ public function loadDoctrineConfiguration(): void
$logger->addSetup('addLogger', [$config->sqlLogger]);
}

// ResultCacheImpl
if ($config->resultCacheImpl !== null) {
$configuration->addSetup('setResultCacheImpl', [$config->resultCacheImpl]);
// ResultCache
if ($config->resultCache !== null) {
$resultCacheName = $this->prefix('resultCache');
$resultCacheDefinition = $definitionsHelper->getDefinitionFromConfig($config->cache, $resultCacheName);

// If service is extension specific, then disable autowiring
if ($resultCacheDefinition instanceof Definition && $resultCacheDefinition->getName() === $resultCacheName) {
$resultCacheDefinition->setAutowired(false);
}
} else {
$resultCacheDefinition = '@' . Cache::class;
}

$configuration->addSetup('setResultCacheImpl', [
$resultCacheDefinition,
]);

// FilterSchemaAssetsExpression
if ($config->filterSchemaAssetsExpression !== null) {
$configuration->addSetup('setFilterSchemaAssetsExpression', [$config->filterSchemaAssetsExpression]);
@@ -6,6 +6,7 @@
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Tests\Toolkit\TestCase;
use Tracy\Bridges\Nette\TracyExtension;
@@ -18,7 +19,13 @@ public function testQueryBuilder(): void
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('tracy', new TracyExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_PATH,
],
]);
}, 'int1');

/** @var Container $container */
@@ -7,6 +7,8 @@
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\InvalidStateException;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\DBAL\Events\DebugEventManager;
use Tests\Toolkit\TestCase;
@@ -20,8 +22,12 @@ public function testDebugMode(): void
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('tracy', new TracyExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_PATH,
],
'dbal' => [
'debug' => [
'panel' => true,
@@ -43,8 +49,14 @@ public function testServerVersion(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig(['dbal' => ['connection' => ['driver' => 'pdo_pgsql', 'serverVersion' => '10.0']]]);
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_PATH,
],
'dbal' => ['connection' => ['driver' => 'pdo_pgsql', 'serverVersion' => '10.0']],
]);
}, 'di2');

/** @var Container $container */
@@ -57,4 +69,17 @@ public function testServerVersion(): void
$this->assertFalse($connection->isConnected());
}

public function testNoCache(): void
{
$this->expectException(InvalidStateException::class);
$this->expectExceptionMessage('Service \'dbal.configuration\' (type of Doctrine\DBAL\Configuration): Service of type \'Doctrine\Common\Cache\Cache\' not found.');

$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('dbal', new DbalExtension());
}, 'di3');

new $class();
}

}
@@ -7,6 +7,7 @@
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Tests\Fixtures\Subscriber\PostConnectSubscriber;
use Tests\Toolkit\NeonLoader;
@@ -19,7 +20,13 @@ public function testPostConnectEvent(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addConfig([
'parameters' => [
'tempDir' => TEMP_PATH,
],
]);
$compiler->addConfig(NeonLoader::load('
dbal:
connection:

0 comments on commit 4c41e69

Please sign in to comment.
You can’t perform that action at this time.