Completed
Push — 2.x ( dc834d...91b37b )
by
unknown
02:07
created

OdmGenerator::generate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
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
                copy($src_file, $dest_file);
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