Completed
Push — master ( d32927...a316bd )
by Andrea
11:24
created

GenerateFormCommand::cleanTemplatePath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 1
crap 12
1
<?php
2
3
namespace Fi\PannelloAmministrazioneBundle\Command;
4
5
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6
use Symfony\Component\Console\Input\InputArgument;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\Filesystem\Filesystem;
10
use Symfony\Component\Finder\Finder;
11
12
class GenerateFormCommand extends ContainerAwareCommand
13
{
14
15
    protected $apppaths;
16
    protected $genhelper;
17
    protected $pammutils;
18
19 2
    protected function configure()
20
    {
21 2
        $this
22 2
                ->setName('pannelloamministrazione:generateformcrud')
23 2
                ->setDescription('Genera le views per il crud')
24 2
                ->setHelp('Genera le views per il crud, <br/>fifree.mwb Fi/CoreBundle default [--schemaupdate]<br/>')
25 2
                ->addArgument('bundlename', InputArgument::REQUIRED, 'Nome del bundle, Fi/CoreBundle')
26 2
                ->addArgument('entityform', InputArgument::REQUIRED, 'Il nome entity del form da creare');
27 2
    }
28
29
    protected function execute(InputInterface $input, OutputInterface $output)
30
    {
31
        set_time_limit(0);
32
        $this->apppaths = $this->getContainer()->get("pannelloamministrazione.projectpath");
33
34
        $bundlename = $input->getArgument('bundlename');
35
        $entityform = $input->getArgument('entityform');
36
        $fs = new Filesystem();
37
        //Controller
38
        $controlleFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR .
39
                $bundlename . DIRECTORY_SEPARATOR . 'Controller' . DIRECTORY_SEPARATOR .
40
                $entityform . 'Controller.php';
41
        $code = $this->getControllerCode(str_replace('/', '\\', $bundlename), $entityform);
42
        $fs->dumpFile($controlleFile, $code);
43
44
        //Routing
45
        $retmsg = $this->generateFormRouting($bundlename, $entityform);
46
        //Twig template (Crea i template per new edit show)
47
        $this->generateFormWiew($bundlename, $entityform, 'edit');
48
        $this->generateFormWiew($bundlename, $entityform, 'index');
49
        $this->generateFormWiew($bundlename, $entityform, 'new');
50
        $appviews = $this->apppaths->getAppPath() . DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'views';
51
        $this->cleanTemplatePath($appviews);
52
53
        $resourcesviews = $this->apppaths->getAppPath() . DIRECTORY_SEPARATOR . 'Resources';
54
        $this->cleanTemplatePath($resourcesviews);
55
56
        $this->generateFormsDefaultTableValues($entityform);
57
58
        return $retmsg;
59
    }
60
61
    private function generateFormRouting($bundlename, $entityform)
62
    {
63
        //Routing del form
64
        $fs = new Filesystem();
65
        $routingFile = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename .
66
                DIRECTORY_SEPARATOR . 'Resources' . DIRECTORY_SEPARATOR . 'config' .
67
                DIRECTORY_SEPARATOR . 'routing' . DIRECTORY_SEPARATOR . strtolower($entityform) . '.yml';
68
69
        $code = $this->getRoutingCode(str_replace('/', '', $bundlename), $entityform);
70
        $fs->dumpFile($routingFile, $code);
71
72
        //Fixed: Adesso questa parte la fa da solo symfony (05/2015)
73
        //Refixed dalla versione 2.8 non lo fa più (04/2016)
74
75
        $dest = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
76
                'Resources' . DIRECTORY_SEPARATOR . 'config' . DIRECTORY_SEPARATOR . 'routing.yml';
77
78
        $routingContext = "\n" . str_replace('/', '', $bundlename) . '_' . $entityform . ': ' . "\n" .
79
                '  resource: "@' . str_replace('/', '', $bundlename) . '/Resources/config/routing/' . strtolower($entityform) . '.yml"' . "\n" .
80
                '  prefix: /' . $entityform . "\n";
81
82
        //Si fa l'append nel file routing del bundle per aggiungerci le rotte della tabella che stiamo gestendo
83
        $fh = fopen($dest, 'a');
84
        fwrite($fh, $routingContext);
85
        fclose($fh);
86
        $retmsg = 'Routing ' . $dest . " generato automaticamente da pannelloammonistrazionebundle\n\n* * * * CLEAR CACHE * * * *\n";
87
88
        return $retmsg;
89
    }
90
91
    private function generateFormWiew($bundlename, $entityform, $view)
92
    {
93
        $fs = new Filesystem();
94
        $folderview = $this->apppaths->getSrcPath() . DIRECTORY_SEPARATOR . $bundlename . DIRECTORY_SEPARATOR .
95
                'Resources' . DIRECTORY_SEPARATOR . 'views' . DIRECTORY_SEPARATOR .
96
                $entityform . DIRECTORY_SEPARATOR;
97
        $dest = $folderview . $view . '.html.twig';
98
        $fs->mkdir($folderview);
99
        file_put_contents($dest, "{% include 'FiCoreBundle:Standard:" . $view . ".html.twig' %}");
100
    }
101
102
    private function generateFormsDefaultTableValues($entityform)
103
    {
104
        //Si inserisce il record di default nella tabella permessi
105
        $em = $this->getContainer()->get('doctrine')->getManager();
106
        $ruoloAmm = $em->getRepository('FiCoreBundle:Ruoli')->findOneBy(array('is_superadmin' => true)); //SuperAdmin
107
108
        $newPermesso = new \Fi\CoreBundle\Entity\Permessi();
109
        $newPermesso->setCrud('crud');
110
        $newPermesso->setModulo($entityform);
111
        $newPermesso->setRuoli($ruoloAmm);
112
        $em->persist($newPermesso);
113
        $em->flush();
114
115
        $tabelle = new \Fi\CoreBundle\Entity\Tabelle();
116
        $tabelle->setNometabella($entityform);
117
        $em->persist($tabelle);
118
        $em->flush();
119
    }
120
121
    private function getControllerCode($bundlename, $tabella)
122
    {
123
        $codeTemplate = <<<EOF
124
<?php
125
namespace [bundle]\Controller;
126
127
use Fi\CoreBundle\Controller\FiController;
128
use Symfony\Component\HttpFoundation\Request;
129
use Symfony\Component\HttpFoundation\Response;
130
use Fi\CoreBundle\Controller\Griglia;
131
use [bundle]\Entity\[tabella];
132
use [bundle]\Form\[tabella]Type;
133
134
135
/**
136
* [tabella] controller.
137
*
138
*/
139
140
class [tabella]Controller extends FiController {
141
142
}
143
EOF;
144
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
145
        $code = str_replace('[tabella]', $tabella, $codebundle);
146
147
        return $code;
148
    }
149
150
    private function getRoutingCode($bundlename, $tabella)
151
    {
152
        $codeTemplate = <<<'EOF'
153
[tabella]_container:
154
    path:  /
155
    defaults: { _controller: "[bundle]:[tabella]:index" }
156
157
[tabella]_new:
158
    path:  /new
159
    defaults: { _controller: "[bundle]:[tabella]:new" }
160
161
[tabella]_create:
162
    path:  /create
163
    defaults: { _controller: "[bundle]:[tabella]:create" }
164
    requirements: { methods: post }
165
166
[tabella]_edit:
167
    path:  /{id}/edit
168
    defaults: { _controller: "[bundle]:[tabella]:edit" }
169
170
[tabella]_update:
171
    path:  /{id}/update
172
    defaults: { _controller: "[bundle]:[tabella]:update" }
173
    requirements: { methods: post|put }
174
175
[tabella]_aggiorna:
176
    path:  /aggiorna
177
    defaults: { _controller: "[bundle]:[tabella]:aggiorna" }
178
    requirements: { methods: post|put }
179
180
[tabella]_delete:
181
    path:  /{id}/delete
182
    defaults: { _controller: "[bundle]:[tabella]:delete" }
183
    requirements: { methods: post|delete }
184
185
[tabella]_deletemultiple:
186
    path:  /delete
187
    defaults: { _controller: "[bundle]:[tabella]:delete" }
188
    requirements: { methods: post|delete }
189
190
[tabella]_griglia:
191
    path:  /griglia
192
    defaults: { _controller: "[bundle]:[tabella]:Griglia" }
193
    requirements: { methods: get|post }
194
EOF;
195
        $codebundle = str_replace('[bundle]', $bundlename, $codeTemplate);
196
        $code = str_replace('[tabella]', $tabella, $codebundle);
197
198
        return $code;
199
    }
200
201
    private function cleanTemplatePath($path)
202
    {
203
        $fs = new Filesystem();
204
        $ret = 0;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
205
        if ($fs->exists($path)) {
206
            $finder = new Finder();
207
            $ret = $finder->files()->in($path);
208
            if (count($ret) == 0) {
209
                $fs->remove($path);
210
            }
211
        }
212
    }
213
}
214