Completed
Push — master ( c1e214...b66c83 )
by Andrea
11:11
created

GenerateFormCommand::execute()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 45
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 2.001

Importance

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