SerializerGenerator::generateOrmSerializer()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.6666
c 0
b 0
f 0
cc 4
nc 3
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 SerializerGenerator implements GeneratorInterface
20
{
21
    /**
22
     * @var string
23
     */
24
    protected $entitySerializerTemplate;
25
26
    /**
27
     * @var string
28
     */
29
    protected $documentSerializerTemplate;
30
31
    public function __construct()
32
    {
33
        $this->entitySerializerTemplate = (string) file_get_contents(
34
            __DIR__.'/../Resources/skeleton/serializer/entity.mustache'
35
        );
36
        $this->documentSerializerTemplate = (string) file_get_contents(
37
            __DIR__.'/../Resources/skeleton/serializer/document.mustache'
38
        );
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function generate(OutputInterface $output, BundleMetadata $bundleMetadata): void
45
    {
46
        $this->generateOrmSerializer($output, $bundleMetadata);
47
        $this->generateOdmSerializer($output, $bundleMetadata);
48
        $this->generatePhpcrSerializer($output, $bundleMetadata);
49
    }
50
51
    protected function generateOrmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void
52
    {
53
        $names = $bundleMetadata->getOrmMetadata()->getEntityNames();
54
55
        if (\is_array($names) && \count($names) > 0) {
56
            $output->writeln(' - Generating ORM serializer files');
57
58
            foreach ($names as $name) {
59
                $destFile = sprintf(
60
                    '%s/Entity.%s.xml',
61
                    $bundleMetadata->getOrmMetadata()->getExtendedSerializerDirectory(),
62
                    $name
63
                );
64
65
                $this->writeSerializerFile($output, $bundleMetadata, $this->entitySerializerTemplate, $destFile, $name);
66
            }
67
        }
68
    }
69
70
    protected function generateOdmSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void
71
    {
72
        $names = $bundleMetadata->getOdmMetadata()->getDocumentNames();
73
74
        if (\is_array($names) && \count($names) > 0) {
75
            $output->writeln(' - Generating ODM serializer files');
76
77
            foreach ($names as $name) {
78
                $destFile = sprintf(
79
                    '%s/Document.%s.xml',
80
                    $bundleMetadata->getOdmMetadata()->getExtendedSerializerDirectory(),
81
                    $name
82
                );
83
84
                $this->writeSerializerFile(
85
                    $output,
86
                    $bundleMetadata,
87
                    $this->documentSerializerTemplate,
88
                    $destFile,
89
                    $name
90
                );
91
            }
92
        }
93
    }
94
95
    protected function generatePhpcrSerializer(OutputInterface $output, BundleMetadata $bundleMetadata): void
96
    {
97
        $names = $bundleMetadata->getPhpcrMetadata()->getDocumentNames();
98
99
        if (\is_array($names) && \count($names) > 0) {
100
            $output->writeln(' - Generating PHPCR serializer files');
101
102
            foreach ($names as $name) {
103
                $destFile = sprintf(
104
                    '%s/Document.%s.xml',
105
                    $bundleMetadata->getPhpcrMetadata()->getExtendedSerializerDirectory(),
106
                    $name
107
                );
108
109
                $this->writeSerializerFile(
110
                    $output,
111
                    $bundleMetadata,
112
                    $this->documentSerializerTemplate,
113
                    $destFile,
114
                    $name
115
                );
116
            }
117
        }
118
    }
119
120
    protected function writeSerializerFile(OutputInterface $output, BundleMetadata $bundleMetadata, string $template, string $destFile, string $name): void
121
    {
122
        if (is_file($destFile)) {
123
            $output->writeln(sprintf('   ~ <info>%s</info>', $name));
124
        } else {
125
            $output->writeln(sprintf('   + <info>%s</info>', $name));
126
127
            $string = Mustache::replace($template, [
128
                'name' => $name,
129
                'namespace' => $bundleMetadata->getExtendedNamespace(),
130
                'root_name' => strtolower(preg_replace('/[A-Z]/', '_\\0', $name)),
131
            ]);
132
133
            file_put_contents($destFile, $string);
134
        }
135
    }
136
}
137