Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions config/doctrine.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,22 @@
| Available: apc|array|file|memcached|redis|void
|
*/
'cache' => [
'default' => env('DOCTRINE_CACHE', 'array'),
'namespace' => null,
'second_level' => false,
'cache' => [
'second_level' => false,
'default' => 'array',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'default' => env('DOCTRINE_CACHE', 'array'),

'namespace' => null,
'metadata' => [
'driver' => env('DOCTRINE_METADATA_CACHE', 'array'),
'namespace' => null,
],
'query' => [
'driver' => env('DOCTRINE_QUERY_CACHE', 'array'),
'namespace' => null,
],
'result' => [
'driver' => env('DOCTRINE_RESULT_CACHE', 'array'),
'namespace' => null,
],
],
/*
|--------------------------------------------------------------------------
Expand Down
35 changes: 30 additions & 5 deletions src/EntityManagerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@ class EntityManagerFactory
*/
private $resolver;

/**
* @var array
*/
private $defaultCache = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary

'type' => 'array',
'namespace' => null
];

/**
* @param Container $container
* @param Setup $setup
Expand Down Expand Up @@ -93,8 +101,7 @@ public function create(array $settings = [])
{
$configuration = $this->setup->createConfiguration(
array_get($settings, 'dev', false),
array_get($settings, 'proxies.path'),
$this->cache->driver()
array_get($settings, 'proxies.path')
);

$this->setMetadataDriver($settings, $configuration);
Expand Down Expand Up @@ -318,13 +325,31 @@ protected function setCustomFunctions(Configuration $configuration)
*/
protected function setCacheSettings(Configuration $configuration)
{
if ($namespace = $this->config->get('doctrine.cache.namespace', null)) {
$this->cache->driver()->setNamespace($namespace);
}
$configuration->setQueryCacheImpl($this->applyNamedCacheConfiguration('query'));
$configuration->setResultCacheImpl($this->applyNamedCacheConfiguration('result'));
$configuration->setMetadataCacheImpl($this->applyNamedCacheConfiguration('metadata'));

$this->setSecondLevelCaching($configuration);
}

/**
* @param string $cacheName
* @return mixed
*/
private function applyNamedCacheConfiguration($cacheName)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Docblocks

{
$defaultDriver = $this->config->get('doctrine.cache.default', $this->defaultCache['type']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace $this->defaultCache['type'] by 'array'
Remove $this->defaultCache['namespace']

$defaultNamespace = $this->config->get('doctrine.cache.namespace', $this->defaultCache['namespace']);

$driver = $this->config->get('doctrine.cache.' . $cacheName . '.driver', $defaultDriver);
Copy link
Contributor

@patrickbrouwers patrickbrouwers Jan 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$cache = $this->cache->driver($driver);

if ($namespace = $this->config->get('doctrine.cache.' . $cacheName . '.namespace', $defaultNamespace)) {
    $cache->setNamespace($namespace);
}

return $cache;


if ($namespace = $this->config->get('doctrine.cache.' . $cacheName . '.namespace', $defaultNamespace)) {
$this->cache->driver($driver)->setNamespace($namespace);
}

return $this->cache->driver($driver);
}

/**
* @param Configuration $configuration
*/
Expand Down
67 changes: 60 additions & 7 deletions tests/EntityManagerFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,11 @@ class EntityManagerFactoryTest extends PHPUnit_Framework_TestCase

protected $setup;

/**
* @var array
*/
protected $caches = [ 'query', 'result', 'metadata' ];

/**
* @var array
*/
Expand All @@ -91,8 +96,19 @@ class EntityManagerFactoryTest extends PHPUnit_Framework_TestCase

protected $cacheImpl;

/**
* @Notes This just holds the length of $caches
* It's just here so we don't have to call count($caches) over and over.
* @var int
*/
private $cachesCount;

protected function setUp()
{

//Just store the count of caches
$this->cachesCount = count($this->caches);

$this->mockApp();
$this->mockMeta();
$this->mockConnection();
Expand Down Expand Up @@ -207,7 +223,8 @@ public function test_second_level_caching_can_be_enabled()
->atLeast()->once()->andReturn($cacheConfig);

$this->cacheImpl = m::mock(Cache::class);
$this->cache->shouldReceive('driver')->once()->andReturn($this->cacheImpl);
$this->cache->shouldReceive('driver')
->once()->andReturn($this->cacheImpl);

$this->configuration->shouldReceive('isSecondLevelCacheEnabled')
->atLeast()->once()
Expand All @@ -226,13 +243,23 @@ public function test_custom_cache_namespace_can_be_set()
$this->disableSecondLevelCaching();

$this->config->shouldReceive('get')
->with('doctrine.cache.namespace', null)->once()
->with('doctrine.cache.namespace', null)
->andReturn('namespace');

foreach ($this->caches as $cache) {
$this->config->shouldReceive('get')
->with('doctrine.cache.' . $cache . '.namespace', 'namespace')
->once()
->andReturn('namespace');
}

$cache = m::mock(Cache::class);
$this->cache->shouldReceive('driver')->once()->andReturn($cache);

$cache->shouldReceive('setNamespace')->once()->with('namespace');
$this->cache->shouldReceive('driver')
->times($this->cachesCount)
->andReturn($cache);

$cache->shouldReceive('setNamespace')->with('namespace');

$manager = $this->factory->create($this->settings);

Expand Down Expand Up @@ -477,6 +504,18 @@ protected function mockConfig()
{
$this->config = m::mock(Repository::class);

$this->config->shouldReceive('get')
->with('doctrine.cache.default', 'array')
->atLeast()->once()
->andReturn('array');

foreach ($this->caches as $cache) {
$this->config->shouldReceive('get')
->with('doctrine.cache.' . $cache . '.driver', 'array')
->atLeast()->once()
->andReturn('array');
}

$this->config->shouldReceive('has')
->with('database.connections.mysql')
->once()
Expand Down Expand Up @@ -505,7 +544,9 @@ protected function mockConfig()
protected function mockCache()
{
$this->cache = m::mock(CacheManager::class);
$this->cache->shouldReceive('driver')->once()->andReturn(new ArrayCache());
$this->cache->shouldReceive('driver')
->times($this->cachesCount)
->andReturn(new ArrayCache());
}

protected function mockConnection()
Expand Down Expand Up @@ -565,8 +606,16 @@ protected function disableSecondLevelCaching()
protected function disableCustomCacheNamespace()
{
$this->config->shouldReceive('get')
->with('doctrine.cache.namespace', null)->atLeast()->once()
->andReturn(false);
->with('doctrine.cache.namespace', null)
->atLeast()->once()
->andReturn(null);

foreach ($this->caches as $cache) {
$this->config->shouldReceive('get')
->with('doctrine.cache.' . $cache . '.namespace', null)
->atLeast()->once()
->andReturn(null);
}
}

protected function disableCustomFunctions()
Expand Down Expand Up @@ -595,6 +644,10 @@ protected function mockORMConfiguration()
->atLeast()->once()
->andReturn('Doctrine\ORM\Mapping\ClassMetadataFactory');

$this->configuration->shouldReceive('setMetadataCacheImpl')->once();
$this->configuration->shouldReceive('setQueryCacheImpl')->once();
$this->configuration->shouldReceive('setResultCacheImpl')->once();

$cache = m::mock(Cache::class);
$this->configuration->shouldReceive('getMetadataCacheImpl')
->atLeast()->once()
Expand Down