3
3
namespace Mouf \Database \SchemaAnalyzer ;
4
4
5
5
use Doctrine \Common \Cache \Cache ;
6
+ use Doctrine \Common \Cache \VoidCache ;
6
7
use Doctrine \DBAL \Schema \AbstractSchemaManager ;
7
8
use Doctrine \DBAL \Schema \ForeignKeyConstraint ;
8
9
use Doctrine \DBAL \Schema \Schema ;
@@ -51,11 +52,15 @@ class SchemaAnalyzer
51
52
public function __construct (AbstractSchemaManager $ schemaManager , Cache $ cache = null , $ schemaCacheKey = null )
52
53
{
53
54
$ this ->schemaManager = $ schemaManager ;
54
- $ this ->cache = $ cache ;
55
- $ this ->cachePrefix = $ schemaCacheKey ;
56
55
if (empty ($ schemaCacheKey ) && $ cache ) {
57
56
throw new SchemaAnalyzerException ('You must provide a schema cache key if you configure SchemaAnalyzer with cache support. ' );
58
57
}
58
+ if ($ cache ) {
59
+ $ this ->cache = $ cache ;
60
+ } else {
61
+ $ this ->cache = new VoidCache ();
62
+ }
63
+ $ this ->cachePrefix = $ schemaCacheKey ;
59
64
}
60
65
61
66
/**
@@ -70,15 +75,10 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
70
75
public function detectJunctionTables ()
71
76
{
72
77
$ junctionTablesKey = $ this ->cachePrefix ."_junctiontables " ;
73
- $ junctionTables = false ;
74
- if ($ this ->cache ) {
75
- $ junctionTables = $ this ->cache ->fetch ($ junctionTablesKey );
76
- }
78
+ $ junctionTables = $ this ->cache ->fetch ($ junctionTablesKey );
77
79
if ($ junctionTables === false ) {
78
80
$ junctionTables = array_filter ($ this ->getSchema ()->getTables (), [$ this , 'isJunctionTable ' ]);
79
- if ($ this ->cache ) {
80
- $ this ->cache ->save ($ junctionTablesKey , $ junctionTables );
81
- }
81
+ $ this ->cache ->save ($ junctionTablesKey , $ junctionTables );
82
82
}
83
83
return $ junctionTables ;
84
84
}
@@ -151,14 +151,10 @@ public function getShortestPath($fromTable, $toTable)
151
151
{
152
152
$ cacheKey = $ this ->cachePrefix ."_shortest_ " .$ fromTable ."``` " .$ toTable ;
153
153
$ path = false ;
154
- if ($ this ->cache ) {
155
- $ path = $ this ->cache ->fetch ($ cacheKey );
156
- }
154
+ $ path = $ this ->cache ->fetch ($ cacheKey );
157
155
if ($ path === false ) {
158
156
$ path = $ this ->getShortestPathWithoutCache ($ fromTable , $ toTable );
159
- if ($ this ->cache ) {
160
- $ this ->cache ->save ($ cacheKey , $ path );
161
- }
157
+ $ this ->cache ->save ($ cacheKey , $ path );
162
158
}
163
159
return $ path ;
164
160
}
@@ -256,14 +252,10 @@ private function buildSchemaGraph()
256
252
private function getSchema () {
257
253
if ($ this ->schema === null ) {
258
254
$ schemaKey = $ this ->cachePrefix ."_schema " ;
259
- if ($ this ->cache ) {
260
- $ this ->schema = $ this ->cache ->fetch ($ schemaKey );
261
- }
255
+ $ this ->schema = $ this ->cache ->fetch ($ schemaKey );
262
256
if (empty ($ this ->schema )) {
263
257
$ this ->schema = $ this ->schemaManager ->createSchema ();
264
- if ($ this ->cache ) {
265
- $ this ->cache ->save ($ schemaKey , $ this ->schema );
266
- }
258
+ $ this ->cache ->save ($ schemaKey , $ this ->schema );
267
259
}
268
260
}
269
261
return $ this ->schema ;
0 commit comments