Completed
Push — 2.x ( e45de2...5989f2 )
by Grégoire
02:40
created

OrmGenerator::generateMappingEntityFiles()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 7
Bugs 1 Features 3
Metric Value
c 7
b 1
f 3
dl 0
loc 27
rs 8.8571
cc 3
eloc 15
nc 3
nop 2
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 OrmGenerator implements GeneratorInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    protected $entityTemplate;
23
24
    /**
25
     * @var string
26
     */
27
    protected $entityRepositoryTemplate;
28
29
    public function __construct()
30
    {
31
        $this->entityTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/entity.mustache');
32
        $this->entityRepositoryTemplate = file_get_contents(__DIR__.'/../Resources/skeleton/orm/repository.mustache');
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function generate(OutputInterface $output, BundleMetadata $bundleMetadata)
39
    {
40
        $this->generateMappingEntityFiles($output, $bundleMetadata);
41
        $this->generateEntityFiles($output, $bundleMetadata);
42
        $this->generateEntityRepositoryFiles($output, $bundleMetadata);
43
    }
44
45
    /**
46
     * @param OutputInterface $output
47
     * @param BundleMetadata  $bundleMetadata
48
     */
49
    public function generateMappingEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
50
    {
51
        $output->writeln(' - Copy entity files');
52
53
        $files = $bundleMetadata->getOrmMetadata()->getEntityMappingFiles();
54
        foreach ($files as $file) {
55
            // copy mapping definition
56
            $fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
57
58
            $dest_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getExtendedMappingEntityDirectory(), $fileName);
59
            $src_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getMappingEntityDirectory(), $file->getFileName());
60
61
            if (is_file($dest_file)) {
62
                $output->writeln(sprintf('   ~ <info>%s</info>', $fileName));
63
            } else {
64
                $output->writeln(sprintf('   + <info>%s</info>', $fileName));
65
66
                $mappingEntityTemplate = file_get_contents($src_file);
67
68
                $string = Mustache::replace($mappingEntityTemplate, array(
69
                    'namespace' => $bundleMetadata->getExtendedNamespace(),
70
                ));
71
72
                file_put_contents($dest_file, $string);
73
            }
74
        }
75
    }
76
77
    /**
78
     * @param OutputInterface $output
79
     * @param BundleMetadata  $bundleMetadata
80
     */
81
    public function generateEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
82
    {
83
        $output->writeln(' - Generating entity files');
84
85
        $names = $bundleMetadata->getOrmMetadata()->getEntityNames();
86
87
        foreach ($names as $name) {
88
            $extendedName = $name;
89
90
            $dest_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name);
91
            $src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName);
92
93
            if (!is_file($src_file)) {
94
                $extendedName = 'Base'.$name;
95
                $src_file = sprintf('%s/%s.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $extendedName);
96
97
                if (!is_file($src_file)) {
98
                    $output->writeln(sprintf('   ! <info>%s</info>', $extendedName));
99
100
                    continue;
101
                }
102
            }
103
104
            if (is_file($dest_file)) {
105
                $output->writeln(sprintf('   ~ <info>%s</info>', $name));
106
            } else {
107
                $output->writeln(sprintf('   + <info>%s</info>', $name));
108
109
                $string = Mustache::replace($this->getEntityTemplate(), array(
110
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
111
                    'name' => $name != $extendedName ? $extendedName : $name,
112
                    'class' => $name,
113
                    'extended_name' => $name == $extendedName ? 'Base'.$name : $extendedName,
114
                    'namespace' => $bundleMetadata->getNamespace(),
115
                ));
116
117
                file_put_contents($dest_file, $string);
118
            }
119
        }
120
    }
121
122
    /**
123
     * @param OutputInterface $output
124
     * @param BundleMetadata  $bundleMetadata
125
     */
126
    public function generateEntityRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata)
127
    {
128
        $output->writeln(' - Generating entity repository files');
129
130
        $names = $bundleMetadata->getOrmMetadata()->getEntityNames();
131
132
        foreach ($names as $name) {
133
            $dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name);
134
            $src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $name);
135
136
            if (!is_file($src_file)) {
137
                $output->writeln(sprintf('   ! <info>%sRepository</info>', $name));
138
                continue;
139
            }
140
141
            if (is_file($dest_file)) {
142
                $output->writeln(sprintf('   ~ <info>%sRepository</info>', $name));
143
            } else {
144
                $output->writeln(sprintf('   + <info>%sRepository</info>', $name));
145
146
                $string = Mustache::replace($this->getEntityRepositoryTemplate(), array(
147
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
148
                    'name' => $name,
149
                    'namespace' => $bundleMetadata->getNamespace(),
150
                ));
151
152
                file_put_contents($dest_file, $string);
153
            }
154
        }
155
    }
156
157
    /**
158
     * @return string
159
     */
160
    public function getEntityTemplate()
161
    {
162
        return $this->entityTemplate;
163
    }
164
165
    /**
166
     * @return string
167
     */
168
    public function getEntityRepositoryTemplate()
169
    {
170
        return $this->entityRepositoryTemplate;
171
    }
172
}
173