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 PHPCRGenerator implements GeneratorInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
protected $DocumentTemplate; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $DocumentRepositoryTemplate; |
28
|
|
|
|
29
|
|
|
public function __construct() |
30
|
|
|
{ |
31
|
|
|
$this->DocumentTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/phpcr/document.mustache'); |
32
|
|
|
$this->DocumentRepositoryTemplate = file_get_contents( |
33
|
|
|
__DIR__.'/../Resources/skeleton/phpcr/repository.mustache' |
34
|
|
|
); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
|
|
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void |
41
|
|
|
{ |
42
|
|
|
$this->generateMappingDocumentFiles($output, $bundleMetadata); |
43
|
|
|
$this->generateDocumentFiles($output, $bundleMetadata); |
44
|
|
|
$this->generateDocumentRepositoryFiles($output, $bundleMetadata); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param OutputInterface $output |
49
|
|
|
* @param BundleMetadata $bundleMetadata |
50
|
|
|
*/ |
51
|
|
|
public function generateMappingDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void |
52
|
|
|
{ |
53
|
|
|
$output->writeln(' - Copy Document files'); |
54
|
|
|
|
55
|
|
|
$files = $bundleMetadata->getPhpcrMetadata()->getDocumentMappingFiles(); |
56
|
|
|
foreach ($files as $file) { |
57
|
|
|
// copy mapping definition |
58
|
|
|
$fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.')); |
59
|
|
|
|
60
|
|
|
$dest_file = sprintf( |
61
|
|
|
'%s/%s', |
62
|
|
|
$bundleMetadata->getPhpcrMetadata()->getExtendedMappingDocumentDirectory(), |
63
|
|
|
$fileName |
64
|
|
|
); |
65
|
|
|
$src_file = sprintf( |
66
|
|
|
'%s/%s', |
67
|
|
|
$bundleMetadata->getPhpcrMetadata()->getMappingDocumentDirectory(), |
68
|
|
|
$file->getFileName() |
69
|
|
|
); |
70
|
|
|
|
71
|
|
|
if (is_file($dest_file)) { |
72
|
|
|
$output->writeln(sprintf(' ~ <info>%s</info>', $fileName)); |
73
|
|
|
} else { |
74
|
|
|
$output->writeln(sprintf(' + <info>%s</info>', $fileName)); |
75
|
|
|
|
76
|
|
|
$mappingEntityTemplate = file_get_contents($src_file); |
77
|
|
|
|
78
|
|
|
$string = Mustache::replace($mappingEntityTemplate, array( |
79
|
|
|
'namespace' => $bundleMetadata->getExtendedNamespace(), |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
file_put_contents($dest_file, $string); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param OutputInterface $output |
89
|
|
|
* @param BundleMetadata $bundleMetadata |
90
|
|
|
*/ |
91
|
|
|
public function generateDocumentFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void |
92
|
|
|
{ |
93
|
|
|
$output->writeln(' - Generating Document files'); |
94
|
|
|
|
95
|
|
|
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames(); |
96
|
|
|
|
97
|
|
|
foreach ($names as $name) { |
98
|
|
|
$extendedName = $name; |
99
|
|
|
|
100
|
|
|
$dest_file = sprintf( |
101
|
|
|
'%s/%s.php', |
102
|
|
|
$bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(), |
103
|
|
|
$name |
104
|
|
|
); |
105
|
|
|
$src_file = sprintf( |
106
|
|
|
'%s/%s.php', |
107
|
|
|
$bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), |
108
|
|
|
$extendedName |
109
|
|
|
); |
110
|
|
|
|
111
|
|
|
if (!is_file($src_file)) { |
112
|
|
|
$extendedName = 'Base'.$name; |
113
|
|
|
$src_file = sprintf( |
114
|
|
|
'%s/%s.php', |
115
|
|
|
$bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), |
116
|
|
|
$extendedName |
117
|
|
|
); |
118
|
|
|
|
119
|
|
|
if (!is_file($src_file)) { |
120
|
|
|
$output->writeln(sprintf(' ! <info>%s</info>', $extendedName)); |
121
|
|
|
|
122
|
|
|
continue; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
if (is_file($dest_file)) { |
127
|
|
|
$output->writeln(sprintf(' ~ <info>%s</info>', $name)); |
128
|
|
|
} else { |
129
|
|
|
$output->writeln(sprintf(' + <info>%s</info>', $name)); |
130
|
|
|
|
131
|
|
|
$string = Mustache::replace($this->getDocumentTemplate(), array( |
132
|
|
|
'extended_namespace' => $bundleMetadata->getExtendedNamespace(), |
133
|
|
|
'name' => $name != $extendedName ? $extendedName : $name, |
134
|
|
|
'class' => $name, |
135
|
|
|
'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName, |
136
|
|
|
'namespace' => $bundleMetadata->getNamespace(), |
137
|
|
|
)); |
138
|
|
|
|
139
|
|
|
file_put_contents($dest_file, $string); |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* @param OutputInterface $output |
146
|
|
|
* @param BundleMetadata $bundleMetadata |
147
|
|
|
*/ |
148
|
|
|
public function generateDocumentRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void |
149
|
|
|
{ |
150
|
|
|
$output->writeln(' - Generating Document repository files'); |
151
|
|
|
|
152
|
|
|
$names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames(); |
153
|
|
|
|
154
|
|
|
foreach ($names as $name) { |
155
|
|
|
$dest_file = sprintf( |
156
|
|
|
'%s/%sRepository.php', |
157
|
|
|
$bundleMetadata->getPhpcrMetadata()->getExtendedDocumentDirectory(), |
158
|
|
|
$name |
159
|
|
|
); |
160
|
|
|
$src_file = sprintf( |
161
|
|
|
'%s/Base%sRepository.php', |
162
|
|
|
$bundleMetadata->getPhpcrMetadata()->getDocumentDirectory(), |
163
|
|
|
$name |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
if (!is_file($src_file)) { |
167
|
|
|
$output->writeln(sprintf(' ! <info>%sRepository</info>', $name)); |
168
|
|
|
|
169
|
|
|
continue; |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
if (is_file($dest_file)) { |
173
|
|
|
$output->writeln(sprintf(' ~ <info>%sRepository</info>', $name)); |
174
|
|
|
} else { |
175
|
|
|
$output->writeln(sprintf(' + <info>%sRepository</info>', $name)); |
176
|
|
|
|
177
|
|
|
$string = Mustache::replace($this->getDocumentRepositoryTemplate(), array( |
178
|
|
|
'extended_namespace' => $bundleMetadata->getExtendedNamespace(), |
179
|
|
|
'name' => $name, |
180
|
|
|
'namespace' => $bundleMetadata->getNamespace(), |
181
|
|
|
)); |
182
|
|
|
|
183
|
|
|
file_put_contents($dest_file, $string); |
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @return string |
190
|
|
|
*/ |
191
|
|
|
public function getDocumentTemplate(): string |
192
|
|
|
{ |
193
|
|
|
return $this->DocumentTemplate; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* @return string |
198
|
|
|
*/ |
199
|
|
|
public function getDocumentRepositoryTemplate(): string |
200
|
|
|
{ |
201
|
|
|
return $this->DocumentRepositoryTemplate; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|