Completed
Push — master ( 60d200...e262bd )
by Andrea
08:44
created

GeneratorHelper::getScriptGenerator()   B

Complexity

Conditions 6
Paths 16

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 0
cts 14
cp 0
rs 8.5125
c 0
b 0
f 0
cc 6
eloc 16
nc 16
nop 0
crap 42
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\DependencyInjection;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Finder\Finder;
7
use MwbExporter\Model\Table;
8
9
class GeneratorHelper
10
{
11
12
    private $container;
13
    private $apppaths;
14
15
    public function __construct($container)
16
    {
17
        $this->container = $container;
18
        $this->apppaths = new ProjectPath($container);
19
    }
20
21
    public function getDestinationEntityYmlPath($bundlePath)
22
    {
23
        return $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
24
                $bundlePath . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR .
25
                'config' . DIRECTORY_SEPARATOR . 'doctrine' . DIRECTORY_SEPARATOR;
26
    }
27
28
    public function checktables($destinationPath, $wbFile, $output)
29
    {
30
        $finder = new Finder();
31
        $fs = new Filesystem();
32
33
        $pathdoctrineyml = $destinationPath;
34
35
        //Si converte il nome file tabella.orm.yml se ha undercore
36
        $finder->in($pathdoctrineyml)->files()->name('*_*');
37
        $table = new Table();
38
39
        foreach ($finder as $file) {
40
            $oldfilename = $file->getPathName();
41
            $newfilename = $pathdoctrineyml . DIRECTORY_SEPARATOR . $table->beautify($file->getFileName());
42
            $fs->rename($oldfilename, $newfilename, true);
43
        }
44
45
        //Si cercano file con nomi errati
46
        $finderwrong = new Finder();
47
        $finderwrong->in($pathdoctrineyml)->files()->name('*_*');
48
        $wrongfilename = array();
49
        if (count($finderwrong) > 0) {
50
            foreach ($finderwrong as $file) {
51
                $wrongfilename[] = $file->getFileName();
52
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
53
            }
54
        }
55
        $finderwrongcapitalize = new Finder();
56
        $finderwrongcapitalize->in($pathdoctrineyml)->files()->name('*.yml');
57
        foreach ($finderwrongcapitalize as $file) {
58
            if (!ctype_upper(substr($file->getFileName(), 0, 1))) {
59
                $wrongfilename[] = $file->getFileName();
60
                $fs->remove($pathdoctrineyml . DIRECTORY_SEPARATOR . $file->getFileName());
61
            }
62
        }
63
64
        if (count($wrongfilename) > 0) {
65
            $errout = '<error>Ci sono tabelle nel file ' . $wbFile . ' con nomi non consentiti:' .
66
                    implode(',', $wrongfilename) .
67
                    '. I nomi tabella devono essere : con la prima lettera maiuscola,underscore ammesso,doppio underscore non ammesso</error>';
68
69
            $output->writeln($errout);
70
71
            return -1;
72
        } else {
73
            return 0;
74
        }
75
    }
76
77
    public function checkprerequisiti($bundlename, $mwbfile, $output)
78
    {
79
        $fs = new Filesystem();
80
81
        $wbFile = $this->apppaths->getDocPath() . DIRECTORY_SEPARATOR . $mwbfile;
82
        $bundlePath = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename;
83
84
        $viewsPath = $bundlePath .
85
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR;
86
        $entityPath = $bundlePath .
87
                DIRECTORY_SEPARATOR . 'Entity' . DIRECTORY_SEPARATOR;
88
        $formPath = $bundlePath .
89
                DIRECTORY_SEPARATOR . 'Form' . DIRECTORY_SEPARATOR;
90
91
        $scriptGenerator = $this->getScriptGenerator();
92
93
        $destinationPath = $this->getDestinationEntityYmlPath($bundlename);
94
        $output->writeln('Creazione entities yml in ' . $destinationPath . ' da file ' . $mwbfile);
95
        $destinationPath = $destinationPath . 'doctrine' . DIRECTORY_SEPARATOR;
96
97
        if (!$fs->exists($bundlePath)) {
98
            $output->writeln('<error>Non esiste la cartella del bundle ' . $bundlePath . '</error>');
99
100
            return -1;
101
        }
102
103
        /* Creazione cartelle se non esistono nel bundle per l'esportazione */
104
        $fs->mkdir($destinationPath);
105
        $fs->mkdir($entityPath);
106
        $fs->mkdir($formPath);
107
        $fs->mkdir($viewsPath);
108
109
        if (!$fs->exists($wbFile)) {
110
            $output->writeln("<error>Nella cartella 'doc' non è presente il file " . $mwbfile . '!');
111
112
            return -1;
113
        }
114
115
        if (!$fs->exists($scriptGenerator)) {
116
            $output->writeln('<error>Non è presente il comando ' . $scriptGenerator . ' per esportare il modello!</error>');
117
118
            return -1;
119
        }
120
        if (!$fs->exists($destinationPath)) {
121
            $output->writeln("<error>Non esiste la cartella per l'esportazione " . $destinationPath . ', controllare il nome del Bundle!</error>');
122
123
            return -1;
124
        }
125
126
        return 0;
127
    }
128
129
    public function getScriptGenerator()
130
    {
131
        $scriptGenerator = "";
132
        if (version_compare(\Symfony\Component\HttpKernel\Kernel::VERSION, '3.0') >= 0) {
133
            $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
134
        } else {
135
            try {
136
                $scriptGenerator = $this->apppaths->getBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
137
            } catch (\Exception $exc) {
138
                //echo $exc->getTraceAsString();
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
139
                if (!file_exists($scriptGenerator)) {
140
                    $scriptGenerator = $this->apppaths->getVendorBinPath() . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
141
                }
142
            }
143
        }
144
        if (!file_exists($scriptGenerator)) {
145
            $scriptGenerator = dirname(dirname(__FILE__)) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR .
146
                    'vendor' . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . 'mysql-workbench-schema-export';
147
        }
148
        if (!$scriptGenerator) {
149
            throw new \Exception("mysql-workbench-schema-export non trovato", -100);
150
        }
151
        return $scriptGenerator;
152
    }
153
154
    public function getExportJsonFile()
155
    {
156
        $fs = new Filesystem();
157
        $cachedir = $this->apppaths->getCachePath();
158
        $exportJson = $cachedir . DIRECTORY_SEPARATOR . 'export.json';
159
        if ($fs->exists($exportJson)) {
160
            $fs->remove($exportJson);
161
        }
162
163
        return $exportJson;
164
    }
165
166
    public function removeExportJsonFile()
167
    {
168
        $this->getExportJsonFile();
169
170
        return true;
171
    }
172
173
    public static function getJsonMwbGenerator()
174
    {
175
        $jsonTemplate = <<<EOF
176
{"export": "doctrine2-yaml", 
177
  "zip": false, 
178
  "dir": "[dir]", 
179
  "params": 
180
    {"indentation": 4, 
181
    "useTabs": false, 
182
    "filename": "%table%.orm.%extension%", 
183
    "skipPluralNameChecking": true, 
184
    "backupExistingFile": false, 
185
    "addGeneratorInfoAsComment":false,
186
    "useLoggedStorage": false, 
187
    "enhanceManyToManyDetection": true, 
188
    "logToConsole": false, 
189
    "logFile": "", 
190
    "bundleNamespace": "[bundle]", 
191
    "entityNamespace": "Entity", 
192
    "repositoryNamespace": "%entity%", 
193
    "useAutomaticRepository": false, 
194
    "extendTableNameWithSchemaName": false}}
195
EOF;
196
        return $jsonTemplate;
197
    }
198
}
199