Completed
Push — master ( 58cbfb...3697fd )
by Andrea
47:14 queued 44:56
created

GenerateFormCommand::getControllerCode()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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