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

OdmGenerator::generateMappingDocumentFiles()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

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