|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace DH\Auditor\Provider\Doctrine\Persistence\Helper; |
|
6
|
|
|
|
|
7
|
|
|
use Composer\Autoload\ClassLoader; |
|
8
|
|
|
use Doctrine\DBAL\Connection; |
|
9
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
10
|
|
|
use Doctrine\DBAL\Result; |
|
11
|
|
|
use Doctrine\DBAL\Schema\AbstractSchemaManager; |
|
12
|
|
|
use Doctrine\DBAL\Schema\Comparator; |
|
|
|
|
|
|
13
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
14
|
|
|
use Doctrine\DBAL\Statement; |
|
15
|
|
|
use Doctrine\DBAL\Types\Types; |
|
16
|
|
|
use Doctrine\ORM\Configuration; |
|
17
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
|
18
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
|
19
|
|
|
use Doctrine\ORM\ORMSetup; |
|
20
|
|
|
use Doctrine\ORM\Tools\Setup; |
|
21
|
|
|
use InvalidArgumentException; |
|
22
|
|
|
use ReflectionClass; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @see \DH\Auditor\Tests\Provider\Doctrine\Persistence\Helper\DoctrineHelperTest |
|
26
|
|
|
* |
|
27
|
|
|
* @internal |
|
28
|
|
|
*/ |
|
29
|
|
|
final class DoctrineHelper |
|
30
|
|
|
{ |
|
31
|
|
|
/** |
|
32
|
|
|
* Gets the real class name of a class name that could be a proxy. |
|
33
|
|
|
* |
|
34
|
|
|
* @param object|string $subject |
|
35
|
|
|
* |
|
36
|
|
|
* credits |
|
37
|
|
|
* https://github.com/api-platform/core/blob/master/src/Util/ClassInfoTrait.php |
|
38
|
|
|
*/ |
|
39
|
|
|
public static function getRealClassName(object|string $subject): string |
|
40
|
|
|
{ |
|
41
|
|
|
$subject = \is_object($subject) ? $subject::class : $subject; |
|
42
|
|
|
|
|
43
|
|
|
// __CG__: Doctrine Common Marker for Proxy (ODM < 2.0 and ORM < 3.0) |
|
44
|
|
|
// __PM__: Ocramius Proxy Manager (ODM >= 2.0) |
|
45
|
|
|
$positionCg = mb_strrpos($subject, '\\__CG__\\'); |
|
46
|
|
|
$positionPm = mb_strrpos($subject, '\\__PM__\\'); |
|
47
|
|
|
if (false === $positionCg && false === $positionPm) { |
|
48
|
|
|
return $subject; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
if (false !== $positionCg) { |
|
52
|
|
|
return mb_substr($subject, $positionCg + 8); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
$className = ltrim($subject, '\\'); |
|
56
|
|
|
|
|
57
|
|
|
return mb_substr( |
|
58
|
|
|
$className, |
|
59
|
|
|
8 + $positionPm, |
|
60
|
|
|
mb_strrpos($className, '\\') - ($positionPm + 8) |
|
61
|
|
|
); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public static function getDoctrineType(string $type): string |
|
65
|
|
|
{ |
|
66
|
|
|
if (!\defined(Types::class.'::'.$type)) { |
|
67
|
|
|
throw new InvalidArgumentException(sprintf('Undefined Doctrine type "%s"', $type)); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
\assert(\is_string(\constant(Types::class.'::'.$type))); |
|
71
|
|
|
|
|
72
|
|
|
return \constant(Types::class.'::'.$type); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @throws \Doctrine\DBAL\Exception |
|
77
|
|
|
*/ |
|
78
|
|
|
public static function executeStatement(QueryBuilder|Statement $statement): void |
|
79
|
|
|
{ |
|
80
|
|
|
if (method_exists($statement, 'executeStatement')) { |
|
81
|
|
|
$statement->executeStatement(); |
|
82
|
|
|
} else { |
|
83
|
|
|
$statement->execute(); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @throws \Doctrine\DBAL\Exception |
|
89
|
|
|
*/ |
|
90
|
|
|
public static function executeQuery(QueryBuilder|Statement $statement): int|Result|string |
|
91
|
|
|
{ |
|
92
|
|
|
if (method_exists($statement, 'executeQuery')) { |
|
93
|
|
|
return $statement->executeQuery(); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return $statement->execute(); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public static function createSchemaManager(Connection $connection): AbstractSchemaManager |
|
100
|
|
|
{ |
|
101
|
|
|
return method_exists($connection, 'createSchemaManager') |
|
102
|
|
|
? $connection->createSchemaManager() |
|
103
|
|
|
: $connection->getSchemaManager(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
public static function introspectSchema(AbstractSchemaManager $schemaManager): Schema |
|
107
|
|
|
{ |
|
108
|
|
|
return method_exists($schemaManager, 'introspectSchema') |
|
109
|
|
|
? $schemaManager->introspectSchema() |
|
110
|
|
|
: $schemaManager->createSchema(); |
|
|
|
|
|
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return array<string> |
|
115
|
|
|
* |
|
116
|
|
|
* @throws \Doctrine\DBAL\Exception |
|
117
|
|
|
*/ |
|
118
|
|
|
public static function getMigrateToSql(Connection $connection, Schema $fromSchema, Schema $toSchema): array |
|
119
|
|
|
{ |
|
120
|
|
|
$schemaComparator = new Comparator(); |
|
121
|
|
|
$platform = $connection->getDatabasePlatform(); |
|
122
|
|
|
|
|
123
|
|
|
if (method_exists($platform, 'getAlterSchemaSQL')) { |
|
124
|
|
|
return $platform->getAlterSchemaSQL( |
|
125
|
|
|
$schemaComparator->compareSchemas($fromSchema, $toSchema) |
|
126
|
|
|
); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $fromSchema->getMigrateToSql($toSchema, $platform); |
|
|
|
|
|
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public static function createAttributeMetadataConfiguration(array $paths, bool $isDevMode = false): Configuration |
|
133
|
|
|
{ |
|
134
|
|
|
if (class_exists(ORMSetup::class)) { |
|
135
|
|
|
return ORMSetup::createAttributeMetadataConfiguration($paths, $isDevMode); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
return Setup::createAttributeMetadataConfiguration($paths, $isDevMode); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
public static function getEntityManagerFromOnFlushEventArgs(OnFlushEventArgs $args): EntityManagerInterface |
|
142
|
|
|
{ |
|
143
|
|
|
return method_exists($args, 'getObjectManager') ? $args->getObjectManager() : $args->getEntityManager(); |
|
|
|
|
|
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
public static function getVendorDir(): string |
|
147
|
|
|
{ |
|
148
|
|
|
$reflection = new ReflectionClass(ClassLoader::class); |
|
149
|
|
|
$filename = $reflection->getFileName(); |
|
150
|
|
|
\assert(\is_string($filename)); |
|
151
|
|
|
|
|
152
|
|
|
return \dirname($filename, 2); |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths