OdmGenerator::generateDocumentRepositoryFiles()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

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