Skip to content

Commit ed08f53

Browse files
committed
Merge branch '1.4' into 1.5
2 parents bcae6f3 + e3d8692 commit ed08f53

File tree

7 files changed

+266
-10
lines changed

7 files changed

+266
-10
lines changed

config/doctrine.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@
156156
| Configure meta-data, query and result caching here.
157157
| Optionally you can enable second level caching.
158158
|
159-
| Available: apc|array|file|memcached|php_file|redis|void
159+
| Available: apc|array|file|illuminate|memcached|php_file|redis|void
160160
|
161161
*/
162162
'cache' => [

src/Configuration/Cache/FileCacheProvider.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Doctrine\Common\Cache\FilesystemCache;
66
use Illuminate\Contracts\Config\Repository;
77
use LaravelDoctrine\ORM\Configuration\Driver;
8+
use function storage_path;
89

910
class FileCacheProvider implements Driver
1011
{
@@ -28,8 +29,10 @@ public function __construct(Repository $config)
2829
*/
2930
public function resolve(array $settings = [])
3031
{
32+
$path = $settings['path'] ?? $this->config->get('cache.stores.file.path', storage_path('framework/cache'));
33+
3134
return new FilesystemCache(
32-
$this->config->get('cache.stores.file.path', storage_path('framework/cache'))
35+
$path
3336
);
3437
}
3538
}

src/Configuration/Cache/IlluminateCacheProvider.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@
22

33
namespace LaravelDoctrine\ORM\Configuration\Cache;
44

5+
use const E_USER_DEPRECATED;
56
use Illuminate\Contracts\Cache\Factory;
7+
use InvalidArgumentException;
68
use LaravelDoctrine\ORM\Configuration\Driver;
79

8-
abstract class IlluminateCacheProvider implements Driver
10+
class IlluminateCacheProvider implements Driver
911
{
1012
/**
1113
* @var Factory
@@ -32,8 +34,18 @@ public function __construct(Factory $cache)
3234
*/
3335
public function resolve(array $settings = [])
3436
{
37+
$store = $this->store ?? $settings['store'] ?? null;
38+
39+
if ($store === null) {
40+
throw new InvalidArgumentException('Please specify the `store` when using the "illuminate" cache driver.');
41+
}
42+
43+
if ($this->store && isset($settings['store'])) {
44+
trigger_error('Using driver "' . $this->store . '" with a custom store is deprecated. Please use the "illuminate" driver.', E_USER_DEPRECATED);
45+
}
46+
3547
return new IlluminateCacheAdapter(
36-
$this->cache->store($this->store)
48+
$this->cache->store($store)
3749
);
3850
}
3951
}

src/Configuration/Cache/PhpFileCacheProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public function __construct(Repository $config)
2929
*/
3030
public function resolve(array $settings = [])
3131
{
32+
$path = $settings['path'] ?? $this->config->get('cache.stores.file.path', storage_path('framework/cache'));
33+
3234
return new PhpFileCache(
33-
$this->config->get('cache.stores.file.path', storage_path('framework/cache'))
35+
$path
3436
);
3537
}
3638
}

src/EntityManagerFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,10 @@ private function applyNamedCacheConfiguration($cacheName)
360360
$defaultDriver = $this->config->get('doctrine.cache.default', 'array');
361361
$defaultNamespace = $this->config->get('doctrine.cache.namespace');
362362

363-
$driver = $this->config->get('doctrine.cache.' . $cacheName . '.driver', $defaultDriver);
363+
$settings = $this->config->get('doctrine.cache.' . $cacheName, []);
364+
$driver = $settings['driver'] ?? $defaultDriver;
364365

365-
$cache = $this->cache->driver($driver);
366+
$cache = $this->cache->driver($driver, $settings);
366367

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

src/Testing/ConfigRepository.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace LaravelDoctrine\ORM\Testing;
44

55
use Illuminate\Contracts\Config\Repository;
6+
use Illuminate\Support\Arr;
67

