1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper; |
6
|
|
|
|
7
|
|
|
use Doctrine\DBAL\Connection; |
8
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
9
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
10
|
|
|
use Doctrine\DBAL\Schema\Schema; |
11
|
|
|
use Doctrine\DBAL\Types\Types; |
12
|
|
|
use InvalidArgumentException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @see \DH\Auditor\Tests\Provider\Doctrine\Persistence\Helper\DoctrineHelperTest |
16
|
|
|
* |
17
|
|
|
* @internal |
18
|
|
|
*/ |
19
|
|
|
final class DoctrineHelper |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Gets the real class name of a class name that could be a proxy. |
23
|
|
|
* |
24
|
|
|
* @param object|string $subject |
25
|
|
|
* |
26
|
|
|
* credits |
27
|
|
|
* https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php |
28
|
|
|
*/ |
29
|
|
|
public static function getRealClassName(object|string $subject): string |
30
|
|
|
{ |
31
|
|
|
$subject = \is_object($subject) ? $subject::class : $subject; |
32
|
|
|
|
33
|
|
|
// __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0) |
34
|
|
|
// __PM__: Ocramius Proxy Manager (ODM >= 2.0) |
35
|
|
|
$positionCg = mb_strrpos($subject, '\\__CG__\\'); |
36
|
|
|
$positionPm = mb_strrpos($subject, '\\__PM__\\'); |
37
|
|
|
if (false === $positionCg && false === $positionPm) { |
38
|
|
|
return $subject; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
if (false !== $positionCg) { |
42
|
|
|
return mb_substr($subject, $positionCg + 8); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
$className = ltrim($subject, '\\'); |
46
|
|
|
|
47
|
|
|
return mb_substr( |
48
|
|
|
$className, |
49
|
|
|
8 + $positionPm, |
50
|
|
|
mb_strrpos($className, '\\') - ($positionPm + 8) |
51
|
|
|
); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public static function getDoctrineType(string $type): string |
55
|
|
|
{ |
56
|
|
|
if (!\defined(Types::class.'::'.$type)) { |
57
|
|
|
throw new InvalidArgumentException(sprintf('Undefined Doctrine type "%s"', $type)); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
\assert(\is_string(\constant(Types::class.'::'.$type))); |
61
|
|
|
|
62
|
|
|
return \constant(Types::class.'::'.$type); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* TODO: remove this method when we drop support of doctrine/dbal 2.13.x. |
67
|
|
|
* |
68
|
|
|
* @throws \Doctrine\DBAL\Exception |
69
|
|
|
*/ |
70
|
|
|
public static function createSchemaManager(Connection $connection): AbstractSchemaManager |
71
|
|
|
{ |
72
|
|
|
return method_exists($connection, 'createSchemaManager') |
73
|
|
|
? $connection->createSchemaManager() |
74
|
|
|
: $connection->getSchemaManager(); // @phpstan-ignore-line |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* TODO: remove this method when we drop support of doctrine/dbal 2.13.x. |
79
|
|
|
* |
80
|
|
|
* @throws \Doctrine\DBAL\Exception |
81
|
|
|
*/ |
82
|
|
|
public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema |
83
|
|
|
{ |
84
|
|
|
return method_exists($schemaManager, 'introspectSchema') |
85
|
|
|
? $schemaManager->introspectSchema() |
86
|
|
|
: $schemaManager->createSchema(); // @phpstan-ignore-line |
|
|
|
|
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* TODO: remove this method when we drop support of doctrine/dbal 2.13.x. |
91
|
|
|
* |
92
|
|
|
* @return array<string> |
93
|
|
|
* |
94
|
|
|
* @throws \Doctrine\DBAL\Exception |
95
|
|
|
*/ |
96
|
|
|
public static function getMigrateToSql(Connection $connection, Schema $fromSchema, Schema $toSchema): array |
97
|
|
|
{ |
98
|
|
|
$schemaComparator = new Comparator(); |
99
|
|
|
$platform = $connection->getDatabasePlatform(); |
100
|
|
|
|
101
|
|
|
if (method_exists($platform, 'getAlterSchemaSQL')) { |
102
|
|
|
return $platform->getAlterSchemaSQL( |
103
|
|
|
$schemaComparator->compareSchemas($fromSchema, $toSchema) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
return $fromSchema->getMigrateToSql($toSchema, $platform); // @phpstan-ignore-line |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.