|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata Project package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\EasyExtendsBundle\Generator; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata; |
|
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
16
|
|
|
|
|
17
|
|
|
class SerializerGenerator implements GeneratorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $entitySerializerTemplate; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var string |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $documentSerializerTemplate; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct() |
|
30
|
|
|
{ |
|
31
|
|
|
$this->entitySerializerTemplate = file_get_contents( |
|
32
|
|
|
__DIR__.'/../Resources/skeleton/serializer/entity.mustache' |
|
33
|
|
|
); |
|
34
|
|
|
$this->documentSerializerTemplate = file_get_contents( |
|
35
|
|
|
__DIR__.'/../Resources/skeleton/serializer/document.mustache' |
|
36
|
|
|
); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* {@inheritdoc} |
|
41
|
|
|
*/ |
|
42
|
|
|
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void |
|
43
|
|
|
{ |
|
44
|
|
|
$this->generateOrmSerializer($output, $bundleMetadata); |
|
45
|
|
|
$this->generateOdmSerializer($output, $bundleMetadata); |
|
46
|
|
|
$this->generatePhpcrSerializer($output, $bundleMetadata); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* @param OutputInterface $output |
|
51
|
|
|
* @param BundleMetadata $bundleMetadata |
|
52
|
|
|
*/ |
|
53
|
|
|
protected function generateOrmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void |
|
54
|
|
|
{ |
|
55
|
|
|
$names = $bundleMetadata->getOrmMetadata()->getEntityNames(); |
|
56
|
|
|
|
|
57
|
|
|
if (is_array($names) && count($names) > 0) { |
|
58
|
|
|
$output->writeln(' - Generating ORM serializer files'); |
|
59
|
|
|
|
|
60
|
|
|
foreach ($names as $name) { |
|
61
|
|
|
$destFile = sprintf( |
|
62
|
|
|
'%s/Entity.%s.xml', |
|
63
|
|
|
$bundleMetadata->getOrmMetadata()->getExtendedSerializerDirectory(), |
|
64
|
|
|
$name |
|
65
|
|
|
); |
|
66
|
|
|
|
|
67
|
|
|
$this->writeSerializerFile($output, $bundleMetadata, $this->entitySerializerTemplate, $destFile, $name); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param OutputInterface $output |
|
74
|
|
|
* @param BundleMetadata $bundleMetadata |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function generateOdmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void |
|
77
|
|
|
{ |
|
78
|
|
|
$names = $bundleMetadata->getOdmMetadata()->getDocumentNames(); |
|
79
|
|
|
|
|
80
|
|
|
if (is_array($names) && count($names) > 0) { |
|
81
|
|
|
$output->writeln(' - Generating ODM serializer files'); |
|
82
|
|
|
|
|
83
|
|
|
foreach ($names as $name) { |
|
84
|
|
|
$destFile = sprintf( |
|
85
|
|
|
'%s/Document.%s.xml', |
|
86
|
|
|
$bundleMetadata->getOdmMetadata()->getExtendedSerializerDirectory(), |
|
87
|
|
|
$name |
|
88
|
|
|
); |
|
89
|
|
|
|
|
90
|
|
|
$this->writeSerializerFile( |
|
91
|
|
|
$output, |
|
92
|
|
|
$bundleMetadata, |
|
93
|
|
|
$this->documentSerializerTemplate, |
|
94
|
|
|
$destFile, |
|
95
|
|
|
$name |
|
96
|
|
|
); |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param OutputInterface $output |
|
103
|
|
|
* @param BundleMetadata $bundleMetadata |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function generatePhpcrSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void |
|
106
|
|
|
{ |
|
107
|
|
|
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames(); |
|
108
|
|
|
|
|
109
|
|
|
if (is_array($names) && count($names) > 0) { |
|
110
|
|
|
$output->writeln(' - Generating PHPCR serializer files'); |
|
111
|
|
|
|
|
112
|
|
|
foreach ($names as $name) { |
|
113
|
|
|
$destFile = sprintf( |
|
114
|
|
|
'%s/Document.%s.xml', |
|
115
|
|
|
$bundleMetadata->getPhpcrMetadata()->getExtendedSerializerDirectory(), |
|
116
|
|
|
$name |
|
117
|
|
|
); |
|
118
|
|
|
|
|
119
|
|
|
$this->writeSerializerFile( |
|
120
|
|
|
$output, |
|
121
|
|
|
$bundleMetadata, |
|
122
|
|
|
$this->documentSerializerTemplate, |
|
123
|
|
|
$destFile, |
|
124
|
|
|
$name |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param OutputInterface $output |
|
132
|
|
|
* @param BundleMetadata $bundleMetadata |
|
133
|
|
|
* @param string $template |
|
134
|
|
|
* @param string $destFile |
|
135
|
|
|
* @param string $name |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function writeSerializerFile(OutputInterface $output, BundleMetadata $bundleMetadata, string $template, string $destFile, string $name): void |
|
138
|
|
|
{ |
|
139
|
|
|
if (is_file($destFile)) { |
|
140
|
|
|
$output->writeln(sprintf(' ~ <info>%s</info>', $name)); |
|
141
|
|
|
} else { |
|
142
|
|
|
$output->writeln(sprintf(' + <info>%s</info>', $name)); |
|
143
|
|
|
|
|
144
|
|
|
$string = Mustache::replace($template, array( |
|
145
|
|
|
'name' => $name, |
|
146
|
|
|
'namespace' => $bundleMetadata->getExtendedNamespace(), |
|
147
|
|
|
'root_name' => strtolower(preg_replace('/[A-Z]/', '_\\0', $name)), |
|
148
|
|
|
)); |
|
149
|
|
|
|
|
150
|
|
|
file_put_contents($destFile, $string); |
|
151
|
|
|
} |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|