78
class ConfigRepository implements Repository
89
{
@@ -23,7 +24,7 @@ public function all()
2324

2425
public function get($key, $default = null)
2526
{
26-
return $this->items[$key] ?? $default;
27+
return Arr::get($this->items, $key, $default);
2728
}
2829

2930
public function set($key, $value = null)

tests/EntityManagerFactoryTest.php

Lines changed: 239 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Illuminate\Contracts\Config\Repository;
1717
use Illuminate\Contracts\Container\Container;
1818
use LaravelDoctrine\ORM\Configuration\Cache\CacheManager;
19+
use LaravelDoctrine\ORM\Configuration\Cache\IlluminateCacheAdapter;
1920
use LaravelDoctrine\ORM\Configuration\Connections\ConnectionManager;
2021
use LaravelDoctrine\ORM\Configuration\LaravelNamingStrategy;
2122
use LaravelDoctrine\ORM\Configuration\MetaData\MetaDataManager;
@@ -485,6 +486,242 @@ public function test_can_set_repository_factory()
485486
$this->assertEntityManager($manager);
486487
}
487488

489+
public function test_illuminate_cache_provider_custom_store()
490+
{
491+
m::resetContainer();
492+
493+
$config = new ConfigRepository([
494+
'database.connections.mysql' => [
495+
'driver' => 'mysql'
496+
],
497+
'doctrine' => [
498+
'meta' => 'annotations',
499+
'connection' => 'mysql',
500+
'paths' => ['Entities'],
501+
'proxies' => [
502+
'path' => 'dir',
503+
'auto_generate' => false,
504+
'namespace' => 'namespace'
505+
],
506+
507+
'cache' => [
508+
'metadata' => [
509+
'driver' => 'illuminate',
510+
'store' => 'myStoreName'
511+
]
512+
]
513+
],
514+
'doctrine.custom_datetime_functions' => [],
515+
'doctrine.custom_numeric_functions' => [],
516+
'doctrine.custom_string_functions' => []
517+
]);
518+
519+
$container = new \Illuminate\Container\Container();
520+
$container->singleton(Repository::class, function () use ($config) {
521+
return $config;
522+
});
523+
524+
$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);
525+
526+
$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
527+
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);
528+
529+
$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
530+
return $factory;
531+
});
532+
533+
$factory = new EntityManagerFactory(
534+
$container,
535+
new Setup(),
536+
new MetaDataManager($container),
537+
new ConnectionManager($container),
538+
new CacheManager($container),
539+
$config,
540+
new EntityListenerResolver($container)
541+
);
542+
543+
$manager = $factory->create($config->get('doctrine'));
544+
545+
$this->assertInstanceOf(IlluminateCacheAdapter::class, $manager->getConfiguration()->getMetadataCacheImpl());
546+
}
547+
548+
public function test_illuminate_cache_provider_redis()
549+
{
550+
m::resetContainer();
551+
552+
$config = new ConfigRepository([
553+
'database.connections.mysql' => [
554+
'driver' => 'mysql'
555+
],
556+
'doctrine' => [
557+
'meta' => 'annotations',
558+
'connection' => 'mysql',
559+
'paths' => ['Entities'],
560+
'proxies' => [
561+
'path' => 'dir',
562+
'auto_generate' => false,
563+
'namespace' => 'namespace'
564+
],
565+
566+
'cache' => [
567+
'metadata' => [
568+
'driver' => 'redis',
569+
]
570+
]
571+
],
572+
'doctrine.custom_datetime_functions' => [],
573+
'doctrine.custom_numeric_functions' => [],
574+
'doctrine.custom_string_functions' => []
575+
]);
576+
577+
$container = new \Illuminate\Container\Container();
578+
$container->singleton(Repository::class, function () use ($config) {
579+
return $config;
580+
});
581+
582+
$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);
583+
584+
$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
585+
$factory->shouldReceive('store')->with('redis')->andReturn($cache);
586+
587+
$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
588+
return $factory;
589+
});
590+
591+
$factory = new EntityManagerFactory(
592+
$container,
593+
new Setup(),
594+
new MetaDataManager($container),
595+
new ConnectionManager($container),
596+
new CacheManager($container),
597+
$config,
598+
new EntityListenerResolver($container)
599+
);
600+
601+
$manager = $factory->create($config->get('doctrine'));
602+
603+
$this->assertInstanceOf(IlluminateCacheAdapter::class, $manager->getConfiguration()->getMetadataCacheImpl());
604+
}
605+
606+
public function test_illuminate_cache_provider_invalid_store()
607+
{
608+
m::resetContainer();
609+
610+
$config = new ConfigRepository([
611+
'database.connections.mysql' => [
612+
'driver' => 'mysql'
613+
],
614+
'doctrine' => [
615+
'meta' => 'annotations',
616+
'connection' => 'mysql',
617+
'paths' => ['Entities'],
618+
'proxies' => [
619+
'path' => 'dir',
620+
'auto_generate' => false,
621+
'namespace' => 'namespace'
622+
],
623+
624+
'cache' => [
625+
'metadata' => [
626+
'driver' => 'illuminate',
627+
]
628+
]
629+
],
630+
'doctrine.custom_datetime_functions' => [],
631+
'doctrine.custom_numeric_functions' => [],
632+
'doctrine.custom_string_functions' => []
633+
]);
634+
635+
$container = new \Illuminate\Container\Container();
636+
$container->singleton(Repository::class, function () use ($config) {
637+
return $config;
638+
});
639+
640+
$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);
641+
642+
$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
643+
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);
644+
645+
$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
646+
return $factory;
647+
});
648+
649+
$factory = new EntityManagerFactory(
650+
$container,
651+
new Setup(),
652+
new MetaDataManager($container),
653+
new ConnectionManager($container),
654+
new CacheManager($container),
655+
$config,
656+
new EntityListenerResolver($container)
657+
);
658+
659+
$this->expectException(InvalidArgumentException::class);
660+
661+
$this->expectExceptionMessage('Please specify the `store` when using the "illuminate" cache driver.');
662+
$factory->create($config->get('doctrine'));
663+
}
664+
665+
public function test_php_file_cache_custom_path()
666+
{
667+
m::resetContainer();
668+
669+
$config = new ConfigRepository([
670+
'database.connections.mysql' => [
671+
'driver' => 'mysql'
672+
],
673+
'doctrine' => [
674+
'meta' => 'annotations',
675+
'connection' => 'mysql',
676+
'paths' => ['Entities'],
677+
'proxies' => [
678+
'path' => 'dir',
679+
'auto_generate' => false,
680+
'namespace' => 'namespace'
681+
],
682+
683+
'cache' => [
684+
'metadata' => [
685+
'driver' => 'php_file',
686+
'path' => 'myCustomPath'
687+
]
688+
]
689+
],
690+
'doctrine.custom_datetime_functions' => [],
691+
'doctrine.custom_numeric_functions' => [],
692+
'doctrine.custom_string_functions' => []
693+
]);
694+
695+
$container = new \Illuminate\Container\Container();
696+
$container->singleton(Repository::class, function () use ($config) {
697+
return $config;
698+
});
699+
700+
$cache = M::mock(Illuminate\Contracts\Cache\Repository::class);
701+
702+
$factory = M::mock(\Illuminate\Contracts\Cache\Factory::class);
703+
$factory->shouldReceive('store')->with('myStoreName')->andReturn($cache);
704+
705+
$container->singleton(Illuminate\Contracts\Cache\Factory::class, function () use ($factory) {
706+
return $factory;
707+
});
708+
709+
$factory = new EntityManagerFactory(
710+
$container,
711+
new Setup(),
712+
new MetaDataManager($container),
713+
new ConnectionManager($container),
714+
new CacheManager($container),
715+
$config,
716+
new EntityListenerResolver($container)
717+
);
718+
719+
$manager = $factory->create($config->get('doctrine'));
720+
721+
$this->assertInstanceOf(\Doctrine\Common\Cache\PhpFileCache::class, $manager->getConfiguration()->getMetadataCacheImpl());
722+
$this->assertStringEndsWith('myCustomPath', $manager->getConfiguration()->getMetadataCacheImpl()->getDirectory());
723+
}
724+
488725
public function test_wrapper_connection()
489726
{
490727
m::resetContainer();
@@ -592,8 +829,8 @@ protected function mockConfig($driverConfig = ['driver' => 'mysql'], $strictCall
592829

593830
foreach ($this->caches as $cache) {
594831
$expectation = $this->config->shouldReceive('get')
595-
->with('doctrine.cache.' . $cache . '.driver', 'array')
596-
->andReturn('array');
832+
->with('doctrine.cache.' . $cache, [])
833+
->andReturn(['driver' => 'array']);
597834

598835
$strictCallCountChecking ? $expectation->once() : $expectation->never();
599836
}

0 commit comments

Comments
 (0)