Completed
Pull Request — 2.x (#93)
by
unknown
02:09
created

SerializerGenerator::writeSerializerFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

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