1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TheCodingMachine\TDBM\Utils\Annotation; |
5
|
|
|
|
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
7
|
|
|
use Doctrine\Common\Annotations\DocParser; |
8
|
|
|
use Doctrine\DBAL\Schema\Column; |
9
|
|
|
use Doctrine\DBAL\Schema\Table; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Parses annotations in database columns. |
13
|
|
|
*/ |
14
|
|
|
class AnnotationParser |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var DocParser |
18
|
|
|
*/ |
19
|
|
|
private $docParser; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* AnnotationParser constructor. |
23
|
|
|
* @param string[] $annotations An array mapping the annotation name to the fully qualified class name |
24
|
|
|
*/ |
25
|
|
|
public function __construct(array $annotations) |
26
|
|
|
{ |
27
|
|
|
$this->docParser = new DocParser(); |
28
|
|
|
$this->docParser->setImports(array_change_key_case($annotations, \CASE_LOWER)); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array<string,string> $additionalAnnotations An array associating the name of the annotation in DB comments to the name of a fully qualified Doctrine annotation class |
33
|
|
|
*/ |
34
|
|
|
public static function buildWithDefaultAnnotations(array $additionalAnnotations): self |
35
|
|
|
{ |
36
|
|
|
$defaultAnnotations = [ |
37
|
|
|
'UUID' => UUID::class, |
38
|
|
|
'Autoincrement' => Autoincrement::class, |
39
|
|
|
'Bean' => Bean::class |
40
|
|
|
]; |
41
|
|
|
$annotations = $defaultAnnotations + $additionalAnnotations; |
42
|
|
|
return new self($annotations); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Parses the doc comment and initializes all the annotations. |
47
|
|
|
*/ |
48
|
|
|
private function parse(string $comment, string $context): Annotations |
49
|
|
|
{ |
50
|
|
|
AnnotationRegistry::registerUniqueLoader('class_exists'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
// compatibility with UUID annotation from TDBM 5.0 |
53
|
|
|
$comment = \str_replace(['@UUID v1', '@UUID v4'], ['@UUID("v1")', '@UUID("v4")'], $comment); |
54
|
|
|
|
55
|
|
|
$annotations = $this->docParser->parse($comment, $context); |
56
|
|
|
|
57
|
|
|
return new Annotations($annotations); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getTableAnnotations(Table $table): Annotations |
61
|
|
|
{ |
62
|
|
|
$options = $table->getOptions(); |
63
|
|
|
if (isset($options['comment'])) { |
64
|
|
|
return $this->parse($options['comment'], ' comment in table '.$table->getName()); |
65
|
|
|
} |
66
|
|
|
return new Annotations([]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
public function getColumnAnnotations(Column $column, Table $table): Annotations |
70
|
|
|
{ |
71
|
|
|
$comment = $column->getComment(); |
72
|
|
|
if ($comment === null) { |
73
|
|
|
return new Annotations([]); |
74
|
|
|
} |
75
|
|
|
return $this->parse($comment, sprintf('comment of column %s in table %s', $column->getName(), $table->getName())); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
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.