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