OrmGenerator::generateEntityFiles()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 40

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 40
rs 8.6577
c 0
b 0
f 0
cc 6
nc 6
nop 2
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Sonata Project package.
7
 *
8
 * (c) Thomas Rabaix <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Sonata\EasyExtendsBundle\Generator;
15
16
use Sonata\EasyExtendsBundle\Bundle\BundleMetadata;
17
use Symfony\Component\Console\Output\OutputInterface;
18
19
class OrmGenerator implements GeneratorInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $entityTemplate;
25
26
    /**
27
     * @var string
28
     */
29
    protected $entityRepositoryTemplate;
30
31
    public function __construct()
32
    {
33
        $this->entityTemplate = (string) file_get_contents(__DIR__.'/../Resources/skeleton/orm/entity.mustache');
34
        $this->entityRepositoryTemplate = (string) file_get_contents(__DIR__.'/../Resources/skeleton/orm/repository.mustache');
35
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void
41
    {
42
        $this->generateMappingEntityFiles($output, $bundleMetadata);
43
        $this->generateEntityFiles($output, $bundleMetadata);
44
        $this->generateEntityRepositoryFiles($output, $bundleMetadata);
45
    }
46
47
    public function generateMappingEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
48
    {
49
        $output->writeln(' - Copy entity files');
50
51
        $files = $bundleMetadata->getOrmMetadata()->getEntityMappingFiles();
52
        foreach ($files as $file) {
53
            // copy mapping definition
54
            $fileName = substr($file->getFileName(), 0, strrpos($file->getFileName(), '.'));
55
56
            $dest_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getExtendedMappingEntityDirectory(), $fileName);
57
            $src_file = sprintf('%s/%s', $bundleMetadata->getOrmMetadata()->getMappingEntityDirectory(), $file->getFileName());
58
59
            if (is_file($dest_file)) {
60
                $output->writeln(sprintf('   ~ <info>%s</info>', $fileName));
61
            } else {
62
                $output->writeln(sprintf('   + <info>%s</info>', $fileName));
63
64
                $mappingEntityTemplate = file_get_contents($src_file);
65
66
                $string = Mustache::replace($mappingEntityTemplate, [
67
                    'namespace' => $bundleMetadata->getExtendedNamespace(),
68
                ]);
69
70
                file_put_contents($dest_file, $string);
71
            }
72
        }
73
    }
74
75
    public function generateEntityFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
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(), [
104
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
105
                    'name' => $extendedName,
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
    public function generateEntityRepositoryFiles(OutputInterface $output, BundleMetadata $bundleMetadata): void
117
    {
118
        $output->writeln(' - Generating entity repository files');
119
120
        $names = $bundleMetadata->getOrmMetadata()->getEntityNames();
121
122
        foreach ($names as $name) {
123
            $dest_file = sprintf('%s/%sRepository.php', $bundleMetadata->getOrmMetadata()->getExtendedEntityDirectory(), $name);
124
            $src_file = sprintf('%s/Base%sRepository.php', $bundleMetadata->getOrmMetadata()->getEntityDirectory(), $name);
125
126
            if (!is_file($src_file)) {
127
                $output->writeln(sprintf('   ! <info>%sRepository</info>', $name));
128
129
                continue;
130
            }
131
132
            if (is_file($dest_file)) {
133
                $output->writeln(sprintf('   ~ <info>%sRepository</info>', $name));
134
            } else {
135
                $output->writeln(sprintf('   + <info>%sRepository</info>', $name));
136
137
                $string = Mustache::replace($this->getEntityRepositoryTemplate(), [
138
                    'extended_namespace' => $bundleMetadata->getExtendedNamespace(),
139
                    'name' => $name,
140
                    'namespace' => $bundleMetadata->getNamespace(),
141
                ]);
142
143
                file_put_contents($dest_file, $string);
144
            }
145
        }
146
    }
147
148
    public function getEntityTemplate(): string
149
    {
150
        return $this->entityTemplate;
151
    }
152
153
    public function getEntityRepositoryTemplate(): string
154
    {
155
        return $this->entityRepositoryTemplate;
156
    }
157
}
158