11
11
use Fhaculty \Graph \Edge \Base ;
12
12
use Fhaculty \Graph \Graph ;
13
13
use Fhaculty \Graph \Vertex ;
14
- use Graphp \Algorithms \ShortestPath \Dijkstra ;
15
14
16
15
/**
17
16
* This class can analyze a database model.
@@ -47,8 +46,8 @@ class SchemaAnalyzer
47
46
48
47
/**
49
48
* @param AbstractSchemaManager $schemaManager
50
- * @param Cache|null $cache The Doctrine cache service to use to cache results (optional)
51
- * @param string|null $schemaCacheKey The unique identifier for the schema manager. Compulsory if cache is set.
49
+ * @param Cache|null $cache The Doctrine cache service to use to cache results (optional)
50
+ * @param string|null $schemaCacheKey The unique identifier for the schema manager. Compulsory if cache is set.
52
51
*/
53
52
public function __construct (AbstractSchemaManager $ schemaManager , Cache $ cache = null , $ schemaCacheKey = null )
54
53
{
@@ -66,7 +65,7 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
66
65
67
66
/**
68
67
* Detect all junctions tables in the schema.
69
- * A table is a junction table if:
68
+ * A table is a junction table if:.
70
69
*
71
70
* - it has exactly 2 foreign keys
72
71
* - it has only 2 columns (or 3 columns if the third one is an autoincremented primary key).
@@ -76,18 +75,19 @@ public function __construct(AbstractSchemaManager $schemaManager, Cache $cache =
76
75
*/
77
76
public function detectJunctionTables ()
78
77
{
79
- $ junctionTablesKey = $ this ->cachePrefix ." _junctiontables " ;
78
+ $ junctionTablesKey = $ this ->cachePrefix .' _junctiontables ' ;
80
79
$ junctionTables = $ this ->cache ->fetch ($ junctionTablesKey );
81
80
if ($ junctionTables === false ) {
82
81
$ junctionTables = array_filter ($ this ->getSchema ()->getTables (), [$ this , 'isJunctionTable ' ]);
83
82
$ this ->cache ->save ($ junctionTablesKey , $ junctionTables );
84
83
}
84
+
85
85
return $ junctionTables ;
86
86
}
87
87
88
88
/**
89
89
* Returns true if $table is a junction table.
90
- * I.e:
90
+ * I.e:.
91
91
*
92
92
* - it must have exactly 2 foreign keys
93
93
* - it must have only 2 columns (or 3 columns if the third one is an autoincremented primary key).
@@ -147,17 +147,20 @@ private function isJunctionTable(Table $table)
147
147
*
148
148
* @param string $fromTable
149
149
* @param string $toTable
150
+ *
150
151
* @return \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
152
+ *
151
153
* @throws SchemaAnalyzerException
152
154
*/
153
155
public function getShortestPath ($ fromTable , $ toTable )
154
156
{
155
- $ cacheKey = $ this ->cachePrefix ." _shortest_ " .$ fromTable ." ``` " .$ toTable ;
157
+ $ cacheKey = $ this ->cachePrefix .' _shortest_ ' .$ fromTable .' ``` ' .$ toTable ;
156
158
$ path = $ this ->cache ->fetch ($ cacheKey );
157
159
if ($ path === false ) {
158
160
$ path = $ this ->getShortestPathWithoutCache ($ fromTable , $ toTable );
159
161
$ this ->cache ->save ($ cacheKey , $ path );
160
162
}
163
+
161
164
return $ path ;
162
165
}
163
166
@@ -166,7 +169,9 @@ public function getShortestPath($fromTable, $toTable)
166
169
*
167
170
* @param string $fromTable
168
171
* @param string $toTable
172
+ *
169
173
* @return \Doctrine\DBAL\Schema\ForeignKeyConstraint[]
174
+ *
170
175
* @throws SchemaAnalyzerException
171
176
*/
172
177
private function getShortestPathWithoutCache ($ fromTable , $ toTable )
@@ -255,33 +260,37 @@ private function buildSchemaGraph()
255
260
}
256
261
257
262
/**
258
- * Returns the schema (from the schema manager or the cache if needed)
263
+ * Returns the schema (from the schema manager or the cache if needed).
264
+ *
259
265
* @return Schema
260
266
*/
261
- private function getSchema () {
267
+ private function getSchema ()
268
+ {
262
269
if ($ this ->schema === null ) {
263
- $ schemaKey = $ this ->cachePrefix ." _schema " ;
270
+ $ schemaKey = $ this ->cachePrefix .' _schema ' ;
264
271
$ this ->schema = $ this ->cache ->fetch ($ schemaKey );
265
272
if (empty ($ this ->schema )) {
266
273
$ this ->schema = $ this ->schemaManager ->createSchema ();
267
274
$ this ->cache ->save ($ schemaKey , $ this ->schema );
268
275
}
269
276
}
277
+
270
278
return $ this ->schema ;
271
279
}
272
280
273
281
/**
274
282
* Returns the full exception message when an ambiguity arises.
275
283
*
276
284
* @param Base[][] $paths
277
- * @param Vertex $startVertex
285
+ * @param Vertex $startVertex
278
286
*/
279
- private function getAmbiguityExceptionMessage (array $ paths , Vertex $ startVertex , Vertex $ endVertex ) {
287
+ private function getAmbiguityExceptionMessage (array $ paths , Vertex $ startVertex , Vertex $ endVertex )
288
+ {
280
289
$ textPaths = [];
281
290
$ i = 1 ;
282
291
foreach ($ paths as $ path ) {
283
- $ textPaths [] = " Path " .$ i ." : " .$ this ->getTextualPath ($ path , $ startVertex );
284
- $ i ++ ;
292
+ $ textPaths [] = ' Path ' .$ i .' : ' .$ this ->getTextualPath ($ path , $ startVertex );
293
+ ++ $ i ;
285
294
}
286
295
287
296
$ msg = sprintf ("There are many possible shortest paths between table '%s' and table '%s' \n\n" ,
@@ -298,7 +307,8 @@ private function getAmbiguityExceptionMessage(array $paths, Vertex $startVertex,
298
307
* @param Base[] $path
299
308
* @param Vertex $startVertex
300
309
*/
301
- private function getTextualPath (array $ path , Vertex $ startVertex ) {
310
+ private function getTextualPath (array $ path , Vertex $ startVertex )
311
+ {
302
312
$ currentVertex = $ startVertex ;
303
313
$ currentTable = $ currentVertex ->getId ();
304
314
@@ -317,9 +327,9 @@ private function getTextualPath(array $path, Vertex $startVertex) {
317
327
318
328
$ columns = implode (', ' , $ fk ->getLocalColumns ());
319
329
320
- $ textPath .= " " .(!$ isForward? " < " : "" );
321
- $ textPath .= " --( " .$ columns ." )-- " ;
322
- $ textPath .= ($ isForward? " > " : "" ). " " ;
330
+ $ textPath .= ' ' .(!$ isForward ? ' < ' : '' );
331
+ $ textPath .= ' --( ' .$ columns .' )-- ' ;
332
+ $ textPath .= ($ isForward ? ' > ' : '' ). ' ' ;
323
333
$ textPath .= $ currentTable ;
324
334
} elseif ($ junctionTable = $ edge ->getAttribute ('junction ' )) {
325
335
/* @var $junctionTable Table */
@@ -331,7 +341,7 @@ private function getTextualPath(array $path, Vertex $startVertex) {
331
341
} else {
332
342
$ currentTable = $ fk ->getForeignTableName ();
333
343
}
334
- $ textPath .= " <=( " .$ junctionTable ->getName ()." )=> " .$ currentTable ;
344
+ $ textPath .= ' <=( ' .$ junctionTable ->getName ().' )=> ' .$ currentTable ;
335
345
} else {
336
346
// @codeCoverageIgnoreStart
337
347
throw new SchemaAnalyzerException ('Unexpected edge. We should have a fk or a junction attribute. ' );
0 commit comments