|
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 OrmGenerator implements GeneratorInterface |
|
18
|
|
|
{ |
|
19
|
|
|
protected $entityTemplate; |
|
20
|
|
|
protected $entityRepositoryTemplate; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct() |
|
23
|
|
|
{ |
|
24
|
|
|
$this->entityTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/entity.mustache'); |
|
25
|
|
|
$this->entityRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/repository.mustache'); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param OutputInterface $output |
|
30
|
|
|
* @param BundleMetadata $bundleMetadata |
|
31
|
|
|
*/ |
|
32
|
|
|
public function generate(OutputInterface $output, BundleMetadata $bundleMetadata) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->generateMappingEntityFiles($output, $bundleMetadata); |
|
35
|
|
|
$this->generateEntityFiles($output, $bundleMetadata); |
|
36
|
|
|
$this->generateEntityRepositoryFiles($output, $bundleMetadata); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param OutputInterface $output |
|
41
|
|
|
* @param BundleMetadata $bundleMetadata |
|
42
|
|
|
*/ |
|
43
|
|
|
public function generateMappingEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata) |
|
44
|
|
|
{ |
|
45
|
|
|
$output->writeln(' - Copy entity files'); |
|
46
|
|
|
|
|
47
|
|
|
$files = $bundleMetadata->getOrmMetadata()->getEntityMappingFiles(); |
|
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->getOrmMetadata()->getExtendedMappingEntityDirectory(), $fileName); |
|
53
|
|
|
$src_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getMappingEntityDirectory(), $file->getFileName()); |
|
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
|
|
|
public function generateEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata) |
|
76
|
|
|
{ |
|
77
|
|
|
$output->writeln(' - Generating entity files'); |
|
78
|
|
|
|
|
79
|
|
|
$names = $bundleMetadata->getOrmMetadata()->getEntityNames(); |
|
80
|
|
|
|
|
81
|
|
|
foreach ($names as $name) { |
|
82
|
|
|
$extendedName = $name; |
|
83
|
|
|
|
|
84
|
|
|
$dest_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name); |
|
85
|
|
|
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName); |
|
86
|
|
|
|
|
87
|
|
|
if (!is_file($src_file)) { |
|
88
|
|
|
$extendedName = 'Base'.$name; |
|
89
|
|
|
$src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName); |
|
90
|
|
|
|
|
91
|
|
|
if (!is_file($src_file)) { |
|
92
|
|
|
$output->writeln(sprintf(' ! <info>%s</info>', $extendedName)); |
|
93
|
|
|
|
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if (is_file($dest_file)) { |
|
99
|
|
|
$output->writeln(sprintf(' ~ <info>%s</info>', $name)); |
|
100
|
|
|
} else { |
|
101
|
|
|
$output->writeln(sprintf(' + <info>%s</info>', $name)); |
|
102
|
|
|
|
|
103
|
|
|
$string = Mustache::replace($this->getEntityTemplate(), array( |
|
104
|
|
|
'extended_namespace' => $bundleMetadata->getExtendedNamespace(), |
|
105
|
|
|
'name' => $name != $extendedName ? $extendedName : $name, |
|
106
|
|
|
'class' => $name, |
|
107
|
|
|
'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName, |
|
108
|
|
|
'namespace' => $bundleMetadata->getNamespace(), |
|
109
|
|
|
)); |
|
110
|
|
|
|
|
111
|
|
|
file_put_contents($dest_file, $string); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param OutputInterface $output |
|
118
|
|
|
* @param BundleMetadata $bundleMetadata |
|
119
|
|
|
*/ |
|
120
|
|
|
public function generateEntityRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata) |
|
121
|
|
|
{ |
|
122
|
|
|
$output->writeln(' - Generating entity repository files'); |
|
123
|
|
|
|
|
124
|
|
|
$names = $bundleMetadata->getOrmMetadata()->getEntityNames(); |
|
125
|
|
|
|
|
126
|
|
|
foreach ($names as $name) { |
|
127
|
|
|
$dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name); |
|
128
|
|
|
$src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $name); |
|
129
|
|
|
|
|
130
|
|
|
if (!is_file($src_file)) { |
|
131
|
|
|
$output->writeln(sprintf(' ! <info>%sRepository</info>', $name)); |
|
132
|
|
|
continue; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
if (is_file($dest_file)) { |
|
136
|
|
|
$output->writeln(sprintf(' ~ <info>%sRepository</info>', $name)); |
|
137
|
|
|
} else { |
|
138
|
|
|
$output->writeln(sprintf(' + <info>%sRepository</info>', $name)); |
|
139
|
|
|
|
|
140
|
|
|
$string = Mustache::replace($this->getEntityRepositoryTemplate(), array( |
|
141
|
|
|
'extended_namespace' => $bundleMetadata->getExtendedNamespace(), |
|
142
|
|
|
'name' => $name, |
|
143
|
|
|
'namespace' => $bundleMetadata->getNamespace(), |
|
144
|
|
|
)); |
|
145
|
|
|
|
|
146
|
|
|
file_put_contents($dest_file, $string); |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @return string |
|
153
|
|
|
*/ |
|
154
|
|
|
public function getEntityTemplate() |
|
155
|
|
|
{ |
|
156
|
|
|
return $this->entityTemplate; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @return string |
|
161
|
|
|
*/ |
|
162
|
|
|
public function getEntityRepositoryTemplate() |
|
163
|
|
|
{ |
|
164
|
|
|
return $this->entityRepositoryTemplate; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|