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